Skip to content

Commit 77b21e8

Browse files
committed
Change types to use mdast types
1 parent 25c1cfa commit 77b21e8

File tree

3 files changed

+22
-28
lines changed

3 files changed

+22
-28
lines changed

index.js

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @typedef {import('unist').Node} Node
2+
* @typedef {import('mdast').Root|import('mdast').Content} Node
33
* @typedef {import('mdast').Heading} Heading
44
*/
55

@@ -23,24 +23,19 @@ export function normalizeHeadings(tree) {
2323
/** @type {Heading|undefined} */
2424
let title
2525

26-
visit(
27-
tree,
28-
'heading',
29-
/** @type {import('unist-util-visit').Visitor<Heading>} */
30-
(node) => {
31-
if (!first) {
32-
first = node
33-
}
26+
visit(tree, 'heading', (node) => {
27+
if (!first) {
28+
first = node
29+
}
3430

35-
if (node.depth === 1) {
36-
if (title) {
37-
multiple = true
38-
} else {
39-
title = node
40-
}
31+
if (node.depth === 1) {
32+
if (title) {
33+
multiple = true
34+
} else {
35+
title = node
4136
}
4237
}
43-
)
38+
})
4439

4540
// If there are no titles, but there is a first heading.
4641
if (!title && first) {
@@ -49,16 +44,11 @@ export function normalizeHeadings(tree) {
4944

5045
// If there are multiple titles.
5146
if (multiple) {
52-
visit(
53-
tree,
54-
'heading',
55-
/** @type {import('unist-util-visit').Visitor<Heading>} */
56-
(node) => {
57-
if (node !== title && node.depth < max) {
58-
node.depth++
59-
}
47+
visit(tree, 'heading', (node) => {
48+
if (node !== title && node.depth < max) {
49+
node.depth++
6050
}
61-
)
51+
})
6252
}
6353

6454
return tree

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@
3838
],
3939
"dependencies": {
4040
"@types/mdast": "^3.0.0",
41-
"@types/unist": "^2.0.0",
42-
"unist-util-visit": "^3.0.0"
41+
"unist-util-visit": "^4.0.0"
4342
},
4443
"devDependencies": {
4544
"@types/tape": "^4.0.0",

test/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* @typedef {import('mdast').Root} Root
3+
*/
4+
15
import fs from 'node:fs'
26
import path from 'node:path'
37
import test from 'tape'
@@ -27,9 +31,10 @@ test('Level 7', (t) => {
2731
function check(t, test, message) {
2832
const input = fs.readFileSync(path.join('test', 'fixture', test + '.in'))
2933
const output = fs.readFileSync(path.join('test', 'fixture', test + '.out'))
34+
const root = /** @type {Root} */ (remark().parse(input))
3035

3136
t.deepEqual(
32-
removePosition(normalizeHeadings(remark().parse(input)), true),
37+
removePosition(normalizeHeadings(root), true),
3338
removePosition(remark().parse(output), true),
3439
message
3540
)

0 commit comments

Comments
 (0)