Skip to content

Commit 4e6d7af

Browse files
committed
Change types to use mdast types
1 parent bc975bc commit 4e6d7af

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

index.js

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,33 @@
11
/**
2-
* @typedef {import('mdast').Content|import('mdast').Root} Node
2+
* @typedef {import('mdast').Content|import('mdast').Root} MdastNode
33
*/
44

55
import {visit} from 'unist-util-visit'
66

77
/**
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.
99
*
10-
* @template {Node} T
11-
* @param {T} tree
12-
* @returns {T}
10+
* @template {MdastNode} Tree
11+
* @param {Tree} tree
12+
* @returns {Tree}
1313
*/
1414
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) {
2824
if ('value' in child) {
29-
// @ts-expect-error must be text.
25+
// @ts-expect-error `previous` has the same type as `child`.
3026
previous.value += child.value
3127
}
3228

3329
if ('children' in child) {
34-
// @ts-expect-error must be block quote.
30+
// @ts-expect-error `previous` has the same type as `child`.
3531
previous.children = previous.children.concat(child.children)
3632
}
3733

@@ -44,7 +40,7 @@ export function compact(tree) {
4440
return index
4541
}
4642
}
47-
)
43+
})
4844

4945
return tree
5046
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
],
3434
"dependencies": {
3535
"@types/mdast": "^3.0.0",
36-
"unist-util-visit": "^3.0.0"
36+
"unist-util-visit": "^4.0.0"
3737
},
3838
"devDependencies": {
3939
"@types/tape": "^4.0.0",

0 commit comments

Comments
 (0)