diff --git a/docs/content/2.syntax/1.markdown.md b/docs/content/2.syntax/1.markdown.md index 86ab835d..ee00259a 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 `
` 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 00000000..c19d0448 --- /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 +++``` + +## Markdown + +```md +First Paragraph
+Second bold
++ +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 3a844b9f..1a0895e6 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``` diff --git a/packages/comark/SPEC/HTML/details-inside-details.md b/packages/comark/SPEC/HTML/details-inside-details.md index bf0165be..b56a7733 100644 --- a/packages/comark/SPEC/HTML/details-inside-details.md +++ b/packages/comark/SPEC/HTML/details-inside-details.md @@ -86,8 +86,11 @@ Nested contentTop
+Nested
+ Nested content +``` diff --git a/packages/comark/SPEC/HTML/details-summary-inline.md b/packages/comark/SPEC/HTML/details-summary-inline.md index eb81f469..c2807a5b 100644 --- a/packages/comark/SPEC/HTML/details-summary-inline.md +++ b/packages/comark/SPEC/HTML/details-summary-inline.md @@ -56,7 +56,10 @@ Explain ```mdTop
+Nested
+ Nested content ++``` 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 00000000..0fde3577 --- /dev/null +++ b/packages/comark/SPEC/HTML/details-summary-markdown-list.md @@ -0,0 +1,85 @@ +## Input + +```md +Hello
+ Explain +++``` + +## 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 6eec7102..6dcd134c 100644 --- a/packages/comark/SPEC/HTML/details-summary-multiline.md +++ b/packages/comark/SPEC/HTML/details-summary-multiline.md @@ -62,9 +62,12 @@ Explain ```mdSum
+ +- Body **b** +- two + ++``` 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 139fb682..5673606f 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 @@ ```mdHello
+ Explain +-**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 de74b57c..b3df72b2 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``` diff --git a/packages/comark/src/internal/stringify/handlers/html.ts b/packages/comark/src/internal/stringify/handlers/html.ts index c7850536..28c9a711 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 3718ac2e..2bb51a39 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 `Top
+Nested
-Nested content +Nested content Nested content2 +` 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([ + ' ', + ' ', + ]) + }) })