feat(web): render $…$ and $$…$$ math with KaTeX - #1
Merged
Merged
Conversation
|
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
1 Skipped Deployment
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Notes showed LaTeX math as raw
$…$/$$…$$source. This renders it with KaTeX everywhere note Markdown is read:renderNote(preview, Screen cards, Study lanes, tables and directive bodies) — amarkedextension inapps/web/src/lib/math.ts: a block token for$$…$$on its own lines (multi-line allowed) and an inline token for$…$/$$…$$inside text.$…$and$$…$$are replaced by aMathFormulawidget; the source comes back while the cursor is on the formula's lines, and clicking a formula puts the cursor there. A display formula alone on its lines is drawn as a block. Code, HTML blocks, tables, mermaid fences and collapsed directives are excluded from the scan (tables/directives render their own math throughrenderNote).Parsing rules (shared by both surfaces)
Pandoc's
tex_math_dollarsrules, so prices stay text: an inline opener must be followed by a non-space, the closer must follow a non-space and not be followed by a digit, and inline math stays on one line. Display math may span lines but not a blank line.\$escapes a dollar.It costs $5 and $10stays literal.Safety
trust: false(no\href/\url/\html*output) andthrowOnError: false(a bad formula shows as a red error in place).output: 'html': DOMPurify drops MathML<semantics>/<annotation>but keeps their text, which would leak the TeX source into the formula. The wrapper<span class="note-math" role="math" aria-label="…TeX…">carries the source for assistive technology instead.Dependency
katex@^0.16.47— the version mermaid 12 already pins, so the app ships one KaTeX copy and the lockfile gains three lines (0.18.x would add a second copy and re-resolve unrelated peers).Tests
apps/web/src/lib/math.test.ts(jsdom + real DOMPurify): scanner rules, inline/display/multi-line rendering, sample-note formulas next to CJK punctuation ($p(9)$,$|\{\text{Socrates}\}|$,$\Box\exists x Fx$,$\#(F)$), prices and code stay literal, tables and directives, sanitizer safety, parse errors. 5 of these failed before the change.apps/web/src/components/live-markdown/math.test.ts: editor widgets, cursor reveal, tables excluded.pnpm test196 files / 1400 tests pass;pnpm lintclean;tsc --noEmitandvite buildpass.pnpm format:checkreports the same 10 pre-existing files ascore; none are touched here.Print / PDF export
note-export.tsbuilds a standalonesrcdocframe that loads none of the app's stylesheets, so it now inlines KaTeX's stylesheet (katex.min.css?inline). Vite rewrites the fonturl()s to root-absolute asset paths (/assets/KaTeX_*.woff2in the build), which the frame resolves against the app's URL. Printing waits fordocument.fonts.readyso formulas are not laid out in fallback fonts. The print document builder is extracted asprintDocument();note-export.math.test.tschecks it carries the KaTeX rules and font faces (a rootvitest.config.tslets Vitest load KaTeX CSS instead of emptying it).