From 806976b1fa5a99c0210b7a248af611c8785dd79f Mon Sep 17 00:00:00 2001 From: Baptiste Leproux Date: Thu, 19 Nov 2026 11:12:41 +0000 Subject: [PATCH] fix(render): keep blank lines around markdown inside raw html Amp-Thread-ID: https://ampcode.com/threads/T-01a1f345-60f4-70e6-bc88-ed7dbb9a9a37 --- docs/content/2.syntax/1.markdown.md | 2 +- .../SPEC/HTML/block-markdown-paragraphs.md | 68 +++++++++++++++ .../HTML/details-inside-details-no-unwrap.md | 3 + .../SPEC/HTML/details-inside-details.md | 3 + .../SPEC/HTML/details-summary-inline.md | 3 + .../HTML/details-summary-markdown-list.md | 85 +++++++++++++++++++ .../SPEC/HTML/details-summary-multiline.md | 3 + .../HTML/incomplete-block-two-new-line.md | 3 +- .../SPEC/HTML/p-details-inside-details.md | 4 +- .../src/internal/stringify/handlers/html.ts | 7 +- .../comark/test/render-markdown-html.test.ts | 17 ++++ 11 files changed, 194 insertions(+), 4 deletions(-) create mode 100644 packages/comark/SPEC/HTML/block-markdown-paragraphs.md create mode 100644 packages/comark/SPEC/HTML/details-summary-markdown-list.md diff --git a/docs/content/2.syntax/1.markdown.md b/docs/content/2.syntax/1.markdown.md index 86ab835d8..ee00259a5 100644 --- a/docs/content/2.syntax/1.markdown.md +++ b/docs/content/2.syntax/1.markdown.md @@ -463,13 +463,13 @@ Comark renders most CommonMark and GFM documents the same way as a spec-complian - **`autoClose`** is on by default, so unterminated syntax is closed: `*foo bar` renders as emphasis, and an unclosed `` ` `` becomes inline code. Set `autoClose: false` for static content that must follow the spec. See the [Streaming API](/reference/auto-close). - **`autoUnwrap`** removes the `

` inside a list item or a blockquote that holds a single paragraph. - **`headingIds`** adds an `id` to every heading. +- **Markdown inside HTML** is parsed even without blank lines around it. `*foo*` on its own line between `

` and `
` becomes emphasis, where CommonMark keeps the whole block as raw HTML. Set `markdown: false` in the [HTML plugin](/plugins/defaults/html#options) to keep that text literal. ### Document model - **Tight and loose lists** aren't distinguished. List items render the same way with or without blank lines between them. - **Empty link text** removes the link: `[](/url)` renders nothing. - **Image alt text** keeps the raw Markdown: `![foo *bar*](/img.png)` gives the alt text `foo *bar*`. -- **Markdown inside an HTML block**, such as a `
` followed by a blank line, renders after the HTML element instead of inside it. - **Fenced code blocks** add `language` and `meta` attributes to the `
` element.
 
 ### GFM extensions
diff --git a/packages/comark/SPEC/HTML/block-markdown-paragraphs.md b/packages/comark/SPEC/HTML/block-markdown-paragraphs.md
new file mode 100644
index 000000000..c19d0448d
--- /dev/null
+++ b/packages/comark/SPEC/HTML/block-markdown-paragraphs.md
@@ -0,0 +1,68 @@
+## Input
+
+```md
+
+ +First Paragraph + +Second **bold** + +
+``` + +## AST + +```json +{ + "frontmatter": {}, + "meta": {}, + "nodes": [ + [ + "div", + { + "$": { + "html": 1, + "block": 1 + }, + "class": "a" + }, + [ + "p", + {}, + "First Paragraph" + ], + [ + "p", + {}, + "Second ", + [ + "strong", + {}, + "bold" + ] + ] + ] + ] +} +``` + +## HTML + +```html +
+

First Paragraph

+

Second bold

+
+``` + +## Markdown + +```md +
+ +First Paragraph + +Second **bold** + +
+``` diff --git a/packages/comark/SPEC/HTML/details-inside-details-no-unwrap.md b/packages/comark/SPEC/HTML/details-inside-details-no-unwrap.md index 3a844b9f4..1a0895e6f 100644 --- a/packages/comark/SPEC/HTML/details-inside-details-no-unwrap.md +++ b/packages/comark/SPEC/HTML/details-inside-details-no-unwrap.md @@ -91,8 +91,11 @@ Nested content
Top
+ Nested + Nested content +
``` diff --git a/packages/comark/SPEC/HTML/details-inside-details.md b/packages/comark/SPEC/HTML/details-inside-details.md index bf0165be2..b56a77333 100644 --- a/packages/comark/SPEC/HTML/details-inside-details.md +++ b/packages/comark/SPEC/HTML/details-inside-details.md @@ -86,8 +86,11 @@ Nested content
Top
+ Nested + Nested content +
``` diff --git a/packages/comark/SPEC/HTML/details-summary-inline.md b/packages/comark/SPEC/HTML/details-summary-inline.md index eb81f469c..c2807a5b2 100644 --- a/packages/comark/SPEC/HTML/details-summary-inline.md +++ b/packages/comark/SPEC/HTML/details-summary-inline.md @@ -56,7 +56,10 @@ Explain ```md
+ Hello + Explain +
``` diff --git a/packages/comark/SPEC/HTML/details-summary-markdown-list.md b/packages/comark/SPEC/HTML/details-summary-markdown-list.md new file mode 100644 index 000000000..0fde35779 --- /dev/null +++ b/packages/comark/SPEC/HTML/details-summary-markdown-list.md @@ -0,0 +1,85 @@ +## Input + +```md +
+Sum + +- Body **b** +- two + +
+``` + +## AST + +```json +{ + "frontmatter": {}, + "meta": {}, + "nodes": [ + [ + "details", + { + "$": { + "html": 1, + "block": 1 + } + }, + [ + "summary", + { + "$": { + "html": 1, + "block": 0 + } + }, + "Sum" + ], + [ + "ul", + {}, + [ + "li", + {}, + "Body ", + [ + "strong", + {}, + "b" + ] + ], + [ + "li", + {}, + "two" + ] + ] + ] + ] +} +``` + +## HTML + +```html +
+ Sum +
    +
  • Body b
  • +
  • two
  • +
+
+``` + +## Markdown + +```md +
+ +Sum + +- Body **b** +- two + +
+``` diff --git a/packages/comark/SPEC/HTML/details-summary-multiline.md b/packages/comark/SPEC/HTML/details-summary-multiline.md index 6eec71027..6dcd134c8 100644 --- a/packages/comark/SPEC/HTML/details-summary-multiline.md +++ b/packages/comark/SPEC/HTML/details-summary-multiline.md @@ -62,9 +62,12 @@ Explain ```md
+ Hello + Explain +
``` diff --git a/packages/comark/SPEC/HTML/incomplete-block-two-new-line.md b/packages/comark/SPEC/HTML/incomplete-block-two-new-line.md index 139fb6820..5673606f7 100644 --- a/packages/comark/SPEC/HTML/incomplete-block-two-new-line.md +++ b/packages/comark/SPEC/HTML/incomplete-block-two-new-line.md @@ -73,10 +73,11 @@ ```md -**bold** and more +**bold** and more - list - **item** + ``` diff --git a/packages/comark/SPEC/HTML/p-details-inside-details.md b/packages/comark/SPEC/HTML/p-details-inside-details.md index de74b57ce..b3df72b26 100644 --- a/packages/comark/SPEC/HTML/p-details-inside-details.md +++ b/packages/comark/SPEC/HTML/p-details-inside-details.md @@ -94,11 +94,13 @@ Nested content2
Top
+ Nested -Nested content +Nested content Nested content2 +
``` diff --git a/packages/comark/src/internal/stringify/handlers/html.ts b/packages/comark/src/internal/stringify/handlers/html.ts index c7850536e..28c9a711d 100644 --- a/packages/comark/src/internal/stringify/handlers/html.ts +++ b/packages/comark/src/internal/stringify/handlers/html.ts @@ -77,7 +77,12 @@ export async function html(node: ElementNode, state: State, parent?: ElementNode (blockTags.has(String(child?.[0])) || (!inlineTags.has(String(child?.[0])) && !hasTextSibling)) // A component written as a tag needs blank lines around its markdown, or CommonMark keeps it in the HTML block. - const markdownBody = !state.context.html && !oneLiner && $.html !== 1 + // Raw HTML gets them only around markdown blocks; a blank line in a literal body would end the HTML block. + const markdownBody = + !state.context.html && + !oneLiner && + ($.html !== 1 || + children.some((child) => typeof child !== 'string' && child[1]?.$?.html !== 1 && blockTags.has(String(child[0])))) const childrenContent: string[] = [] for (let i = 0; i < children.length; i++) { diff --git a/packages/comark/test/render-markdown-html.test.ts b/packages/comark/test/render-markdown-html.test.ts index 3718ac2e6..2bb51a39a 100644 --- a/packages/comark/test/render-markdown-html.test.ts +++ b/packages/comark/test/render-markdown-html.test.ts @@ -40,4 +40,21 @@ describe('render: markdown/html', () => { '', ]) }) + + it('keeps paragraphs parseable when its own output is rendered again', async () => { + // The second pass reads `` back as raw HTML (`$.html: 1`) holding markdown paragraphs. + const once = await render(await parseMarkdown('::card\nFirst Paragraph\n\nSecond **bold**\n::'), { + format: 'markdown/html', + }) + const tokens = await reparse(once) + + expect(tokens.filter((token) => token.type === 'inline').map((token) => token.content)).toEqual([ + 'First Paragraph', + 'Second **bold**', + ]) + expect(tokens.filter((token) => token.type === 'html_block').map((token) => token.content.trim())).toEqual([ + '', + '', + ]) + }) })