1
1
/**
2
- * @typedef {import('mdast').Content|import('mdast').Root } Node
2
+ * @typedef {import('mdast').Content|import('mdast').Root } MdastNode
3
3
*/
4
4
5
5
import { visit } from 'unist-util-visit'
6
6
7
7
/**
8
- * Make an mdast tree compact by merging adjacent text nodes.
8
+ * Make an mdast tree compact by merging adjacent text nodes and block quotes .
9
9
*
10
- * @template {Node} T
11
- * @param {T } tree
12
- * @returns {T }
10
+ * @template {MdastNode} Tree
11
+ * @param {Tree } tree
12
+ * @returns {Tree }
13
13
*/
14
14
export function compact ( tree ) {
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
-
15
+ visit ( tree , ( child , index , parent ) => {
16
+ if (
17
+ parent &&
18
+ index &&
19
+ ( child . type === 'text' || child . type === 'blockquote' )
20
+ ) {
21
+ const previous = parent . children [ index - 1 ]
22
+
23
+ if ( previous . type === child . type ) {
28
24
if ( 'value' in child ) {
29
- // @ts -expect-error must be text .
25
+ // @ts -expect-error `previous` has the same type as `child` .
30
26
previous . value += child . value
31
27
}
32
28
33
29
if ( 'children' in child ) {
34
- // @ts -expect-error must be block quote .
30
+ // @ts -expect-error `previous` has the same type as `child` .
35
31
previous . children = previous . children . concat ( child . children )
36
32
}
37
33
@@ -44,7 +40,7 @@ export function compact(tree) {
44
40
return index
45
41
}
46
42
}
47
- )
43
+ } )
48
44
49
45
return tree
50
46
}
0 commit comments