Skip to content

fix: keep trailing text on HTML block close line for PI, declarations, and CDATA#3991

Open
sarathfrancis90 wants to merge 1 commit into
markedjs:masterfrom
sarathfrancis90:fix-html-block-trailing-text
Open

fix: keep trailing text on HTML block close line for PI, declarations, and CDATA#3991
sarathfrancis90 wants to merge 1 commit into
markedjs:masterfrom
sarathfrancis90:fix-html-block-trailing-text

Conversation

@sarathfrancis90

Copy link
Copy Markdown
Contributor

Description

While checking marked against the CommonMark spec I noticed that text following the closing delimiter of an HTML block on the same line is dropped from the block for type 3 (<?...?>), type 4 (<!...>), and type 5 (<![CDATA[...]]>).

CommonMark ends these blocks on the line containing the closing delimiter, and the entire line — including anything after the delimiter — is part of the raw HTML block. marked already handles this for type 2 comments (spec example 177 shows <!-- foo -->*bar* keeping *bar* as raw text), but rules 3–5 stopped right at the delimiter, so the trailing text got split off and reparsed.

For example:

<?php echo '>'; ?>*not emphasis*

rendered as:

<?php echo '>'; ?><p><em>not emphasis</em></p>

instead of leaving *not emphasis* inside the HTML block like the reference does.

Fix

The comment rule (2) already consumes the rest of the closing line with a trailing [^\n]*. I applied the same to rules 3, 4, and 5 so they consume to the end of the closing line before the newline.

Tests

Added test/specs/new/html_block_trailing_text.md covering all three block types. It fails on master and passes with this change. Full CommonMark/GFM spec suites, unit tests, lint, types, and the ReDoS check are all green, and the CommonMark conformance score is unchanged.

Contributor checklist

  • Tests pass (npm test)
  • Lint passes (npm run lint)
  • Added a test for the fixed behavior

…, and CDATA

CommonMark ends HTML blocks of types 3 (<?...?>), 4 (<!...>), and 5
(<![CDATA[...]]>) on the line that contains the closing delimiter, and
the whole of that line - including any text after the delimiter - is part
of the block (see spec example 177 for the type 2 comment equivalent).

marked already does this for comments via the trailing [^\n]* in rule 2,
but rules 3-5 stopped at the closing delimiter, so trailing text on the
same line was split off and reparsed as a paragraph. For example
'<?php ?>*x*' rendered the '*x*' as emphasis instead of leaving it inside
the raw HTML block.

Match the comment rule by consuming the rest of the closing line before
the newline. Added a new spec test covering all three block types.
@vercel

vercel Bot commented Jun 12, 2026

Copy link
Copy Markdown

@sarathfrancis90 is attempting to deploy a commit to the MarkedJS Team on Vercel.

A member of the Team first needs to authorize it.

@vercel

vercel Bot commented Jun 12, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marked-website Ready Ready Preview, Comment Jun 12, 2026 2:36pm

Request Review

@UziTech UziTech left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice catch! Thanks for fixing this 💯

@UziTech UziTech requested review from calculuschild and styfle June 16, 2026 06:20
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.

2 participants