File tree 7 files changed +48
-19
lines changed 7 files changed +48
-19
lines changed Original file line number Diff line number Diff line change 4
4
* @import {Heading, PhrasingContent} from 'mdast'
5
5
*/
6
6
7
+ import { dropSurroundingBreaks } from '../util/drop-surrounding-breaks.js'
8
+
7
9
/**
8
10
* @param {State } state
9
11
* State.
@@ -17,7 +19,9 @@ export function heading(state, node) {
17
19
/* c8 ignore next */
18
20
Number ( node . tagName . charAt ( 1 ) ) || 1
19
21
)
20
- const children = /** @type {Array<PhrasingContent> } */ ( state . all ( node ) )
22
+ const children = dropSurroundingBreaks (
23
+ /** @type {Array<PhrasingContent> } */ ( state . all ( node ) )
24
+ )
21
25
22
26
/** @type {Heading } */
23
27
const result = { type : 'heading' , depth, children}
Original file line number Diff line number Diff line change 4
4
* @import {Paragraph, PhrasingContent} from 'mdast'
5
5
*/
6
6
7
+ import { dropSurroundingBreaks } from '../util/drop-surrounding-breaks.js'
8
+
7
9
/**
8
10
* @param {State } state
9
11
* State.
13
15
* mdast node.
14
16
*/
15
17
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
+ )
19
23
20
24
if ( children . length > 0 ) {
21
25
/** @type {Paragraph } */
Original file line number Diff line number Diff line change @@ -227,20 +227,7 @@ function all(parent) {
227
227
}
228
228
}
229
229
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
244
231
}
245
232
246
233
/**
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'
16
16
import { phrasing as hastPhrasing } from 'hast-util-phrasing'
17
17
import { whitespace } from 'hast-util-whitespace'
18
18
import { phrasing as mdastPhrasing } from 'mdast-util-phrasing'
19
+ import { dropSurroundingBreaks } from './drop-surrounding-breaks.js'
19
20
20
21
/**
21
22
* Check if there are phrasing mdast nodes.
@@ -63,7 +64,7 @@ export function wrap(nodes) {
63
64
return d . type === 'text' ? whitespace ( d . value ) : false
64
65
} )
65
66
? [ ]
66
- : [ { type : 'paragraph' , children : nodes } ]
67
+ : [ { type : 'paragraph' , children : dropSurroundingBreaks ( nodes ) } ]
67
68
}
68
69
}
69
70
Original file line number Diff line number Diff line change 10
10
< h1 > < br > kilo</ h1 >
11
11
< h1 > lima< br > </ h1 >
12
12
< 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
25
25
mike
26
26
27
27
november
28
+
29
+ oscar\
30
+ papa
31
+
32
+ quebec
33
+
34
+ romeo
You can’t perform that action at this time.
0 commit comments