From 3b480e9ac4469a9b78ddaf5565b0d13ef40fad78 Mon Sep 17 00:00:00 2001 From: onmax Date: Thu, 5 Nov 2026 20:32:50 +0100 Subject: [PATCH] fix(parse): preserve block HTML order and slot boundaries in components --- .../HTML/block+component-ordered-blocks.md | 84 +++++++++++++++++++ .../SPEC/HTML/incomplete-before-slot.md | 70 ++++++++++++++++ packages/comark/src/internal/parse/tree.ts | 6 +- .../src/internal/stringify/handlers/html.ts | 5 +- 4 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 packages/comark/SPEC/HTML/block+component-ordered-blocks.md create mode 100644 packages/comark/SPEC/HTML/incomplete-before-slot.md diff --git a/packages/comark/SPEC/HTML/block+component-ordered-blocks.md b/packages/comark/SPEC/HTML/block+component-ordered-blocks.md new file mode 100644 index 000000000..078c5df48 --- /dev/null +++ b/packages/comark/SPEC/HTML/block+component-ordered-blocks.md @@ -0,0 +1,84 @@ +## Input + +```md +::foo +
+ +para + +# Title + +more + +
+:: +``` + +## AST + +```json +{ + "frontmatter": {}, + "meta": {}, + "nodes": [ + [ + "foo", + {}, + [ + "div", + { + "$": { + "html": 1, + "block": 1 + } + }, + [ + "p", + {}, + "para" + ], + [ + "h1", + { + "id": "title" + }, + "Title" + ], + [ + "p", + {}, + "more" + ] + ] + ] + ] +} +``` + +## HTML + +```html + +
+

para

+

Title

+

more

+
+
+``` + +## Markdown + +```md +::foo +
+para + + +# Title + + +more +
+:: +``` diff --git a/packages/comark/SPEC/HTML/incomplete-before-slot.md b/packages/comark/SPEC/HTML/incomplete-before-slot.md new file mode 100644 index 000000000..e29429701 --- /dev/null +++ b/packages/comark/SPEC/HTML/incomplete-before-slot.md @@ -0,0 +1,70 @@ +## Input + +```md +::foo +
+ +default + +#one +named +:: +``` + +## AST + +```json +{ + "frontmatter": {}, + "meta": {}, + "nodes": [ + [ + "foo", + {}, + [ + "div", + { + "$": { + "html": 1, + "block": 1 + } + }, + "default" + ], + [ + "template", + { + "name": "one" + }, + "named" + ] + ] + ] +} +``` + +## HTML + +```html + +
+ default +
+ +
+``` + +## Markdown + +```md +::foo +
+default +
+ +#one +named +:: +``` diff --git a/packages/comark/src/internal/parse/tree.ts b/packages/comark/src/internal/parse/tree.ts index 06c813fd7..dd8859970 100644 --- a/packages/comark/src/internal/parse/tree.ts +++ b/packages/comark/src/internal/parse/tree.ts @@ -310,7 +310,11 @@ function processBlockChildrenWithSlots( const result = processToken(tokens, i, state) if (result) { - pushChild(result.node) + for (const frame of state.htmlStack) frame.block = true + if (result.node !== undefined) { + const free = result.siblingOfHtmlStack ? result.node : deliverBlock(state, result.node) + pushChild(free) + } i = result.nextIndex } else { i += 1 diff --git a/packages/comark/src/internal/stringify/handlers/html.ts b/packages/comark/src/internal/stringify/handlers/html.ts index 0a7dc61ae..43bb9b48b 100644 --- a/packages/comark/src/internal/stringify/handlers/html.ts +++ b/packages/comark/src/internal/stringify/handlers/html.ts @@ -113,7 +113,10 @@ export async function html(node: ElementNode, state: State, parent?: ElementNode content = '\n' + paddNoneHtmlContent(content, state, String(tag)).trimEnd() + '\n' } - return `<${tag}${attrs}>${content}` + (!parent && !isInline ? state.context.blockSeparator : '') + // Components and slots need a separator between block HTML and the next block. + const endsBlock = !parent || (!oneLiner && !state.context.html && parent[1].$?.html !== 1) + + return `<${tag}${attrs}>${content}` + (endsBlock && !isInline ? state.context.blockSeparator : '') } // Literal-content tags whose body must be rendered verbatim (no indentation