1
1
/**
2
- * @typedef {import('mdast').Content } Node
2
+ * @typedef {import('mdast').Content|import('mdast').Root } Node
3
3
*/
4
4
5
5
import { visit } from 'unist-util-visit'
@@ -12,37 +12,39 @@ import {visit} from 'unist-util-visit'
12
12
* @returns {T }
13
13
*/
14
14
export function compact ( tree ) {
15
- visit ( tree , visitor )
16
-
17
- return tree
18
-
19
- /** @type {import('unist-util-visit').Visitor<Node> } */
20
- function visitor ( child , index , parent ) {
21
- const siblings = parent ? parent . children : [ ]
22
- const previous = index && siblings [ index - 1 ]
23
-
24
- if (
25
- ( child . type === 'text' || child . type === 'blockquote' ) &&
26
- previous &&
27
- child . type === previous . type
28
- ) {
29
- if ( 'value' in child ) {
30
- // @ts -ignore must be text.
31
- previous . value += child . value
15
+ visit (
16
+ tree ,
17
+ /** @type {import('unist-util-visit').Visitor<Node> } */
18
+ // @ts -expect-error: fine.
19
+ ( child , index , parent ) => {
20
+ if (
21
+ parent &&
22
+ index &&
23
+ ( child . type === 'text' || child . type === 'blockquote' ) &&
24
+ child . type === parent . children [ index - 1 ] . type
25
+ ) {
26
+ const previous = parent . children [ index - 1 ]
27
+
28
+ if ( 'value' in child ) {
29
+ // @ts -expect-error must be text.
30
+ previous . value += child . value
31
+ }
32
+
33
+ if ( 'children' in child ) {
34
+ // @ts -expect-error must be block quote.
35
+ previous . children = previous . children . concat ( child . children )
36
+ }
37
+
38
+ parent . children . splice ( index , 1 )
39
+
40
+ if ( previous . position && child . position ) {
41
+ previous . position . end = child . position . end
42
+ }
43
+
44
+ return index
32
45
}
33
-
34
- if ( 'children' in child ) {
35
- // @ts -ignore must be block quote.
36
- previous . children = previous . children . concat ( child . children )
37
- }
38
-
39
- siblings . splice ( index , 1 )
40
-
41
- if ( previous . position && child . position ) {
42
- previous . position . end = child . position . end
43
- }
44
-
45
- return index
46
46
}
47
- }
47
+ )
48
+
49
+ return tree
48
50
}
0 commit comments