# rutforcode/formula-detector architecture

> Architecture diagram of the GitHub repository rutforcode/formula-detector, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rutforcode/formula-detector
- Repository: https://github.com/rutforcode/formula-detector

## Overview

#### Purpose and Runtime

Formula Detector is a client-only React/TypeScript application that finds mathematical expressions in prose or raw LaTeX, converts plain-text math to safe LaTeX, and renders live SVG math locally. It has no backend, API keys, network calls, or LLM dependency at runtime.

Browser entry points are `src/main.tsx` and `src/App.tsx`; Vite serves and bundles the application.

#### UI Composition

`App.tsx` owns input state and synchronously derives detected formulas and rendered output as text changes. It composes:
- `src/components/Editor.tsx` for source-text entry.
- `src/components/Preview.tsx` for mixed prose/math rendering and HTML copy.
- `src/components/DetectedList.tsx` for formula metadata and converted-text copy.

The UI boundary is intentional: detection/conversion lives in pure library modules, while components append rendered MathJax DOM and handle clipboard interactions.

#### Detector Orchestration

`src/lib/detection.ts` is the detector factory and primary composition point. It chains raw-LaTex detection before plain-text pattern detection, preventing overlapping claims and duplicate formulas.
- `src/lib/types.ts` defines `DetectedFormula` and the `FormulaDetector` contract.
- `src/lib/latexDetector.ts` identifies delimited and bare known LaTeX and passes its contents through unchanged.
- `src/lib/patternDetector.ts` finds math-like spans in ordinary text.

A future asynchronous LLM integration is isolated behind `src/lib/llmDetector.ts`; adopting it would require changing the app’s synchronous derived-data flow to effect-driven async state.

#### Plain-Text Math Recognition

The pattern detector performs the core heuristic pipeline:
1. Tokenize text and match brackets using `src/lib/tokenizer.ts`.
2. Identify math triggers such as relations, operators, symbols, functions, and valid fractions.
3. Expand only through math-like neighboring tokens.
4. Reject prose-contaminated or structurally invalid spans.
5. Mark formulas as block math when they occupy their own line.

Vocabulary-driven recognition and symbol mappings are centralized in `src/lib/mathVocab.ts`, keeping detector and converter semantics aligned.

#### Conversion and Rendering Boundary

`src/lib/toLatex.ts` recursively transforms detected plain-text expressions into LaTeX: scripts, fractions, functions, special notation, vocabulary mappings, and escaping. Raw LaTeX identified by `latexDetector.ts` bypasses this transformation.

`src/lib/mathjax.ts` initializes bundled `mathjax-full` once and produces detached SVG `<mjx-container>` elements through `renderLatex(latex, display)`. MathJax is a rendering boundary: user prose is inserted as text nodes, while generated/passthrough LaTeX is the only math payload.

#### Security Model

The application avoids rendering user input via `innerHTML`. Conversion is constrained by fixed mappings and escaping, and MathJax disables TeX features that could inject links, HTML, or arbitrary Unicode behavior. This makes the input-to-rendering pipeline materially safer than treating source text as HTML.

#### Build and Deployment

Vite provides local development and production bundling, configured in `vite.config.ts`; the production build is static output suitable for GitHub Pages. `.github/workflows/deploy.yml` deploys pushes from `main` to GitHub Pages. Material runtime dependencies are React 18, Vite, TypeScript, and bundled MathJax 3 SVG rendering.

Main entry points: [`src/App.tsx`](https://github.com/rutforcode/formula-detector/blob/main/src/App.tsx) (App), [`src/lib/detection.ts`](https://github.com/rutforcode/formula-detector/blob/main/src/lib/detection.ts) (Detector factory), [`src/lib/patternDetector.ts`](https://github.com/rutforcode/formula-detector/blob/main/src/lib/patternDetector.ts) (Pattern detector), [`src/lib/toLatex.ts`](https://github.com/rutforcode/formula-detector/blob/main/src/lib/toLatex.ts) (LaTeX converter)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_browser["Browser UI"]
  node_main["Browser entry<br/>React bootstrap<br/>[main.tsx]"]
  node_app["App<br/>state &amp; synchronous orchestration<br/>[App.tsx]"]
  node_editor["Editor<br/>source input<br/>[Editor.tsx]"]
  node_preview["Preview<br/>mixed-content view<br/>[Preview.tsx]"]
  node_detected_list["Detected list<br/>formula metadata &amp; copy<br/>[DetectedList.tsx]"]
end

subgraph group_detection["Detection pipeline"]
  node_detection_factory["Detector factory<br/>detector orchestration<br/>[detection.ts]"]
  node_detector_contracts["Detector contracts<br/>formula types<br/>[types.ts]"]
  node_latex_detector["LaTeX detector<br/>raw LaTeX recognition<br/>[latexDetector.ts]"]
  node_pattern_detector["Pattern detector<br/>plain-text heuristics<br/>[patternDetector.ts]"]
  node_tokenizer["Tokenizer<br/>tokens &amp; bracket matching<br/>[tokenizer.ts]"]
  node_math_vocab["Math vocabulary<br/>recognition &amp; mapping data<br/>[mathVocab.ts]"]
  node_llm_detector["LLM detector<br/>future async extension<br/>[llmDetector.ts]"]
end

subgraph group_rendering["Conversion &amp; rendering"]
  node_to_latex["LaTeX converter<br/>recursive plain-text conversion<br/>[toLatex.ts]"]
  node_mathjax["MathJax SVG renderer<br/>local math rendering<br/>[mathjax.ts]"]
end

subgraph group_delivery["Build &amp; deployment"]
  node_vite{{"Vite<br/>dev server &amp; bundler<br/>[vite.config.ts]"}}
  node_static_build["Static production build<br/>deployable site output"]
  node_deploy["GitHub Pages deploy<br/>CI deployment<br/>[deploy.yml]"]
end

node_vite -->|"serves and bundles"| node_main
node_main -->|"mounts"| node_app
node_app -->|"provides input state"| node_editor
node_app -->|"derives formulas"| node_detection_factory
node_app -->|"converts plain-text formulas"| node_to_latex
node_app -->|"provides mixed output"| node_preview
node_app -->|"provides metadata"| node_detected_list
node_detection_factory -->|"uses"| node_detector_contracts
node_detection_factory -->|"runs first"| node_latex_detector
node_detection_factory -->|"runs remaining text"| node_pattern_detector
node_pattern_detector -->|"tokenizes and matches brackets"| node_tokenizer
node_pattern_detector -->|"uses triggers"| node_math_vocab
node_to_latex -->|"uses mappings"| node_math_vocab
node_to_latex -->|"sends generated LaTeX"| node_mathjax
node_latex_detector -->|"passes raw LaTeX"| node_mathjax
node_mathjax -->|"appends SVG math"| node_preview
node_vite -->|"builds"| node_static_build
node_deploy -->|"publishes"| node_static_build
node_llm_detector -.->|"future async integration"| node_detection_factory

click node_vite "https://github.com/rutforcode/formula-detector/blob/main/vite.config.ts"
click node_main "https://github.com/rutforcode/formula-detector/blob/main/src/main.tsx"
click node_app "https://github.com/rutforcode/formula-detector/blob/main/src/App.tsx"
click node_editor "https://github.com/rutforcode/formula-detector/blob/main/src/components/Editor.tsx"
click node_preview "https://github.com/rutforcode/formula-detector/blob/main/src/components/Preview.tsx"
click node_detected_list "https://github.com/rutforcode/formula-detector/blob/main/src/components/DetectedList.tsx"
click node_detection_factory "https://github.com/rutforcode/formula-detector/blob/main/src/lib/detection.ts"
click node_detector_contracts "https://github.com/rutforcode/formula-detector/blob/main/src/lib/types.ts"
click node_latex_detector "https://github.com/rutforcode/formula-detector/blob/main/src/lib/latexDetector.ts"
click node_pattern_detector "https://github.com/rutforcode/formula-detector/blob/main/src/lib/patternDetector.ts"
click node_tokenizer "https://github.com/rutforcode/formula-detector/blob/main/src/lib/tokenizer.ts"
click node_math_vocab "https://github.com/rutforcode/formula-detector/blob/main/src/lib/mathVocab.ts"
click node_llm_detector "https://github.com/rutforcode/formula-detector/blob/main/src/lib/llmDetector.ts"
click node_to_latex "https://github.com/rutforcode/formula-detector/blob/main/src/lib/toLatex.ts"
click node_mathjax "https://github.com/rutforcode/formula-detector/blob/main/src/lib/mathjax.ts"
click node_deploy "https://github.com/rutforcode/formula-detector/blob/main/.github/workflows/deploy.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_app,node_editor,node_preview,node_detected_list toneBlue
class node_detection_factory,node_detector_contracts,node_latex_detector,node_pattern_detector,node_tokenizer,node_math_vocab,node_llm_detector toneAmber
class node_to_latex,node_mathjax toneMint
class node_vite,node_static_build,node_deploy toneRose
```

## Components

### Browser UI

Client-side React application.

- [Browser entry](https://github.com/rutforcode/formula-detector/blob/main/src/main.tsx): React bootstrap · `src/main.tsx`
- [App](https://github.com/rutforcode/formula-detector/blob/main/src/App.tsx): state & synchronous orchestration · Derives formulas and rendered output on source changes. · `src/App.tsx`
- [Editor](https://github.com/rutforcode/formula-detector/blob/main/src/components/Editor.tsx): source input · `src/components/Editor.tsx`
- [Preview](https://github.com/rutforcode/formula-detector/blob/main/src/components/Preview.tsx): mixed-content view · Renders prose as text nodes and appends math SVG. · `src/components/Preview.tsx`
- [Detected list](https://github.com/rutforcode/formula-detector/blob/main/src/components/DetectedList.tsx): formula metadata & copy · `src/components/DetectedList.tsx`

### Detection pipeline

Synchronous pure formula recognition.

- [Detector factory](https://github.com/rutforcode/formula-detector/blob/main/src/lib/detection.ts): detector orchestration · Prioritizes raw LaTeX claims before plain-text spans. · `src/lib/detection.ts`
- [Detector contracts](https://github.com/rutforcode/formula-detector/blob/main/src/lib/types.ts): formula types · `src/lib/types.ts`
- [LaTeX detector](https://github.com/rutforcode/formula-detector/blob/main/src/lib/latexDetector.ts): raw LaTeX recognition · Finds delimited and known bare LaTeX without conversion. · `src/lib/latexDetector.ts`
- [Pattern detector](https://github.com/rutforcode/formula-detector/blob/main/src/lib/patternDetector.ts): plain-text heuristics · Selects valid math-like spans and block formulas. · `src/lib/patternDetector.ts`
- [Tokenizer](https://github.com/rutforcode/formula-detector/blob/main/src/lib/tokenizer.ts): tokens & bracket matching · `src/lib/tokenizer.ts`
- [Math vocabulary](https://github.com/rutforcode/formula-detector/blob/main/src/lib/mathVocab.ts): recognition & mapping data · Shared semantics for recognition and conversion. · `src/lib/mathVocab.ts`
- [LLM detector](https://github.com/rutforcode/formula-detector/blob/main/src/lib/llmDetector.ts): future async extension · Not used by the synchronous runtime flow. · `src/lib/llmDetector.ts`

### Conversion & rendering

Safe local LaTeX and SVG output.

- [LaTeX converter](https://github.com/rutforcode/formula-detector/blob/main/src/lib/toLatex.ts): recursive plain-text conversion · Applies fixed mappings and escaping. · `src/lib/toLatex.ts`
- [MathJax SVG renderer](https://github.com/rutforcode/formula-detector/blob/main/src/lib/mathjax.ts): local math rendering · Initializes once and returns detached SVG containers. · `src/lib/mathjax.ts`

### Build & deployment

Static-site build and publishing.

- [Vite](https://github.com/rutforcode/formula-detector/blob/main/vite.config.ts): dev server & bundler · `vite.config.ts`
- **Static production build**: deployable site output · Suitable for GitHub Pages.
- [GitHub Pages deploy](https://github.com/rutforcode/formula-detector/blob/main/.github/workflows/deploy.yml): CI deployment · Deploys pushes from main. · `.github/workflows/deploy.yml`

## Connections

- Vite → Browser entry: serves and bundles
- Browser entry → App: mounts
- App → Editor: provides input state
- App → Detector factory: derives formulas
- App → LaTeX converter: converts plain-text formulas
- App → Preview: provides mixed output
- App → Detected list: provides metadata
- Detector factory → Detector contracts: uses
- Detector factory → LaTeX detector: runs first
- Detector factory → Pattern detector: runs remaining text
- Pattern detector → Tokenizer: tokenizes and matches brackets
- Pattern detector → Math vocabulary: uses triggers
- LaTeX converter → Math vocabulary: uses mappings
- LaTeX converter → MathJax SVG renderer: sends generated LaTeX
- LaTeX detector → MathJax SVG renderer: passes raw LaTeX
- MathJax SVG renderer → Preview: appends SVG math
- Vite → Static production build: builds
- GitHub Pages deploy → Static production build: publishes
- LLM detector → Detector factory: future async integration
