How rutforcode/formula-detector works
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: App.tsx App · detection.ts Detector factory · patternDetector.ts Pattern detector · toLatex.ts LaTeX converter
Components 17
Browser UI
Client-side React application.
- Browser entry
src/main.tsx - App · Derives formulas and rendered output on source changes.
src/App.tsx - Editor
src/components/Editor.tsx - Preview · Renders prose as text nodes and appends math SVG.
src/components/Preview.tsx - Detected list
src/components/DetectedList.tsx
Detection pipeline
Synchronous pure formula recognition.
- Detector factory · Prioritizes raw LaTeX claims before plain-text spans.
src/lib/detection.ts - Detector contracts
src/lib/types.ts - LaTeX detector · Finds delimited and known bare LaTeX without conversion.
src/lib/latexDetector.ts - Pattern detector · Selects valid math-like spans and block formulas.
src/lib/patternDetector.ts - Tokenizer
src/lib/tokenizer.ts - Math vocabulary · Shared semantics for recognition and conversion.
src/lib/mathVocab.ts - LLM detector · Not used by the synchronous runtime flow.
src/lib/llmDetector.ts
Conversion & rendering
Safe local LaTeX and SVG output.
- LaTeX converter · Applies fixed mappings and escaping.
src/lib/toLatex.ts - MathJax SVG renderer · Initializes once and returns detached SVG containers.
src/lib/mathjax.ts
Build & deployment
Static-site build and publishing.
- Vite
vite.config.ts - Static production build · Suitable for GitHub Pages.
- GitHub Pages deploy · Deploys pushes from main.
.github/workflows/deploy.yml
Connections 19
- 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