mirror of
https://github.com/pewdiepie-archdaemon/odysseus.git
synced 2026-08-01 19:18:35 -04:00
fix(markdown): restore extracted blocks verbatim so $& and $$ survive (#5768)
The placeholder-restore pass in mdToHtml put code, math, mermaid and allowed-HTML blocks back with a string replacement, so String.replace read `$&`, `` $` ``, `$'` and `$$` in the *replacement* as substitution patterns. A fenced block containing them rendered corrupted: `$&` re-inserted the placeholder (`perl -pe 's/world/$& again/'` became `s/world/___CODE_BLOCK_0___amp; again/`), `` $` `` and `$'` spliced in the surrounding document, and `$$` collapsed to a single `$`. Pass a function replacer at all four sites, matching the inline-code site below them, which was already fixed this way. A function's return value is inserted verbatim with no `$` interpretation. The inline-code comment claimed `echo $1` would be read as a back-reference; with a string search value there are no capture groups, so `$1` is already literal. Reworded to name the four sequences that do corrupt. Fixes #5663
This commit is contained in:
+13
-7
@@ -758,30 +758,36 @@ export function mdToHtml(src, opts) {
|
||||
// Remove empty paragraphs
|
||||
s = s.replace(/<p><\/p>/g, '');
|
||||
|
||||
// Every restore below passes a function replacer rather than the block string
|
||||
// itself. With a string replacement, `String.replace` reads `$&`, `` $` ``,
|
||||
// `$'` and `$$` in the *replacement* as substitution patterns, so a restored
|
||||
// block containing them is corrupted: `$&` re-inserts the placeholder, `` $` ``
|
||||
// and `$'` splice in the surrounding document, and `$$` collapses to `$`. Those
|
||||
// sequences are ordinary content in fenced code (`perl -pe 's/x/$& y/'`,
|
||||
// `echo "$$USD"`). A function replacer inserts its return value verbatim.
|
||||
|
||||
// CRITICAL: Restore allowed HTML blocks first
|
||||
allowedHtmlBlocks.forEach((block, index) => {
|
||||
s = s.replace(`___ALLOWED_HTML_${index}___`, block);
|
||||
s = s.replace(`___ALLOWED_HTML_${index}___`, () => block);
|
||||
});
|
||||
|
||||
// Restore math blocks
|
||||
mathBlocks.forEach((block, index) => {
|
||||
s = s.replace(`___MATH_BLOCK_${index}___`, block);
|
||||
s = s.replace(`___MATH_BLOCK_${index}___`, () => block);
|
||||
});
|
||||
|
||||
// Restore mermaid diagram blocks
|
||||
mermaidBlocks.forEach((block, index) => {
|
||||
s = s.replace(`___MERMAID_BLOCK_${index}___`, block);
|
||||
s = s.replace(`___MERMAID_BLOCK_${index}___`, () => block);
|
||||
});
|
||||
|
||||
// CRITICAL: Restore code blocks at the end
|
||||
codeBlocks.forEach((block, index) => {
|
||||
s = s.replace(`___CODE_BLOCK_${index}___`, block);
|
||||
s = s.replace(`___CODE_BLOCK_${index}___`, () => block);
|
||||
});
|
||||
|
||||
// Restore inline code spans last, so placeholders carried inside restored
|
||||
// <a>/allowed-HTML blocks are resolved too. The function replacer keeps the
|
||||
// escaped code literal — e.g. a shell snippet like `echo $1` is not treated
|
||||
// as a regex back-reference.
|
||||
// <a>/allowed-HTML blocks are resolved too.
|
||||
inlineCodeBlocks.forEach((block, index) => {
|
||||
s = s.replace(`___INLINE_CODE_${index}___`, () => block);
|
||||
});
|
||||
|
||||
@@ -214,6 +214,50 @@ def test_inline_code_content_is_html_escaped(node_available):
|
||||
assert "<b>" not in html
|
||||
|
||||
|
||||
def test_fenced_code_keeps_dollar_ampersand(node_available):
|
||||
# Issue #5663: the block-restore pass used a string replacement, so `$&` in a
|
||||
# restored block was read as "the matched text" and re-inserted the
|
||||
# placeholder. `perl -pe 's/world/$& again/'` rendered as
|
||||
# "s/world/___CODE_BLOCK_0___amp; again/" — the trailing "amp;" is the orphan
|
||||
# left behind after `$&` consumed the `$&` of the escaped `$&`.
|
||||
html = _run_markdown_case(
|
||||
"```sh\necho \"hello world\" | perl -pe 's/world/$& again/'\n```"
|
||||
)
|
||||
|
||||
assert "___CODE_BLOCK_" not in html
|
||||
assert "s/world/$& again/" in html
|
||||
assert "amp; again" not in html.replace("$& again", "")
|
||||
|
||||
|
||||
def test_fenced_code_keeps_dollar_backtick_and_quote(node_available):
|
||||
# `` $` `` and `$'` splice the text before/after the placeholder into the
|
||||
# block. Unlike `$&` these leave no placeholder behind — the characters just
|
||||
# vanish — so assert the content survives verbatim.
|
||||
html = _run_markdown_case("```sh\nsed \"s/$`/x/\" && sed \"s/$'/y/\"\n```")
|
||||
|
||||
assert "___CODE_BLOCK_" not in html
|
||||
assert "s/$`/x/" in html
|
||||
assert "s/$'/y/" in html
|
||||
|
||||
|
||||
def test_fenced_code_keeps_double_dollar(node_available):
|
||||
# `$$` collapsed to a single `$` in the restored block.
|
||||
html = _run_markdown_case('```sh\necho "$$USD and $$"\n```')
|
||||
|
||||
assert "$$USD and $$" in html
|
||||
|
||||
|
||||
def test_mermaid_block_keeps_dollar_ampersand(node_available):
|
||||
# The mermaid restore site had the same hazard: a node label containing `$&`
|
||||
# re-inserted the ___MERMAID_BLOCK_n___ placeholder into the diagram source,
|
||||
# which then fails to parse. The math and allowed-HTML sites are fixed the
|
||||
# same way; they need KaTeX/sanitizer conditions this harness doesn't set up.
|
||||
html = _run_markdown_case('```mermaid\ngraph TD; A["$&"] --> B;\n```')
|
||||
|
||||
assert "___MERMAID_BLOCK_" not in html
|
||||
assert "$&" in html
|
||||
|
||||
|
||||
def test_currency_dollar_amounts_are_not_rendered_as_math(node_available):
|
||||
# "$5 to $10" used to pair the two dollar signs as inline-math delimiters
|
||||
# and render "5 to" through KaTeX. Pandoc-style rules now reject it: the
|
||||
|
||||
Reference in New Issue
Block a user