Skip to content

fix(markdown): strip HTML comments instead of rendering them as text - #14009

Merged
kevinyang372 merged 2 commits into
warpdotdev:masterfrom
maxmilian:fix/gh13977-strip-html-comments
Jul 28, 2026
Merged

kevinyang372 merged 2 commits into
warpdotdev:masterfrom
maxmilian:fix/gh13977-strip-html-comments

Conversation

@maxmilian

@maxmilian maxmilian commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • 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 FormattedTextFragments.
  • 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 #13652's broader raw-HTML work and does not overlap it.

Linked Issue

  • The linked issue is labeled ready-to-spec or ready-to-implement.
  • Where appropriate, screenshots or a short video of the implementation are included below.

Closes #13977

Testing

  • cargo fmt -- --check
  • cargo check -p markdown_parser
  • PATH="/tmp/warp-corepack-shims:$PATH" cargo clippy -p markdown_parser --all-targets --all-features --tests -- -D warnings
  • cargo nextest run --no-fail-fast -p markdown_parser — 147 passed, 0 failed (6 new + 141 existing regression tests)
  • 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.

Before and after: HTML comments in the Markdown viewer

Source file used:

# 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

  • Warp Agent Mode - This PR was created via Warp's AI Agent Mode

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.
@cla-bot cla-bot Bot added the cla-signed label Jul 20, 2026
@github-actions github-actions Bot added the external-contributor Indicates that a PR has been opened by someone outside the Warp team. label Jul 20, 2026
@maxmilian
maxmilian marked this pull request as ready for review July 20, 2026 15:15
@warp-for-oss

warp-for-oss Bot commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

@maxmilian

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 /oz-review on this pull request to retrigger a review (up to 3 times on the same pull request).

Powered by Oz

warp-for-oss[bot]
warp-for-oss Bot previously requested changes Jul 20, 2026

@warp-for-oss warp-for-oss Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ [IMPORTANT] 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.

@fbartho

fbartho commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

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.
@maxmilian

Copy link
Copy Markdown
Contributor Author

Thanks for the catch — you're right. opt(parse_line_ending) let the block-comment branch 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.

Fixed in c2bfb42: the trailing spaces after --> must now be terminated by a line ending or EOF (alt((value((), parse_line_ending), value((), eof)))). When other content follows on the same line the block parser fails, so the line falls through to inline parsing, where the comment is stripped inline and the remainder stays literal text.

Added a regression test (test_parse_html_comment_with_trailing_same_line_content_is_not_block): <!-- hidden --> # Heading now yields a single Line([" # Heading"]) instead of a Heading. Full suite green (148/148), fmt/clippy clean.

@maxmilian

Copy link
Copy Markdown
Contributor Author

/oz-review

@warp-for-oss

warp-for-oss Bot commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

@maxmilian

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: @kevinyang372.

Comment /oz-review on this pull request to retrigger a review (up to 3 times on the same pull request).

Powered by Oz

@warp-for-oss
warp-for-oss Bot dismissed their stale review July 24, 2026 01:48

Oz no longer requests changes for this pull request after the latest automated review.

@warp-for-oss warp-for-oss Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@warp-for-oss
warp-for-oss Bot requested a review from kevinyang372 July 24, 2026 01:48
@kevinyang372
kevinyang372 enabled auto-merge (squash) July 28, 2026 22:32
@kevinyang372
kevinyang372 merged commit 222903c into warpdotdev:master Jul 28, 2026
24 checks passed
zhangyu1818 added a commit to zhangyu1818/warply that referenced this pull request Aug 3, 2026
…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.
TranscriptionFactory pushed a commit to TranscriptionFactory/warp that referenced this pull request Aug 30, 2026
…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.

![Before and after: HTML comments in the Markdown
viewer](https://raw.githubusercontent.com/maxmilian/warp/gh13977-assets/gh13977-compare.png)

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed external-contributor Indicates that a PR has been opened by someone outside the Warp team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Markdown viewer: strip HTML comments instead of rendering them as text

3 participants