normalize display math fences attached to content / nested in lists - #332
Open
dykhgit wants to merge 2 commits into
Open
normalize display math fences attached to content / nested in lists#332dykhgit wants to merge 2 commits into
dykhgit wants to merge 2 commits into
Conversation
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.
Fix display math swallowing text after the formula
Problem
normalizeDisplayMathinlib/markdown.tsrewrites display math so thatremark-math (which requires
$$to sit on their own lines) can parse it.Three common LLM output shapes are currently normalized incorrectly, and each
makes the text after the formula render as a giant red
katex-errorblock(
ParseError: Can't use function '$' in math mode):Single-line
$$…$$nested in a GFM list itemThe split emits
$$/ content /$$. Inside a list item thecolumn-0 content line is a lazy continuation, which breaks remark-math's
math-flow tokenizer: the opening
$$becomes an empty math node and theclosing one becomes a new opener that swallows all following text.
Multi-line block with delimiters glued to content
remark-math never finds a valid fence (delimiters must start their own
lines), so the whole block — and everything after it — is consumed as math.
Indented bare
$$fences inside a list item with column-0 content:same lazy-continuation breakage as (1).
Fix
$$fence when the block isindented, keeping it inside the list item (no lazy continuation).
$$blocks with glued delimiters to$$\n<content>\n$$.$$and re-indent lazy content of indented barefences.
\[…\]handling are unchanged;a top-level block whose delimiters are already on their own lines is left
untouched.
Tests
Adds
lib/markdown.test.mjs(12 cases) using the built-innode:testrunner —no new dependencies, no lockfile changes. Run with
npm test.Verified end-to-end through the app's render pipeline
(
remark-gfm + remark-math → rehype-raw → rehype-sanitize → rehype-katex):both affected real-world answers drop from 3
katex-errorspans and dozens ofstray
$to 0; a regression pass over all local session texts reports 0 newfailures.
tsc --noEmitis clean.