Repository navigation
fix(markdown): strip HTML comments instead of rendering them as text - #14009
Conversation
The markdown parser had no handling for `<!-- ... -->`, so comment syntax fell through to `unmatched_char` in `parse_inline_token` and to `parse_paragraph` at the block level, rendering as visible text. Add `parse_html_comment` and consume comments at both levels: block-level comments are handled in the `parse_markdown_internal` loop (they yield no `FormattedTextLine` at all) and inline comments become a discarded `InlineToken::Comment`. Code spans are ordered ahead of the inline branch so ```<!-- x -->``` still renders literally, and an unterminated `<!--` fails the parser and stays literal per CommonMark.
|
I'm starting a first review of this pull request. You can view the conversation on Warp. I completed the review and no human review was requested for this pull request. Comment Powered by Oz |
There was a problem hiding this comment.
Overview
This PR updates the shared Markdown parser to strip HTML comments at both block and inline positions, with regression coverage for inline comments, whole-line comments, multi-line comments, code spans, fenced code blocks, and unterminated comments.
Concerns
- The block-comment parser currently accepts comments that are not whole-line comments, so content after a closing
-->on the same line can be reparsed as a fresh block rather than as the continuation of the original line.
Verdict
Found: 0 critical, 1 important, 0 suggestions
Request changes
Comment /oz-review on this pull request to retrigger a review (up to 3 times on the same pull request).
Powered by Oz
| "html_comment_block", | ||
| terminated( | ||
| preceded(space0, parse_html_comment), | ||
| pair(space0, opt(parse_line_ending)), |
There was a problem hiding this comment.
opt(parse_line_ending) makes this block parser succeed even when non-whitespace content follows the comment on the same line, so <!-- hidden --> # Heading drops the comment and then reparses # Heading as a new heading/list/etc. Require a line ending or EOF after the trailing spaces here so same-line content falls through to inline parsing instead.
|
I took a stab at addressing Oz's feedback here: maxmilian#5 feel free to ignore it if you have a different approach |
Address review: opt(parse_line_ending) let the block comment parser succeed even when non-whitespace followed the closing --> on the same line, so `<!-- hidden --> # Heading` dropped the comment and then reparsed `# Heading` as a fresh heading block. Require a line ending or EOF after the trailing spaces so same-line content falls through to inline parsing (which strips the comment) instead.
|
Thanks for the catch — you're right. Fixed in c2bfb42: the trailing spaces after Added a regression test ( |
|
/oz-review |
|
I'm re-reviewing this pull request in response to a review request. You can view the conversation on Warp. I reviewed this pull request and requested human review from: Comment Powered by Oz |
Oz no longer requests changes for this pull request after the latest automated review.
There was a problem hiding this comment.
Overview
This PR updates the shared Markdown parser to strip HTML comments at both block and inline positions, while preserving comments in code spans/fenced code and rendering unterminated comment markers literally. The included regression tests cover the issue scenarios and the key preservation cases.
Concerns
- None.
Verdict
Found: 0 critical, 0 important, 0 suggestions
Approve
Comment /oz-review on this pull request to retrigger a review (up to 3 times on the same pull request).
Powered by Oz
…arpdotdev#14009) Port of upstream 222903c. Adds parse_html_comment to the shared markdown parser and consumes comments at both block and inline levels so <!-- ... --> no longer renders as visible text. Code spans and unterminated comments are preserved literally per CommonMark.
…arpdotdev#14009) ## Description The Markdown viewer had no handling for HTML comments, so `<!-- ... -->` rendered as visible text. This fixes it in the shared parser rather than the view layer, as the triage note on warpdotdev#13977 suggested. Root cause, at both levels of `crates/markdown_parser`: - **Inline** — `parse_inline_token`'s `alt` had no comment branch, so `<`, `!` and `-` fell through to `unmatched_char` (the deliberate catch-all at the end of the chain) and became `InlineToken::Text` → visible `FormattedTextFragment`s. - **Block** — `parse_markdown_internal`'s `alt` had no comment branch either, so a comment line fell through to `parse_paragraph` and rendered as an ordinary line. The fix adds `parse_html_comment` (`<!--` … `-->`, multi-line) and consumes comments at both levels: - Block-level comments are handled in the `parse_markdown_internal` loop rather than inside `alt`, because every `alt` branch must yield a `FormattedTextLine` and a comment must yield *nothing at all*. It runs before the `alt` since a comment can only begin at `<!--`, which no other block construct claims. - Inline comments become an `InlineToken::Comment` that is discarded without emitting a fragment. Two ordering decisions worth calling out, both pinned by tests: - `code_span` is ordered **ahead of** the inline comment branch, so `` `<!-- x -->` `` still renders literally. - An unterminated `<!--` makes the parser **fail** rather than consume to end-of-input, so it stays literal per CommonMark (HTML block type 2). Scope is parser-only — no view-layer changes. This is deliberately narrower than warpdotdev#13652's broader raw-HTML work and does not overlap it. ## Linked Issue - [x] The linked issue is labeled `ready-to-spec` or `ready-to-implement`. - [x] Where appropriate, screenshots or a short video of the implementation are included below. Closes warpdotdev#13977 ## Testing - [x] `cargo fmt -- --check` - [x] `cargo check -p markdown_parser` - [x] `PATH="/tmp/warp-corepack-shims:$PATH" cargo clippy -p markdown_parser --all-targets --all-features --tests -- -D warnings` - [x] `cargo nextest run --no-fail-fast -p markdown_parser` — **147 passed, 0 failed** (6 new + 141 existing regression tests) - [x] I have manually tested my changes locally with `./script/run` Six tests were added to `markdown_parser_tests.rs`, covering the five cases from the triage comment plus a code-span guard. They were written first and confirmed red before the fix: the three "should be stripped" cases failed, while the three "should be preserved" cases (unterminated, fenced code block, code span) passed from the start and now serve as regression guards. - `test_parse_html_comment_inline_is_stripped` - `test_parse_html_comment_single_line_block_is_stripped` - `test_parse_html_comment_multi_line_block_is_stripped` - `test_parse_unterminated_html_comment_stays_literal` - `test_parse_html_comment_inside_code_block_is_preserved` - `test_parse_html_comment_inside_code_span_is_preserved` ### Screenshots / Videos Captured from two local `./script/run` dev builds of the same worktree — the only difference is this PR's parser change (`crates/markdown_parser/src/markdown_parser.rs` reverted for the "before" build, restored for the "after" build). Both open the same file in the Markdown viewer at identical window geometry.  Source file used: ```markdown # HTML comment handling <!-- this whole line is a comment and should not render --> Inline <!-- hidden note --> comment inside a paragraph. <!-- a multi-line comment spanning several lines --> Code span keeps it literal: `<!-- not a comment -->` Unterminated stays literal: <!-- no closing tag here ``` **Before** — the standalone comment line, the inline comment and the whole multi-line comment all render as visible text. **After** — all three are gone, while the code span and the unterminated `<!--` are still rendered literally, which is the intended behavior. ## Agent Mode - [x] Warp Agent Mode - This PR was created via Warp's AI Agent Mode <!-- CHANGELOG-BUG-FIX --> (cherry picked from commit 222903c)
Description
The Markdown viewer had no handling for HTML comments, so
<!-- ... -->rendered as visible text. This fixes it in the shared parser rather than the view layer, as the triage note on #13977 suggested.Root cause, at both levels of
crates/markdown_parser:parse_inline_token'salthad no comment branch, so<,!and-fell through tounmatched_char(the deliberate catch-all at the end of the chain) and becameInlineToken::Text→ visibleFormattedTextFragments.parse_markdown_internal'salthad no comment branch either, so a comment line fell through toparse_paragraphand rendered as an ordinary line.The fix adds
parse_html_comment(<!--…-->, multi-line) and consumes comments at both levels:parse_markdown_internalloop rather than insidealt, because everyaltbranch must yield aFormattedTextLineand a comment must yield nothing at all. It runs before thealtsince a comment can only begin at<!--, which no other block construct claims.InlineToken::Commentthat is discarded without emitting a fragment.Two ordering decisions worth calling out, both pinned by tests:
code_spanis ordered ahead of the inline comment branch, so`<!-- x -->`still renders literally.<!--makes the parser fail rather than consume to end-of-input, so it stays literal per CommonMark (HTML block type 2).Scope is parser-only — no view-layer changes. This is deliberately narrower than #13652's broader raw-HTML work and does not overlap it.
Linked Issue
ready-to-specorready-to-implement.Closes #13977
Testing
cargo fmt -- --checkcargo check -p markdown_parserPATH="/tmp/warp-corepack-shims:$PATH" cargo clippy -p markdown_parser --all-targets --all-features --tests -- -D warningscargo nextest run --no-fail-fast -p markdown_parser— 147 passed, 0 failed (6 new + 141 existing regression tests)./script/runSix tests were added to
markdown_parser_tests.rs, covering the five cases from the triage comment plus a code-span guard. They were written first and confirmed red before the fix: the three "should be stripped" cases failed, while the three "should be preserved" cases (unterminated, fenced code block, code span) passed from the start and now serve as regression guards.test_parse_html_comment_inline_is_strippedtest_parse_html_comment_single_line_block_is_strippedtest_parse_html_comment_multi_line_block_is_strippedtest_parse_unterminated_html_comment_stays_literaltest_parse_html_comment_inside_code_block_is_preservedtest_parse_html_comment_inside_code_span_is_preservedScreenshots / Videos
Captured from two local
./script/rundev builds of the same worktree — the only difference is this PR's parser change (crates/markdown_parser/src/markdown_parser.rsreverted for the "before" build, restored for the "after" build). Both open the same file in the Markdown viewer at identical window geometry.Source file used:
Before — the standalone comment line, the inline comment and the whole multi-line comment all render as visible text.
After — all three are gone, while the code span and the unterminated
<!--are still rendered literally, which is the intended behavior.Agent Mode