fix(markdown): preserve HTML comments as text during markdown parsing#7722
fix(markdown): preserve HTML comments as text during markdown parsing#7722weilinzung wants to merge 2 commits intoueberdosis:mainfrom
Conversation
🦋 Changeset detectedLatest commit: da5a9be The changes in this PR will be included in the next version bump. This PR includes changesets to release 72 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
✅ Deploy Preview for tiptap-embed ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this comment.
Pull request overview
Fixes @tiptap/markdown parsing so HTML comments (<!-- ... -->) are preserved (as text) instead of being dropped by the browser DOM parsing step, improving markdown round-tripping and preventing data loss for comment-based metadata.
Changes:
- Intercepts HTML comment tokens in
MarkdownManager.parseHTMLTokenand converts them into text (paragraph-wrapped for block tokens). - Adds unit tests covering block, inline, multiline, and whitespace-preserving comment scenarios.
- Updates the Markdown parse demo content and adds a changeset for a patch release.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| packages/markdown/src/MarkdownManager.ts | Adds HTML-comment detection and a text-node fallback to preserve comment content during parse. |
| packages/markdown/tests/mixed-html.spec.ts | Adds regression tests for comment preservation in different positions/forms. |
| demos/src/Markdown/Parse/React/index.jsx | Extends the demo markdown sample with HTML and “Hidden HTML Comments” examples. |
| .changeset/calm-cycles-hear.md | Declares a patch release for preserving HTML comments during markdown parsing. |
|
@bdbch, I would appreciate it if you could review this. This is about our AI usage: we are feeding it MD content with hidden comments to replace. |
Changes Overview
HTML comments (
<!-- ... -->) passed througheditor.markdown.parse()were silently dropped because the browser DOM parser strips comment nodes beforegenerateJSONprocesses them. This fix preserves them as plain text so comment content is not lost.It should be similar to match markedJs:
Implementation Approach
Added a regex check in
parseHTMLTokenthat intercepts comment tokens before they reach the DOM parser, returning them as plain text nodes instead:Block comments are wrapped in a paragraph; inline comments are returned as bare text nodes.
Testing Done
Added 4 unit tests in
mixed-html.spec.ts:Verification Steps
pnpm dev → open http://localhost:3000/Markdown/Parse/React
Click Parse Markdown — the Hidden HTML Comments section should render the comment text visibly in the editor
Run
pnpm -w -F @tiptap/markdown test mixed-htmlAdditional Notes
Before
After
Checklist
Related Issues
Fixes #7720