From a6f0ca567547219e6004f341b19d06fd8ef350d7 Mon Sep 17 00:00:00 2001 From: 0froq Date: Tue, 20 Oct 2026 00:50:54 +0800 Subject: [PATCH 1/5] fix(stringify): keep inline HTML comments inside their paragraph Inline comments are parsed as comment nodes, but the comment handler always appended the block separator, so `Hello world` was stringified as two paragraphs. Only add the separator for top-level comments, and add a spec fixture for inline comments. Closes #466 --- packages/comark/SPEC/COMARK/comment-inline.md | 61 +++++++++++++++++++ .../internal/stringify/handlers/comment.ts | 5 +- .../comark/src/internal/stringify/state.ts | 2 +- 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 packages/comark/SPEC/COMARK/comment-inline.md diff --git a/packages/comark/SPEC/COMARK/comment-inline.md b/packages/comark/SPEC/COMARK/comment-inline.md new file mode 100644 index 000000000..4a76b9a5e --- /dev/null +++ b/packages/comark/SPEC/COMARK/comment-inline.md @@ -0,0 +1,61 @@ +## Input + +```md +Hello world + +**bold text** +``` + +## AST + +```json +{ + "frontmatter": {}, + "meta": {}, + "nodes": [ + [ + "p", + {}, + "Hello ", + [ + null, + {}, + " note " + ], + " world" + ], + [ + "p", + {}, + [ + "strong", + {}, + "bold ", + [ + null, + {}, + " hidden " + ], + " text" + ] + ] + ] +} +``` + +## HTML + +```html +

+ Hello world +

+

bold text

+``` + +## Markdown + +```md +Hello world + +**bold text** +``` diff --git a/packages/comark/src/internal/stringify/handlers/comment.ts b/packages/comark/src/internal/stringify/handlers/comment.ts index 8177dae87..7be1c1297 100644 --- a/packages/comark/src/internal/stringify/handlers/comment.ts +++ b/packages/comark/src/internal/stringify/handlers/comment.ts @@ -1,9 +1,10 @@ import type { State } from 'comark/render' import type { ElementNode } from 'comark' -export function comment(node: ElementNode, _state: State) { +export function comment(node: ElementNode, state: State, parent?: ElementNode) { if (node[0] === null) { - return `` + _state.context.blockSeparator + // Inline comments (inside a paragraph, emphasis, …) must not end the block + return `` + (parent ? '' : state.context.blockSeparator) } return '' diff --git a/packages/comark/src/internal/stringify/state.ts b/packages/comark/src/internal/stringify/state.ts index 1cad594e6..ac164d9fc 100644 --- a/packages/comark/src/internal/stringify/state.ts +++ b/packages/comark/src/internal/stringify/state.ts @@ -50,7 +50,7 @@ export async function one(node: Node, state: State, parent?: ElementNode, atLine } if (node[0] === null) { - return await state.handlers.comment(node as unknown as ElementNode, state) + return await state.handlers.comment(node as unknown as ElementNode, state, parent) } // Scope `renderData.props` to the current element's resolved attributes so From 6c474b9d580bc9c84d9419c5fc4d155c23941d42 Mon Sep 17 00:00:00 2001 From: 0froq Date: Tue, 20 Oct 2026 01:49:50 +0800 Subject: [PATCH 2/5] refactor(stringify): declare comment handler return type --- packages/comark/src/internal/stringify/handlers/comment.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/comark/src/internal/stringify/handlers/comment.ts b/packages/comark/src/internal/stringify/handlers/comment.ts index 7be1c1297..ee838dc27 100644 --- a/packages/comark/src/internal/stringify/handlers/comment.ts +++ b/packages/comark/src/internal/stringify/handlers/comment.ts @@ -1,7 +1,7 @@ import type { State } from 'comark/render' import type { ElementNode } from 'comark' -export function comment(node: ElementNode, state: State, parent?: ElementNode) { +export function comment(node: ElementNode, state: State, parent?: ElementNode): string { if (node[0] === null) { // Inline comments (inside a paragraph, emphasis, …) must not end the block return `` + (parent ? '' : state.context.blockSeparator) From 28e2c8aff35e4f1901580367254952c6672063ab Mon Sep 17 00:00:00 2001 From: 0froq Date: Tue, 20 Oct 2026 13:36:30 +0800 Subject: [PATCH 3/5] fix(stringify): keep block comments separated inside containers --- .../comark/SPEC/COMARK/comment-blockquote.md | 50 +++++++++++++++++++ .../internal/stringify/handlers/comment.ts | 30 ++++++++++- 2 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 packages/comark/SPEC/COMARK/comment-blockquote.md diff --git a/packages/comark/SPEC/COMARK/comment-blockquote.md b/packages/comark/SPEC/COMARK/comment-blockquote.md new file mode 100644 index 000000000..f36ab1227 --- /dev/null +++ b/packages/comark/SPEC/COMARK/comment-blockquote.md @@ -0,0 +1,50 @@ +## Input + +```md +> +> +> foo +``` + +## AST + +```json +{ + "frontmatter": {}, + "meta": {}, + "nodes": [ + [ + "blockquote", + {}, + [ + null, + {}, + " note " + ], + [ + "p", + {}, + "foo" + ] + ] + ] +} +``` + +## HTML + +```html +
+ + +

foo

+
+``` + +## Markdown + +```md +> +> +> foo +``` diff --git a/packages/comark/src/internal/stringify/handlers/comment.ts b/packages/comark/src/internal/stringify/handlers/comment.ts index ee838dc27..a48bfbc28 100644 --- a/packages/comark/src/internal/stringify/handlers/comment.ts +++ b/packages/comark/src/internal/stringify/handlers/comment.ts @@ -1,10 +1,36 @@ import type { State } from 'comark/render' import type { ElementNode } from 'comark' +// Parents whose children are phrasing content: a comment inside them is inline +const PHRASING_PARENTS = new Set([ + 'p', + 'h1', + 'h2', + 'h3', + 'h4', + 'h5', + 'h6', + 'strong', + 'em', + 'del', + 'a', + 'span', + 'sub', + 'sup', + 'kbd', + 'code', + 'td', + 'th', +]) + export function comment(node: ElementNode, state: State, parent?: ElementNode): string { if (node[0] === null) { - // Inline comments (inside a paragraph, emphasis, …) must not end the block - return `` + (parent ? '' : state.context.blockSeparator) + // Inline comments (inside a paragraph, emphasis, …) must not end the block. + // Block comments nested in a container (blockquote, list item, …) still do. + const isInline = + parent !== undefined && + (PHRASING_PARENTS.has(String(parent[0])) || parent.some((child, index) => index > 1 && typeof child === 'string')) + return `` + (isInline ? '' : state.context.blockSeparator) } return '' From ecf44143564eb46fa9b7114182e1b0ab8cf1aad4 Mon Sep 17 00:00:00 2001 From: 0froq Date: Tue, 20 Oct 2026 13:55:43 +0800 Subject: [PATCH 4/5] fix(stringify): treat comments in inline HTML elements as inline --- .../SPEC/COMARK/comment-inline-element.md | 50 +++++++++++++++++++ .../internal/stringify/handlers/comment.ts | 4 +- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 packages/comark/SPEC/COMARK/comment-inline-element.md diff --git a/packages/comark/SPEC/COMARK/comment-inline-element.md b/packages/comark/SPEC/COMARK/comment-inline-element.md new file mode 100644 index 000000000..bb718ca97 --- /dev/null +++ b/packages/comark/SPEC/COMARK/comment-inline-element.md @@ -0,0 +1,50 @@ +## Input + +```md +Hello world +``` + +## AST + +```json +{ + "frontmatter": {}, + "meta": {}, + "nodes": [ + [ + "p", + {}, + "Hello ", + [ + "mark", + { + "$": { + "html": 1, + "block": 0 + } + }, + [ + null, + {}, + " note " + ] + ], + " world" + ] + ] +} +``` + +## HTML + +```html +

+ Hello world +

+``` + +## Markdown + +```md +Hello world +``` diff --git a/packages/comark/src/internal/stringify/handlers/comment.ts b/packages/comark/src/internal/stringify/handlers/comment.ts index a48bfbc28..e898c7204 100644 --- a/packages/comark/src/internal/stringify/handlers/comment.ts +++ b/packages/comark/src/internal/stringify/handlers/comment.ts @@ -29,7 +29,9 @@ export function comment(node: ElementNode, state: State, parent?: ElementNode): // Block comments nested in a container (blockquote, list item, …) still do. const isInline = parent !== undefined && - (PHRASING_PARENTS.has(String(parent[0])) || parent.some((child, index) => index > 1 && typeof child === 'string')) + (PHRASING_PARENTS.has(String(parent[0])) || + (parent[1] as { $?: { block?: number } })?.$?.block === 0 || + parent.some((child, index) => index > 1 && typeof child === 'string')) return `` + (isInline ? '' : state.context.blockSeparator) } From b822cf00cb2674ed00483885ac8b5172a1869b89 Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Mon, 9 Nov 2026 16:04:09 +0100 Subject: [PATCH 5/5] test(stringify): keep inline comment fixtures, drop handler change already on main The inline HTML comment fix is already on `main`, so this reverts `packages/comark/src/internal/stringify/handlers/comment.ts` to match `main`. The PR now contains only the spec fixtures `comment-inline.md`, `comment-blockquote.md` and `comment-inline-element.md`. All three pass against main's handler. --- .../internal/stringify/handlers/comment.ts | 30 ++----------------- 1 file changed, 2 insertions(+), 28 deletions(-) diff --git a/packages/comark/src/internal/stringify/handlers/comment.ts b/packages/comark/src/internal/stringify/handlers/comment.ts index 634cb7cd8..4c999b219 100644 --- a/packages/comark/src/internal/stringify/handlers/comment.ts +++ b/packages/comark/src/internal/stringify/handlers/comment.ts @@ -1,38 +1,12 @@ import type { State } from 'comark/render' import type { ElementNode } from 'comark' -// Parents whose children are phrasing content: a comment inside them is inline. -const PHRASING_PARENTS = new Set([ - 'p', - 'h1', - 'h2', - 'h3', - 'h4', - 'h5', - 'h6', - 'strong', - 'em', - 'del', - 'a', - 'span', - 'sub', - 'sup', - 'kbd', - 'code', - 'td', - 'th', -]) - export function comment(node: ElementNode, state: State, parent?: ElementNode) { if (node[0] === null) { - // Inline comments (phrasing parents, inline HTML, or an inline render - // context) must not end the block. Block comments in a container still do. const inline = state.context.inline === true || - (parent !== undefined && - (PHRASING_PARENTS.has(String(parent[0])) || - parent[1]?.$?.block === 0 || - parent.some((child, index) => index > 1 && typeof child === 'string'))) + parent?.[0] === 'p' || + (parent?.some((child, index) => index > 1 && typeof child === 'string') ?? false) return `` + (inline ? '' : state.context.blockSeparator) }