Skip to content

Commit d119d4f

Browse files
committed
Change types to use mdast types
1 parent f3c7e62 commit d119d4f

File tree

7 files changed

+44
-29
lines changed

7 files changed

+44
-29
lines changed

example.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22
import {inspect} from 'util'
33

44
// Dependencies:
5+
/** @typedef {import('mdast').Root} Root */
56
import {u} from 'unist-builder'
67
import {toc} from './index.js'
78

89
// Now running:
9-
const tree = u('root', [
10-
u('heading', {depth: 1}, [u('text', 'Alpha')]),
11-
u('heading', {depth: 2}, [u('text', 'Bravo')]),
12-
u('heading', {depth: 3}, [u('text', 'Charlie')]),
13-
u('heading', {depth: 2}, [u('text', 'Delta')])
14-
])
10+
const tree = /** @type {Root} */ (
11+
u('root', [
12+
u('heading', {depth: 1}, [u('text', 'Alpha')]),
13+
u('heading', {depth: 2}, [u('text', 'Bravo')]),
14+
u('heading', {depth: 3}, [u('text', 'Charlie')]),
15+
u('heading', {depth: 2}, [u('text', 'Delta')])
16+
])
17+
)
1518

1619
const table = toc(tree)
1720

lib/contents.js

Lines changed: 1 addition & 1 deletion
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').List} List
44
* @typedef {import('mdast').ListItem} ListItem
55
* @typedef {import('mdast').PhrasingContent} PhrasingContent

lib/index.js

Lines changed: 1 addition & 1 deletion
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').List} List
44
* @typedef {import('./search.js').SearchOptions} SearchOptions
55
* @typedef {import('./contents.js').ContentsOptions} ContentsOptions

lib/search.js

Lines changed: 1 addition & 1 deletion
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
* @typedef {import('mdast').PhrasingContent} PhrasingContent
55
* @typedef {import('unist-util-visit').Visitor<Heading>} HeadingVisitor

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
"@types/extend": "^3.0.0",
3939
"@types/github-slugger": "^1.0.0",
4040
"@types/mdast": "^3.0.0",
41-
"@types/unist": "^2.0.0",
4241
"extend": "^3.0.0",
4342
"github-slugger": "^1.0.0",
4443
"mdast-util-to-string": "^3.1.0",

readme.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,22 @@ npm install mdast-util-toc
2626
Dependencies:
2727

2828
```javascript
29+
/** @typedef {import('mdast').Root} Root */
2930
import {u} from 'unist-builder'
3031
import {toc} from 'mdast-util-toc'
3132
```
3233

3334
Now running:
3435

3536
```javascript
36-
const tree = u('root', [
37-
u('heading', {depth: 1}, [u('text', 'Alpha')]),
38-
u('heading', {depth: 2}, [u('text', 'Bravo')]),
39-
u('heading', {depth: 3}, [u('text', 'Charlie')]),
40-
u('heading', {depth: 2}, [u('text', 'Delta')])
41-
])
37+
const tree = /** @type {Root} */ (
38+
u('root', [
39+
u('heading', {depth: 1}, [u('text', 'Alpha')]),
40+
u('heading', {depth: 2}, [u('text', 'Bravo')]),
41+
u('heading', {depth: 3}, [u('text', 'Charlie')]),
42+
u('heading', {depth: 2}, [u('text', 'Delta')])
43+
])
44+
)
4245

4346
const table = toc(tree)
4447
```

test/index.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/**
2-
* @typedef {import('unist').Node} Node
2+
* @typedef {import('mdast').Root} Root
3+
* @typedef {import('mdast').Blockquote} Blockquote
4+
* @typedef {import('mdast').List} List
35
* @typedef {import('../index.js').Options} Options
46
*
57
* @typedef TestConfig
@@ -72,8 +74,9 @@ test('Fixtures', (t) => {
7274
})
7375
}
7476

75-
const actual = toc(processor.runSync(processor.parse(input)), options)
76-
/** @type {Node} */
77+
const tree = /** @type {Root} */ (processor.runSync(processor.parse(input)))
78+
const actual = toc(tree, options)
79+
/** @type {Root} */
7780
const expected = JSON.parse(
7881
String(fs.readFileSync(join(root, name, 'output.json')))
7982
)
@@ -85,19 +88,26 @@ test('Fixtures', (t) => {
8588
})
8689

8790
test('processing nodes', (t) => {
88-
const rootNode = u('root', [
89-
u('heading', {depth: 1}, [u('text', 'Alpha')]),
90-
u('heading', {depth: 2}, [u('text', 'Bravo')])
91-
])
92-
93-
const parentNode = u('parent', rootNode.children)
91+
const rootNode = /** @type {Root} */ (
92+
u('root', [
93+
u('heading', {depth: 1}, [u('text', 'Alpha')]),
94+
u('heading', {depth: 2}, [u('text', 'Bravo')])
95+
])
96+
)
9497

95-
const blockquoteNode = u('root', [
96-
u('heading', {depth: 1}, [u('text', 'Charlie')]),
97-
u('heading', {depth: 2}, [u('text', 'Delta')]),
98+
const parentNode = /** @type {Blockquote} */ (
9899
u('blockquote', rootNode.children)
99-
])
100+
)
101+
102+
const blockquoteNode = /** @type {Root} */ (
103+
u('root', [
104+
u('heading', {depth: 1}, [u('text', 'Charlie')]),
105+
u('heading', {depth: 2}, [u('text', 'Delta')]),
106+
u('blockquote', rootNode.children)
107+
])
108+
)
100109

110+
/** @type {List} */
101111
const expectedRootMap = u('list', {ordered: false, spread: true}, [
102112
u('listItem', {spread: true}, [
103113
u('paragraph', [

0 commit comments

Comments
 (0)