Skip to content

Commit a02be0e

Browse files
committed
Fix to filter out initial, final <br>s
Closes GH-66.
1 parent ff69c3f commit a02be0e

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

lib/all.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,39 @@ export function all(h, parent) {
1818
// @ts-expect-error Assume `parent` is a parent.
1919
const nodes = parent.children || []
2020
/** @type {Array.<MdastNode>} */
21-
let values = []
21+
const values = []
2222
let index = -1
23-
/** @type {MdastNode|Array.<MdastNode>} */
24-
let result
23+
let length = nodes.length
24+
let child = nodes[index + 1]
2525

26-
while (++index < nodes.length) {
26+
// Trim initial and final `<br>`s.
27+
// They’re not semantic per HTML, and they can’t be made in markdown things
28+
// like paragraphs or headings.
29+
while (child && child.type === 'element' && child.tagName === 'br') {
30+
index++
31+
child = nodes[index + 1]
32+
}
33+
34+
child = nodes[length - 1]
35+
36+
while (
37+
length - 1 > index &&
38+
child &&
39+
child.type === 'element' &&
40+
child.tagName === 'br'
41+
) {
42+
length--
43+
child = nodes[length - 1]
44+
}
45+
46+
while (++index < length) {
2747
// @ts-expect-error assume `parent` is a parent.
28-
result = one(h, nodes[index], parent)
48+
const result = one(h, nodes[index], parent)
2949

30-
if (result) {
31-
values = values.concat(result)
50+
if (Array.isArray(result)) {
51+
values.push(...result)
52+
} else if (result) {
53+
values.push(result)
3254
}
3355
}
3456

test/fixtures/br/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
<p>alpha<br>bravo</p>
22
<pre>charlie<br>delta</pre>
3+
<p><br></p>
4+
<p>echo<br></p>
5+
<p>foxtrot<br><br></p>
6+
<p><br>golf</p>
7+
<p><br><br>hotel</p>
8+
<p><br>india<br></p>
9+
<p><br><br>juliett<br><br></p>
10+
<h1><br>kilo</h1>
11+
<h1>lima<br></h1>

test/fixtures/br/index.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,19 @@ bravo
33

44
charlie
55
delta
6+
7+
echo
8+
9+
foxtrot
10+
11+
golf
12+
13+
hotel
14+
15+
india
16+
17+
juliett
18+
19+
# kilo
20+
21+
# lima

0 commit comments

Comments
 (0)