File tree Expand file tree Collapse file tree 7 files changed +48
-19
lines changed Expand file tree Collapse file tree 7 files changed +48
-19
lines changed Original file line number Diff line number Diff line change 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}
Original file line number Diff line number Diff line change 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.
1315 * mdast node.
1416 */
1517export 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 } */
Original file line number Diff line number Diff 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/**
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ import structuredClone from '@ungap/structured-clone'
1616import { phrasing as hastPhrasing } from 'hast-util-phrasing'
1717import { whitespace } from 'hast-util-whitespace'
1818import { 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
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change @@ -25,3 +25,10 @@ juliett
2525mike
2626
2727november
28+
29+ oscar\
30+ papa
31+
32+ quebec
33+
34+ romeo
You can’t perform that action at this time.
0 commit comments