fix(kit): restore whitespace eaten by \(...\) and $$ KaTeX markers - #817
fix(kit): restore whitespace eaten by \(...\) and $$ KaTeX markers#817hungnnvidia wants to merge 1 commit into
Conversation
The inline `\(...\)` and display `$$` extractors required a leading space/newline but dropped it from the replacement, gluing formulas to the preceding text.
coyotte508
left a comment
There was a problem hiding this comment.
🤖 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.ymlrunsnpm run lintandnpm run check(lines 29–35) but nevernpm test, so the newkit/package.json"test"script only runs locally. Suggest adding annpm teststep (withworking-directory: kit) so the regression test actually guards against regressions. - 🟡 Test imports a module with heavy side effects.
markKatex.test.jsimports./index.js, whose module scope callshljs.addPlugin(...), constructsmdsvex(...), and does a top-levelawait findSvelteComponentNames("./src/lib")(index.js:166–167) — a filesystem scan relative toprocess.cwd(). The test therefore requiresnpm ciinkit/and only works when run from thekit/directory (npm testguarantees that, but barenode --testfrom repo root would fail). Consider extractingmarkKatex/renderKatexinto 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\nonly. 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.
Summary
\(...\)and$$...$$require to match, which glued formulas to the preceding text and collapsed consecutive\( ... \)lines (reported in Inline\(...\)math swallows the preceding whitespace; the custom delimiter regexes also diverge from standard KaTeX #809).$...$already captured and restored surrounding spaces; the other two delimiters now do the same.node --testcoverage formarkKatex(npm testinkit/).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 testuv run --python 3.10 python -m pytest -n 1 --dist=loadfile -q ./tests/(138 passed; unrelated to this change)units/en/unit2/mc-vs-td) no longer rendersone stepS_{t+1}Fixes #809 (whitespace-eating part)