diff --git a/lib/all.js b/lib/all.js index 73fc666..6e18fae 100644 --- a/lib/all.js +++ b/lib/all.js @@ -18,17 +18,39 @@ export function all(h, parent) { // @ts-expect-error Assume `parent` is a parent. const nodes = parent.children || [] /** @type {Array.} */ - let values = [] + const values = [] let index = -1 - /** @type {MdastNode|Array.} */ - let result + let length = nodes.length + let child = nodes[index + 1] - while (++index < nodes.length) { + // Trim initial and final `
`s. + // They’re not semantic per HTML, and they can’t be made in markdown things + // like paragraphs or headings. + while (child && child.type === 'element' && child.tagName === 'br') { + index++ + child = nodes[index + 1] + } + + child = nodes[length - 1] + + while ( + length - 1 > index && + child && + child.type === 'element' && + child.tagName === 'br' + ) { + length-- + child = nodes[length - 1] + } + + while (++index < length) { // @ts-expect-error assume `parent` is a parent. - result = one(h, nodes[index], parent) + const result = one(h, nodes[index], parent) - if (result) { - values = values.concat(result) + if (Array.isArray(result)) { + values.push(...result) + } else if (result) { + values.push(result) } } diff --git a/test/fixtures/br/index.html b/test/fixtures/br/index.html index b779ba9..770bddf 100644 --- a/test/fixtures/br/index.html +++ b/test/fixtures/br/index.html @@ -1,2 +1,11 @@

alpha
bravo

charlie
delta
+


+

echo

+

foxtrot

+


golf

+



hotel

+


india

+



juliett

+


kilo

+

lima

diff --git a/test/fixtures/br/index.md b/test/fixtures/br/index.md index 58cb0a5..b17aec9 100644 --- a/test/fixtures/br/index.md +++ b/test/fixtures/br/index.md @@ -3,3 +3,19 @@ bravo charlie delta + +echo + +foxtrot + +golf + +hotel + +india + +juliett + +# kilo + +# lima