File tree 5 files changed +24
-66
lines changed
5 files changed +24
-66
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
1
/**
2
2
* @typedef {import('mdast').Nodes } Nodes
3
- * @typedef {import('mdast').Paragraph } Paragraph
4
3
*/
5
4
6
5
import { visit } from 'unist-util-visit'
7
6
8
7
/**
9
8
* Remove empty paragraphs in `tree`.
10
9
*
11
- * @template {Nodes} Tree
12
- * Node type.
13
- * @param {Tree } tree
10
+ * @param {Nodes } tree
14
11
* Tree to change.
15
- * @returns {Tree extends Paragraph ? Tree | null : Tree }
16
- * Changed `tree`, or `null` if it was an empty paragraph .
12
+ * @returns {undefined }
13
+ * Nothing .
17
14
*/
18
15
export function squeezeParagraphs ( tree ) {
19
- if ( emptyParagraph ( tree ) ) {
20
- // @ts -expect-error: it’s an empty paragraph.
21
- return null
22
- }
23
-
24
16
visit ( tree , function ( node , index , parent ) {
25
- if ( index !== undefined && parent && emptyParagraph ( node ) ) {
17
+ if (
18
+ index !== undefined &&
19
+ parent &&
20
+ node . type === 'paragraph' &&
21
+ node . children . every ( function ( child ) {
22
+ return child . type === 'text' && / ^ \s * $ / . test ( child . value )
23
+ } )
24
+ ) {
26
25
parent . children . splice ( index , 1 )
27
26
return index
28
27
}
29
28
} )
30
-
31
- // @ts -expect-error: it’s not an empty paragraph.
32
- return tree
33
- }
34
-
35
- /**
36
- * Check if a node is an empty paragraph.
37
- *
38
- * @param {Nodes } node
39
- * Node to check.
40
- * @returns {boolean }
41
- * Whether `node` was an empty paragraph.
42
- */
43
- function emptyParagraph ( node ) {
44
- return (
45
- node . type === 'paragraph' &&
46
- node . children . every ( function ( child ) {
47
- return child . type === 'text' && / ^ \s * $ / . test ( child . value )
48
- } )
49
- )
50
29
}
Original file line number Diff line number Diff line change 49
49
"prettier" : " ^2.0.0" ,
50
50
"remark-cli" : " ^11.0.0" ,
51
51
"remark-preset-wooorm" : " ^9.0.0" ,
52
- "tape" : " ^5.0.0" ,
53
- "tsd" : " ^0.28.0" ,
54
52
"type-coverage" : " ^2.0.0" ,
55
53
"typescript" : " ^5.0.0" ,
56
54
"unist-builder" : " ^4.0.0" ,
84
82
"strict" : true
85
83
},
86
84
"xo" : {
87
- "overrides" : [
88
- {
89
- "files" : [
90
- " **/*.ts"
91
- ],
92
- "rules" : {
93
- "@typescript-eslint/ban-types" : " off" ,
94
- "@typescript-eslint/consistent-type-assertions" : " off"
95
- }
96
- }
97
- ],
98
85
"prettier" : true
99
86
}
100
87
}
Original file line number Diff line number Diff line change @@ -106,7 +106,7 @@ Remove empty paragraphs in `tree`.
106
106
107
107
###### Returns
108
108
109
- Changed ` tree ` ( [ ` Node ` ] [ node ] ), or ` null ` if it was an empty paragraph .
109
+ Nothing ( ` undefined ` ) .
110
110
111
111
## Types
112
112
Original file line number Diff line number Diff line change @@ -20,8 +20,10 @@ test('squeezeParagraphs', async function (t) {
20
20
] )
21
21
] )
22
22
23
+ squeezeParagraphs ( tree )
24
+
23
25
assert . deepEqual (
24
- squeezeParagraphs ( tree ) ,
26
+ tree ,
25
27
u ( 'root' , [
26
28
u ( 'paragraph' , [ u ( 'text' , 'first' ) ] ) ,
27
29
u ( 'paragraph' , [
@@ -37,9 +39,14 @@ test('squeezeParagraphs', async function (t) {
37
39
)
38
40
} )
39
41
40
- await t . test ( 'should return `null` for empty paragraphs' , async function ( ) {
41
- const tree = u ( 'paragraph' , [ ] )
42
+ await t . test (
43
+ 'should do nothing with a (empty or not) paragraph' ,
44
+ async function ( ) {
45
+ const tree = u ( 'paragraph' , [ ] )
42
46
43
- assert . deepEqual ( squeezeParagraphs ( tree ) , null )
44
- } )
47
+ squeezeParagraphs ( tree )
48
+
49
+ assert . deepEqual ( tree , u ( 'paragraph' , [ ] ) )
50
+ }
51
+ )
45
52
} )
You can’t perform that action at this time.
0 commit comments