Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/comark/src/internal/stringify/handlers/comment.ts
Original file line number Diff line number Diff line change
@@ -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 `<!--${node[2]}-->` + _state.context.blockSeparator
const inline =
state.context.inline === true ||
parent?.[0] === 'p' ||
(parent?.some((child, index) => index > 1 && typeof child === 'string') ?? false)
return `<!--${node[2]}-->` + (inline ? '' : state.context.blockSeparator)
}

return ''
Expand Down
2 changes: 1 addition & 1 deletion packages/comark/src/internal/stringify/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions packages/comark/test/html-roundtrip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ describe('raw HTML round-trip (parse → renderMarkdown → parse)', () => {
'object with two params':
'<object data="x.swf">\n<param name="a" value="1">\n<param name="b" value="2">\n</object>',
'nested divs': '<div>\n<div>one</div>\n<div>two</div>\n</div>',
'inline comment inside a paragraph': 'text <!-- note --> tail',
'inline comment inside a component': '::note\ntext <!-- note --> tail\n::',
'single-child video': '<video controls>\n<source src="a.mp4" type="video/mp4">\n</video>',
'mixed text and element children': '<div>\nsome text\n<figcaption>cap</figcaption>\n</div>',
'pre with authored language attribute': '<pre language="ruby">\nputs 1\n</pre>',
Expand Down
Loading