fix(markdown): restore placeholder blocks with a function replacer#5664
Open
wbaxterh wants to merge 1 commit into
Open
fix(markdown): restore placeholder blocks with a function replacer#5664wbaxterh wants to merge 1 commit into
wbaxterh wants to merge 1 commit into
Conversation
The allowed-HTML, math, mermaid, and code-block restore sites in mdToHtml put content back with String.replace and a *string* replacement, so `$&`, `$\``, `$'`, `$$`, and `$1` inside the restored block were interpreted as special replacement patterns and corrupted the output. These sequences are common in fenced code (shell, sed, regex), so code samples rendered wrong in chat, notes, and documents. Use the function-replacer form `() => block` at all four sites, which inserts the content verbatim — the same fix already applied to the inline-code site right below them. Fixes # Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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
The placeholder-restore pass at the end of
mdToHtmlinstatic/js/markdown.jsputs extracted blocks back withString.prototype.replaceand a string replacement:A string replacement makes
String.replaceinterpret$&,$`,$',$$, and$1..$99as special patterns, so any restored code / allowed-HTML / math / mermaid block containing them is corrupted ($&re-inserts the placeholder,$`inserts everything before it,$'everything after,$$collapses to$,$1becomes an empty back-reference). These sequences are extremely common in fenced code (shell"$&",sed 's/$/…',echo $1,$$USD`), so code samples render mangled in chat, notes, and documents.The inline-code restore site right below (line ~793) was already fixed to use a function replacer
() => blockwith a comment explaining exactly this hazard. This PR applies the same() => blockform to the four sibling sites (allowed-HTML, math, mermaid, code) that were missed. A function replacer inserts the content verbatim.Target branch
dev, notmain.Linked Issue
Fixes #5663
Type of Change
Checklist
devdocker compose uporuvicorn app:app) and verified the change works end-to-end. Type-checks and unit tests are not enough.How to Test
python -m pytest tests/test_markdown_placeholder_restore_js.py -v # or directly: node tests/markdown_codefence_placeholder_regression.mjsExtends the existing
tests/markdown_codefence_placeholder_regression.mjsharness with a fenced code block for each$sequence ($&,$`,$',$$,$1), asserting the surrounding marker/trailing text survives and no___CODE_BLOCK_placeholder leaks. Addstests/test_markdown_placeholder_restore_js.pyso CI's pytest actually runs the node harness (it wasn't wrapped before). Verified the new assertions fail on the pre-fixmarkdown.jsand pass after.