Skip to content

Commit 50ba4da

Browse files
committed
Fix breaks at end of paragraphs
Previously this behavior was solved on hast. Now, it is solved on hast, which works just as well, and better. Closes remarkjs/remark-rehype#27.
1 parent fa388c7 commit 50ba4da

File tree

3 files changed

+20
-24
lines changed

3 files changed

+20
-24
lines changed

lib/all.js

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,8 @@ export function all(h, parent) {
1818
/** @type {Array<MdastNode>} */
1919
const values = []
2020
let index = -1
21-
let length = nodes.length
22-
let child = nodes[index + 1]
23-
24-
// Trim initial and final `<br>`s.
25-
// They’re not semantic per HTML, and they can’t be made in markdown things
26-
// like paragraphs or headings.
27-
while (child && child.type === 'element' && child.tagName === 'br') {
28-
index++
29-
child = nodes[index + 1]
30-
}
31-
32-
child = nodes[length - 1]
3321

34-
while (
35-
length - 1 > index &&
36-
child &&
37-
child.type === 'element' &&
38-
child.tagName === 'br'
39-
) {
40-
length--
41-
child = nodes[length - 1]
42-
}
43-
44-
while (++index < length) {
22+
while (++index < nodes.length) {
4523
// @ts-expect-error assume `parent` is a parent.
4624
const result = one(h, nodes[index], parent)
4725

@@ -52,5 +30,18 @@ export function all(h, parent) {
5230
}
5331
}
5432

55-
return values
33+
let start = 0
34+
let end = values.length
35+
36+
while (start < end && values[start].type === 'break') {
37+
start++
38+
}
39+
40+
while (end > start && values[end - 1].type === 'break') {
41+
end--
42+
}
43+
44+
return start === 0 && end === values.length
45+
? values
46+
: values.slice(start, end)
5647
}

test/fixtures/br/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
<p><br><br>juliett<br><br></p>
1010
<h1><br>kilo</h1>
1111
<h1>lima<br></h1>
12+
<p>mike</p>november<br><p></p>

test/fixtures/br/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ juliett
1919
# kilo
2020

2121
# lima
22+
23+
mike
24+
25+
november

0 commit comments

Comments
 (0)