File tree 3 files changed +56
-54
lines changed
3 files changed +56
-54
lines changed Original file line number Diff line number Diff line change 1
- /**
2
- * @typedef {import('mdast').Content|import('mdast').Root } Node
3
- */
4
-
5
- import { visit } from 'unist-util-visit'
6
-
7
- /**
8
- * Make an mdast tree compact by merging adjacent text nodes and block quotes.
9
- *
10
- * @template {Node} Tree
11
- * @param {Tree } tree
12
- * @returns {Tree }
13
- */
14
- export function compact ( tree ) {
15
- // @ts -expect-error: hush, TS.
16
- visit ( tree , visitor )
17
-
18
- return tree
19
-
20
- /**
21
- * @param {import('mdast').Content } child
22
- * @param {number } index
23
- * @param {Extract<Node, import('mdast').Parent> } parent
24
- */
25
- function visitor ( child , index , parent ) {
26
- if (
27
- parent &&
28
- index &&
29
- ( child . type === 'text' || child . type === 'blockquote' )
30
- ) {
31
- const previous = parent . children [ index - 1 ]
32
-
33
- if ( previous . type === child . type ) {
34
- if ( 'value' in child ) {
35
- // @ts -expect-error `previous` has the same type as `child`.
36
- previous . value += child . value
37
- }
38
-
39
- if ( 'children' in child ) {
40
- // @ts -expect-error `previous` has the same type as `child`.
41
- previous . children = previous . children . concat ( child . children )
42
- }
43
-
44
- parent . children . splice ( index , 1 )
45
-
46
- if ( previous . position && child . position ) {
47
- previous . position . end = child . position . end
48
- }
49
-
50
- return index
51
- }
52
- }
53
- }
54
- }
1
+ export { compact } from './lib/index.js'
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @typedef {import('mdast').Content|import('mdast').Root } Node
3
+ */
4
+
5
+ import { visit } from 'unist-util-visit'
6
+
7
+ /**
8
+ * Make an mdast tree compact by merging adjacent text nodes and block quotes.
9
+ *
10
+ * @template {Node} Tree
11
+ * @param {Tree } tree
12
+ * @returns {Tree }
13
+ */
14
+ export function compact ( tree ) {
15
+ // @ts -expect-error: hush, TS.
16
+ visit ( tree , visitor )
17
+
18
+ return tree
19
+
20
+ /**
21
+ * @param {import('mdast').Content } child
22
+ * @param {number } index
23
+ * @param {Extract<Node, import('mdast').Parent> } parent
24
+ */
25
+ function visitor ( child , index , parent ) {
26
+ if (
27
+ parent &&
28
+ index &&
29
+ ( child . type === 'text' || child . type === 'blockquote' )
30
+ ) {
31
+ const previous = parent . children [ index - 1 ]
32
+
33
+ if ( previous . type === child . type ) {
34
+ if ( 'value' in child ) {
35
+ // @ts -expect-error `previous` has the same type as `child`.
36
+ previous . value += child . value
37
+ }
38
+
39
+ if ( 'children' in child ) {
40
+ // @ts -expect-error `previous` has the same type as `child`.
41
+ previous . children = previous . children . concat ( child . children )
42
+ }
43
+
44
+ parent . children . splice ( index , 1 )
45
+
46
+ if ( previous . position && child . position ) {
47
+ previous . position . end = child . position . end
48
+ }
49
+
50
+ return index
51
+ }
52
+ }
53
+ }
54
+ }
Original file line number Diff line number Diff line change 28
28
"main" : " index.js" ,
29
29
"types" : " index.d.ts" ,
30
30
"files" : [
31
+ " lib/" ,
31
32
" index.d.ts" ,
32
33
" index.js"
33
34
],
You can’t perform that action at this time.
0 commit comments