Skip to content

Commit d6cab31

Browse files
committed
Fix to not drop brs in phrasing
Closes GH-83.
1 parent f8a2bf0 commit d6cab31

File tree

7 files changed

+48
-19
lines changed

7 files changed

+48
-19
lines changed

lib/handlers/heading.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* @import {Heading, PhrasingContent} from 'mdast'
55
*/
66

7+
import {dropSurroundingBreaks} from '../util/drop-surrounding-breaks.js'
8+
79
/**
810
* @param {State} state
911
* State.
@@ -17,7 +19,9 @@ export function heading(state, node) {
1719
/* c8 ignore next */
1820
Number(node.tagName.charAt(1)) || 1
1921
)
20-
const children = /** @type {Array<PhrasingContent>} */ (state.all(node))
22+
const children = dropSurroundingBreaks(
23+
/** @type {Array<PhrasingContent>} */ (state.all(node))
24+
)
2125

2226
/** @type {Heading} */
2327
const result = {type: 'heading', depth, children}

lib/handlers/p.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* @import {Paragraph, PhrasingContent} from 'mdast'
55
*/
66

7+
import {dropSurroundingBreaks} from '../util/drop-surrounding-breaks.js'
8+
79
/**
810
* @param {State} state
911
* State.
@@ -13,9 +15,11 @@
1315
* mdast node.
1416
*/
1517
export function p(state, node) {
16-
// Allow potentially “invalid” nodes, they might be unknown.
17-
// We also support straddling later.
18-
const children = /** @type {Array<PhrasingContent>} */ (state.all(node))
18+
const children = dropSurroundingBreaks(
19+
// Allow potentially “invalid” nodes, they might be unknown.
20+
// We also support straddling later.
21+
/** @type {Array<PhrasingContent>} */ (state.all(node))
22+
)
1923

2024
if (children.length > 0) {
2125
/** @type {Paragraph} */

lib/state.js

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -227,20 +227,7 @@ function all(parent) {
227227
}
228228
}
229229

230-
let start = 0
231-
let end = results.length
232-
233-
while (start < end && results[start].type === 'break') {
234-
start++
235-
}
236-
237-
while (end > start && results[end - 1].type === 'break') {
238-
end--
239-
}
240-
241-
return start === 0 && end === results.length
242-
? results
243-
: results.slice(start, end)
230+
return results
244231
}
245232

246233
/**

lib/util/drop-surrounding-breaks.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @import {Nodes} from 'mdast'
3+
*/
4+
5+
/**
6+
* Drop trailing initial and final `br`s.
7+
*
8+
* @template {Nodes} Node
9+
* Node type.
10+
* @param {Array<Node>} nodes
11+
* List of nodes.
12+
* @returns {Array<Node>}
13+
* List of nodes w/o `break`s.
14+
*/
15+
export function dropSurroundingBreaks(nodes) {
16+
let start = 0
17+
let end = nodes.length
18+
19+
while (start < end && nodes[start].type === 'break') start++
20+
while (end > start && nodes[end - 1].type === 'break') end--
21+
22+
return start === 0 && end === nodes.length ? nodes : nodes.slice(start, end)
23+
}

lib/util/wrap.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import structuredClone from '@ungap/structured-clone'
1616
import {phrasing as hastPhrasing} from 'hast-util-phrasing'
1717
import {whitespace} from 'hast-util-whitespace'
1818
import {phrasing as mdastPhrasing} from 'mdast-util-phrasing'
19+
import {dropSurroundingBreaks} from './drop-surrounding-breaks.js'
1920

2021
/**
2122
* Check if there are phrasing mdast nodes.
@@ -63,7 +64,7 @@ export function wrap(nodes) {
6364
return d.type === 'text' ? whitespace(d.value) : false
6465
})
6566
? []
66-
: [{type: 'paragraph', children: nodes}]
67+
: [{type: 'paragraph', children: dropSurroundingBreaks(nodes)}]
6768
}
6869
}
6970

test/fixtures/br/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@
1010
<h1><br>kilo</h1>
1111
<h1>lima<br></h1>
1212
<p>mike</p>november<br><p></p>
13+
<p>oscar<span><br></span>papa</p>
14+
<p><span><br></span>quebec</p>
15+
<p>romeo<span><br></span></p>

test/fixtures/br/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,10 @@ juliett
2525
mike
2626

2727
november
28+
29+
oscar\
30+
papa
31+
32+
quebec
33+
34+
romeo

0 commit comments

Comments
 (0)