From 477b7c3524e14c1799b97cd43ed31750cfbfa5bf Mon Sep 17 00:00:00 2001 From: Sarath Francis Date: Thu, 11 Jun 2026 23:49:43 -0400 Subject: [PATCH] fix: keep trailing text on HTML block close line for PI, declarations, and CDATA CommonMark ends HTML blocks of types 3 (), 4 (), and 5 () 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 '*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. --- src/rules.ts | 6 +++--- test/specs/new/html_block_trailing_text.html | 6 ++++++ test/specs/new/html_block_trailing_text.md | 11 +++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 test/specs/new/html_block_trailing_text.html create mode 100644 test/specs/new/html_block_trailing_text.md diff --git a/src/rules.ts b/src/rules.ts index cc2e62bccf..db8ebaba66 100644 --- a/src/rules.ts +++ b/src/rules.ts @@ -151,9 +151,9 @@ const html = edit( '^ {0,3}(?:' // optional indentation + '<(script|pre|style|textarea)[\\s>][\\s\\S]*?(?:[^\\n]*\\n+|$)' // (1) + '|comment[^\\n]*(\\n+|$)' // (2) -+ '|<\\?[\\s\\S]*?(?:\\?>\\n*|$)' // (3) -+ '|\\n*|$)' // (4) -+ '|\\n*|$)' // (5) ++ '|<\\?[\\s\\S]*?(?:\\?>[^\\n]*\\n+|$)' // (3) ++ '|[^\\n]*\\n+|$)' // (4) ++ '|[^\\n]*\\n+|$)' // (5) + '|)[\\s\\S]*?(?:(?:\\n[ \t]*)+\\n|$)' // (6) + '|<(?!script|pre|style|textarea)([a-z][\\w-]*)(?:attribute)*? */?>(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n[ \t]*)+\\n|$)' // (7) open tag + '|(?=[ \\t]*(?:\\n|$))[\\s\\S]*?(?:(?:\\n[ \t]*)+\\n|$)' // (7) closing tag diff --git a/test/specs/new/html_block_trailing_text.html b/test/specs/new/html_block_trailing_text.html new file mode 100644 index 0000000000..f05e542c94 --- /dev/null +++ b/test/specs/new/html_block_trailing_text.html @@ -0,0 +1,6 @@ +'; ?>*not emphasis* +

emphasis

+*not emphasis* +

emphasis

+*not emphasis* +

emphasis

diff --git a/test/specs/new/html_block_trailing_text.md b/test/specs/new/html_block_trailing_text.md new file mode 100644 index 0000000000..e2a22a36fc --- /dev/null +++ b/test/specs/new/html_block_trailing_text.md @@ -0,0 +1,11 @@ +--- +gfm: false +--- +'; ?>*not emphasis* +*emphasis* + +*not emphasis* +*emphasis* + +*not emphasis* +*emphasis*