fix(desktop): keep $ patterns in headings out of the exported TOC by Dev-next-gen · Pull Request #5742 · marktext/marktext · GitHub
Skip to content

fix(desktop): keep $ patterns in headings out of the exported TOC - #5742

Merged
Jocs merged 1 commit into
marktext:developfrom
Dev-next-gen:fix/export-toc-dollar
Oct 14, 2026
Merged

Jocs merged 1 commit into
marktext:developfrom
Dev-next-gen:fix/export-toc-dollar

Conversation

@Dev-next-gen

Copy link
Copy Markdown
Contributor

Closes #5741

Summary

When a document with [TOC] is exported to styled HTML, PDF or print, exportStyledHTML splices the generated TOC into the article with article.replace(regex, toc). Because toc is a replacement string, $ patterns in heading text are expanded: ## Pay in US$$ only ends up as Pay in US$ only in the TOC, ## A$&B as A<p>[TOC]</p>amp;B, and ## Bob$'s notes pulls the whole rest of the article into the TOC link, so the body appears twice in the export.

The fix passes the TOC through a replacer function (() => toc) in both branches, the same way the <body> splice at the end of the function already does since #5701.

Type of change

  • Bug fix (non-breaking, fixes an issue)
  • New feature (non-breaking, adds functionality)
  • Breaking change (causes existing functionality to change)
  • Documentation update

Test plan

  • New tests added (or explain why not needed)
  • Manually tested on:

test/unit/specs/exportHtmlTocDollar.spec.ts boots a real Muya and runs the same steps as the editor's handleExport: getHtmlToc(muya.getTOC()), then exportStyledHTML(muya, muya.getMarkdown(), { toc }). It checks the TOC entry for headings containing $$, $', $& and $`. Before the fix all 4 cases fail with the outputs above; after it they pass, and exportHtml.spec.ts still passes (25/25 across both files). I put the test in its own file because #5732 is editing exportHtml.spec.ts.

Found by a defect-hunting pipeline I build and run (Dev-next-gen), using Claude Code with Anthropic's Claude Opus 5.5.


By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

exportStyledHTML spliced the generated TOC into the article with a string
replacement, so $$, $&, $` and $' in a heading were read as replacement
patterns: "US$$" became "US$" and "Bob$'s notes" pulled the rest of the
document into the TOC entry. Pass the TOC through a replacer function, as the
<body> splice below already does since marktext#5701.
@Jocs
Jocs merged commit 54755cc into marktext:develop Oct 14, 2026
10 checks passed
@Jocs

Jocs commented Oct 14, 2026

Copy link
Copy Markdown
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Exported TOC mangles headings that contain $$, $&, $` or $'

2 participants