From 0204e8767c426c8ccc69920a1c4a2d374082e81a Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sun, 1 Nov 2026 14:05:34 +0000 Subject: [PATCH] fix(render): don't end a paragraph after an inline comment --- .../comark/src/internal/stringify/handlers/comment.ts | 8 ++++++-- packages/comark/src/internal/stringify/state.ts | 2 +- packages/comark/test/html-roundtrip.test.ts | 2 ++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/comark/src/internal/stringify/handlers/comment.ts b/packages/comark/src/internal/stringify/handlers/comment.ts index 8177dae87..8876cb257 100644 --- a/packages/comark/src/internal/stringify/handlers/comment.ts +++ b/packages/comark/src/internal/stringify/handlers/comment.ts @@ -1,9 +1,13 @@ 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 + const inline = + state.context.inline === true || + parent?.[0] === 'p' || + (parent?.some((child, index) => index > 1 && typeof child === 'string') ?? false) + return `` + (inline ? '' : state.context.blockSeparator) } return '' diff --git a/packages/comark/src/internal/stringify/state.ts b/packages/comark/src/internal/stringify/state.ts index 392d399f9..f5d82e651 100644 --- a/packages/comark/src/internal/stringify/state.ts +++ b/packages/comark/src/internal/stringify/state.ts @@ -49,7 +49,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 diff --git a/packages/comark/test/html-roundtrip.test.ts b/packages/comark/test/html-roundtrip.test.ts index 47dae02c3..b8a6d1ee2 100644 --- a/packages/comark/test/html-roundtrip.test.ts +++ b/packages/comark/test/html-roundtrip.test.ts @@ -26,6 +26,8 @@ describe('raw HTML round-trip (parse → renderMarkdown → parse)', () => { 'object with two params': '\n\n\n', 'nested divs': '
\n
one
\n
two
\n
', + 'inline comment inside a paragraph': 'text tail', + 'inline comment inside a component': '::note\ntext tail\n::', 'single-child video': '', 'mixed text and element children': '
\nsome text\n
cap
\n
', 'pre with authored language attribute': '
\nputs 1\n
',