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
6 changes: 5 additions & 1 deletion packages/comark/src/internal/parse/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,11 @@ const processors: Record<string, Processor> = {
if (frameTag !== null && frameTag.toLowerCase() === closeTag) break
idx--
}
if (idx < 0) return { nextIndex: start + 1, node: undefined }
if (idx < 0) {
// HTML parsers read a stray `</br>` as `<br>`
const node: Node | undefined = closeTag === 'br' ? ['br', makeHtmlAttrs({}, false)] : undefined
return { nextIndex: start + 1, node }
}

let closed: Node | undefined
while (state.htmlStack.length > idx) {
Expand Down
47 changes: 47 additions & 0 deletions packages/comark/test/html-block.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,4 +474,51 @@ describe('stray HTML close tags', () => {

expect(result.nodes).toEqual([['p', {}, ' orphan']])
})

it('reads a stray `</br>` as a line break', async () => {
const result = await parseMarkdown('**Experimental feature**</br>\nEnable View Transitions.')

expect(result.nodes).toEqual([
[
'p',
{},
['strong', {}, 'Experimental feature'],
['br', { $: { html: 1, block: 0 } }],
'Enable View Transitions.',
],
])
})

it('reads a lone `</br>` as a block `<br>`', async () => {
const result = await parseMarkdown('</br>')

expect(result.nodes).toEqual([['br', { $: { html: 1, block: 1 } }]])
expect(result.nodes).toEqual((await parseMarkdown('<br>')).nodes)
})

it('reads a `</br>` line between paragraphs as a block `<br>`', async () => {
const result = await parseMarkdown('before\n\n</br>\n\nafter')

expect(result.nodes).toEqual([
['p', {}, 'before'],
['br', { $: { html: 1, block: 1 } }],
['p', {}, 'after'],
])
})

it('reads a lone `</br>` as a block `<br>` when markdown: false', async () => {
const result = await parseMarkdown('</br>', {
plugins: [html({ markdown: false })],
})

expect(result.nodes).toEqual([['br', { $: { html: 1, block: 1 } }]])
})

it('keeps a stray `</br>` inside the open inline element', async () => {
const result = await parseMarkdown('<span>a</br>b</span>')

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