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
57 changes: 26 additions & 31 deletions packages/comark/src/internal/parse/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,6 @@ interface ProcessState {
startLine: number
/** Unclosed HTML open tags, outer → inner. Lives across blocks like the heading id stack. */
htmlStack: HtmlOpenFrame[]
/**
* > 0 while building a nested markdown container (list, blockquote, table…).
* Free paragraphs inside those containers keep ownership of their children
* instead of nesting into the HTML stack; the finished container lands on the
* stack as a whole unit via deliverBlock.
*/
insideMarkdownContainer: number
}

type Processor = (tokens: Token[], start: number, state: ProcessState) => ProcessorResult
Expand Down Expand Up @@ -173,10 +166,12 @@ function processToken(tokens: Token[], i: number, state: ProcessState): Processo
* @param nestHtml When true (free paragraph / top-level), free nodes nest into
* the open HTML stack. When false (nested markdown containers like strong/li),
* free nodes return to the parent container — the container itself later lands
* on the HTML stack as a whole unit via deliverBlock.
* on the HTML stack as a whole unit via deliverBlock. Tags opened within
* the run always receive the nodes up to their close tag.
*/
function processInline(inlineTokens: Token[], state: ProcessState, nestHtml = true): Node[] {
const nodes: Node[] = []
const depth = state.htmlStack.length
let i = 0

while (i < inlineTokens.length) {
Expand All @@ -185,7 +180,7 @@ function processInline(inlineTokens: Token[], state: ProcessState, nestHtml = tr
i += 1
continue
}
const node = nestHtml ? deliverInline(state, result.node) : result.node
const node = nestHtml || state.htmlStack.length > depth ? deliverInline(state, result.node) : result.node
if (node !== undefined) pushNode(nodes, node)
i = result.nextIndex
}
Expand Down Expand Up @@ -217,11 +212,13 @@ function processChildren(
nestHtml = false
): { children: Node[]; nextIndex: number } {
const children: Node[] = []
const depth = state.htmlStack.length
let i = start + 1

while (i < tokens.length) {
const token = tokens[i]
if (token.type === closeType) {
if (!nestHtml) closeHtmlFrames(state, children, depth, false)
return { children, nextIndex: i + 1 }
}

Expand All @@ -235,7 +232,7 @@ function processChildren(
if (result) {
// Same nesting rule as processInline: free nodes join the open HTML
// frame when nestHtml is on (e.g. block children under bare HTML).
const node = nestHtml ? deliverInline(state, result.node) : result.node
const node = nestHtml || state.htmlStack.length > depth ? deliverInline(state, result.node) : result.node
if (node !== undefined) pushNode(children, node)
i = result.nextIndex
} else {
Expand Down Expand Up @@ -267,6 +264,7 @@ function processBlockChildrenWithSlots(
let currentSlot: { tag: string; attrs: Record<string, unknown>; children: Node[] } | null = null

const flushSlot = () => {
closeHtmlFrames(state, currentSlot ? currentSlot.children : nodes)
if (!currentSlot) return
nodes.push([currentSlot.tag, currentSlot.attrs, ...mergeAdjacentTextNodes(currentSlot.children)] as ElementNode)
currentSlot = null
Expand Down Expand Up @@ -327,9 +325,9 @@ function processMdcBlock(tokens: Token[], start: number, state: ProcessState): P
const open = tokens[start]
// Own child paragraphs/inlines so an outer unclosed HTML tag (e.g. `<Hello>`)
// doesn't steal `::component` body content off the stack.
state.insideMarkdownContainer += 1
const { children, nextIndex } = processBlockChildrenWithSlots(tokens, start, 'mdc_block_close', state)
state.insideMarkdownContainer -= 1
const { children, nextIndex } = withHtmlScope(state, () =>
processBlockChildrenWithSlots(tokens, start, 'mdc_block_close', state)
)
const attrs = processAttributes(open.attrs)

let node = [open.tag || 'div', attrs, ...mergeAdjacentTextNodes(children)] as ElementNode
Expand Down Expand Up @@ -389,22 +387,24 @@ function withHtmlScope(
fn: () => { children: Node[]; nextIndex: number }
): { children: Node[]; nextIndex: number } {
const outerStack = state.htmlStack
const outerContainer = state.insideMarkdownContainer
state.htmlStack = []
state.insideMarkdownContainer = 0
try {
const result = fn()
while (state.htmlStack.length > 0) {
const frame = state.htmlStack.pop()!
frame.block = true
const node = frameToNode(frame)
if (state.htmlStack.length > 0) state.htmlStack[state.htmlStack.length - 1].children.push(node)
else result.children.push(node)
}
closeHtmlFrames(state, result.children)
return result
} finally {
state.htmlStack = outerStack
state.insideMarkdownContainer = outerContainer
}
}

/** Close HTML frames above `depth`, innermost into its parent, outermost into `children`. */
function closeHtmlFrames(state: ProcessState, children: Node[], depth = 0, block = true) {
while (state.htmlStack.length > depth) {
const frame = state.htmlStack.pop()!
if (block) frame.block = true
const node = frameToNode(frame)
if (state.htmlStack.length > depth) state.htmlStack[state.htmlStack.length - 1].children.push(node)
else children.push(node)
}
}

Expand Down Expand Up @@ -482,8 +482,7 @@ const processors: Record<string, Processor> = {
paragraph_open(tokens, start, state) {
const inline = tokens[start + 1]
const depthBefore = state.htmlStack.length
const nestHtml = state.insideMarkdownContainer === 0
const { children, nextIndex } = processChildren(tokens, start, 'paragraph_close', state, nestHtml)
const { children, nextIndex } = processChildren(tokens, start, 'paragraph_close', state, true)
const asParagraph = (): ProcessorResult =>
children.length === 0
? { nextIndex, node: undefined }
Expand Down Expand Up @@ -512,11 +511,8 @@ const processors: Record<string, Processor> = {
// frame is open, and must emit as a sibling of that frame (parse5-style),
// not nest into it.
if (state.htmlStack.length > 0) {
if (nestHtml) {
for (const frame of state.htmlStack) flushPendingInline(frame)
return freeSiblings(children.length > 0)
}
return asParagraph()
for (const frame of state.htmlStack) flushPendingInline(frame)
return freeSiblings(children.length > 0)
}

const empty = asParagraph()
Expand Down Expand Up @@ -645,7 +641,6 @@ export function tokenListToTree(tokens: Token[], options: ProcessorOptions = {})
headingIds: createHeadingIdTracker(options.headingIds ?? true),
startLine: options.startLine ?? 0,
htmlStack: [],
insideMarkdownContainer: 0,
}
const nodes: Node[] = []
let i = 0
Expand Down
63 changes: 63 additions & 0 deletions packages/comark/test/html-block.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,69 @@ describe('HTML inside markdown containers', () => {

expect(result.nodes).toEqual([['ul', {}, ['li', {}, ['span', { $: { html: 1, block: 1 } }, 'a']], ['li', {}, 'b']]])
})

it('keeps inline HTML content inside a component', async () => {
const result = await parseMarkdown('::warning\nPress <kbd>Ctrl</kbd>+<kbd>S</kbd> to save.\n::')

expect(result.nodes).toEqual([
[
'warning',
{},
'Press ',
['kbd', { $: { html: 1, block: 0 } }, 'Ctrl'],
'+',
['kbd', { $: { html: 1, block: 0 } }, 'S'],
' to save.',
],
])
})

it('keeps inline HTML content inside emphasis', async () => {
const result = await parseMarkdown('Press **<kbd>Ctrl</kbd>** to copy.')

expect(result.nodes).toEqual([
['p', {}, 'Press ', ['strong', {}, ['kbd', { $: { html: 1, block: 0 } }, 'Ctrl']], ' to copy.'],
])
})

it('keeps inline HTML content inside a heading', async () => {
const result = await parseMarkdown('# Press <kbd>Ctrl</kbd> now')

expect(result.nodes).toEqual([
['h1', { id: 'press-ctrl-now' }, 'Press ', ['kbd', { $: { html: 1, block: 0 } }, 'Ctrl'], ' now'],
])
})

it('closes an unclosed inline tag at the end of its component', async () => {
const result = await parseMarkdown('::warning\nPress <kbd>Ctrl to save.\n::\n\nafter')

expect(result.nodes).toEqual([
['warning', {}, 'Press ', ['kbd', { $: { html: 1, block: 1 } }, 'Ctrl to save.']],
['p', {}, 'after'],
])
})

it('closes an unclosed inline tag at the end of its slot', async () => {
const result = await parseMarkdown('::card\n#title\nPress <kbd>Ctrl\n#default\nbody\n::')

expect(result.nodes).toEqual([
[
'card',
{},
['template', { name: 'title' }, 'Press ', ['kbd', { $: { html: 1, block: 1 } }, 'Ctrl']],
['template', { name: 'default' }, 'body'],
],
])
})

it('closes an unclosed inline tag at the end of its emphasis', async () => {
const result = await parseMarkdown('**a <kbd>b** c\n\nnext')

expect(result.nodes).toEqual([
['p', {}, ['strong', {}, 'a ', ['kbd', { $: { html: 1, block: 0 } }, 'b']], ' c'],
['p', {}, 'next'],
])
})
})

describe('stray HTML close tags', () => {
Expand Down
Loading