Skip to content

Self-hosted AI workspace.

Python

fix(markdown): restore extracted blocks verbatim so $& and $$ survive (#5768)

Husam578312200ac1
diff --git a/static/js/markdown.js b/static/js/markdown.js
index 8735b83e7..f249facc9 100644
--- a/static/js/markdown.js
+++ b/static/js/markdown.js
@@ -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);
});
diff --git a/tests/test_markdown_rendering_js.py b/tests/test_markdown_rendering_js.py
index 2ffe8914f..536789b89 100644
--- a/tests/test_markdown_rendering_js.py
+++ b/tests/test_markdown_rendering_js.py
@@ -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 `$&amp;`.
+ html = _run_markdown_case(
+ "```sh\necho \"hello world\" | perl -pe 's/world/$& again/'\n```"
+ )
+
+ assert "___CODE_BLOCK_" not in html
+ assert "s/world/$&amp; again/" in html
+ assert "amp; again" not in html.replace("$&amp; 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/$&#39;/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 "$&amp;" 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