Skip to content

Commit 96288c0

Browse files
committed
Refactor to move implementation to lib/
1 parent aac8d47 commit 96288c0

File tree

3 files changed

+56
-54
lines changed

3 files changed

+56
-54
lines changed

index.js

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1 @@
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'

lib/index.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"main": "index.js",
2929
"types": "index.d.ts",
3030
"files": [
31+
"lib/",
3132
"index.d.ts",
3233
"index.js"
3334
],

0 commit comments

Comments
 (0)