Skip to content

fix(kit): restore whitespace eaten by \(...\) and $$ KaTeX markers - #817

Open
hungnnvidia wants to merge 1 commit into
huggingface:mainfrom
hungnnvidia:fix/katex-preserve-math-whitespace
Open

fix(kit): restore whitespace eaten by \(...\) and $$ KaTeX markers#817
hungnnvidia wants to merge 1 commit into
huggingface:mainfrom
hungnnvidia:fix/katex-preserve-math-whitespace

Conversation

@hungnnvidia

Copy link
Copy Markdown

Summary

This does not take on the rest of #809 (switching to remark-math / supporting \[...\] / relaxing $...$ punctuation boundaries). Those are larger syntax-policy changes.

Test plan

  • cd kit && npm test
  • uv run --python 3.10 python -m pytest -n 1 --dist=loadfile -q ./tests/ (138 passed; unrelated to this change)
  • Confirm Deep RL course page (units/en/unit2/mc-vs-td) no longer renders one stepS_{t+1}

Fixes #809 (whitespace-eating part)

The inline `\(...\)` and display `$$` extractors required a leading space/newline
but dropped it from the replacement, gluing formulas to the preceding text.

@coyotte508 coyotte508 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Automated bot review — this review was generated by an AI agent (moon-coder). It may contain mistakes; please verify findings before acting on them.

This PR fixes a real whitespace-eating bug in markKatex (kit/preprocessors/mdsvex/index.js) by capturing the mandatory leading \n/\s before $$...$$ and \\(...\\) and re-emitting it in the replacement — exactly the pattern the $...$ branch already used — and adds the first node --test unit tests for the preprocessor. The change is minimal, correct, and well-scoped (it deliberately defers the larger remark-math/\[...\] questions from #809).

Findings

  • 🟡 New test suite isn't wired into CI. .github/workflows/lint-svelte-kit.yml runs npm run lint and npm run check (lines 29–35) but never npm test, so the new kit/package.json "test" script only runs locally. Suggest adding an npm test step (with working-directory: kit) so the regression test actually guards against regressions.
  • 🟡 Test imports a module with heavy side effects. markKatex.test.js imports ./index.js, whose module scope calls hljs.addPlugin(...), constructs mdsvex(...), and does a top-level await findSvelteComponentNames("./src/lib") (index.js:166–167) — a filesystem scan relative to process.cwd(). The test therefore requires npm ci in kit/ and only works when run from the kit/ directory (npm test guarantees that, but bare node --test from repo root would fail). Consider extracting markKatex/renderKatex into a small pure module (e.g. katex.js) so the unit test has no import side effects.
  • 🔵 Known limitations left as-is (fine, but worth a line in the PR body): math at the very start of the content still won't match (both regexes require a preceding \n/\s, index.js:105–106), and $...$ still consumes its trailing space from the scanner's perspective, so $a$ $b$ with a single separating space only marks the first. Both pre-existing, not regressions.
  • 🔵 The display regex comment says "leading \s/\n" collectively; the display marker specifically requires \n only. Trivial wording nit in the new comment block (index.js:98–101).

Correctness itself checks out: tracing the replacement flow in index.js:100–128, restoring the captured char cannot create spurious matches in the two subsequent .replace passes (markers contain no $, \, or whitespace), and the four tests cover the reported repro (one stepS_{t+1}), consecutive-line collapse, $$ paragraph boundaries, and the untouched $...$ path. The two-backslash test inputs correctly mirror what convert_rst_to_mdx/notebook conversion emits. Style (tabs, double quotes) matches the file.

Verdict: LGTM with minor suggestions — the fix is correct and consistent with the existing $...$ handling; the only meaningful gap is that CI doesn't run the new tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inline \(...\) math swallows the preceding whitespace; the custom delimiter regexes also diverge from standard KaTeX

2 participants