Skip to content

Commit

Permalink
Change types to use mdast types
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 26, 2021
1 parent f3c7e62 commit d119d4f
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 29 deletions.
15 changes: 9 additions & 6 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
import {inspect} from 'util'

// Dependencies:
/** @typedef {import('mdast').Root} Root */
import {u} from 'unist-builder'
import {toc} from './index.js'

// Now running:
const tree = u('root', [
u('heading', {depth: 1}, [u('text', 'Alpha')]),
u('heading', {depth: 2}, [u('text', 'Bravo')]),
u('heading', {depth: 3}, [u('text', 'Charlie')]),
u('heading', {depth: 2}, [u('text', 'Delta')])
])
const tree = /** @type {Root} */ (
u('root', [
u('heading', {depth: 1}, [u('text', 'Alpha')]),
u('heading', {depth: 2}, [u('text', 'Bravo')]),
u('heading', {depth: 3}, [u('text', 'Charlie')]),
u('heading', {depth: 2}, [u('text', 'Delta')])
])
)

const table = toc(tree)

Expand Down
2 changes: 1 addition & 1 deletion lib/contents.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @typedef {import('unist').Node} Node
* @typedef {import('mdast').Root|import('mdast').Content} Node
* @typedef {import('mdast').List} List
* @typedef {import('mdast').ListItem} ListItem
* @typedef {import('mdast').PhrasingContent} PhrasingContent
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @typedef {import('unist').Node} Node
* @typedef {import('mdast').Root|import('mdast').Content} Node
* @typedef {import('mdast').List} List
* @typedef {import('./search.js').SearchOptions} SearchOptions
* @typedef {import('./contents.js').ContentsOptions} ContentsOptions
Expand Down
2 changes: 1 addition & 1 deletion lib/search.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @typedef {import('unist').Node} Node
* @typedef {import('mdast').Root|import('mdast').Content} Node
* @typedef {import('mdast').Heading} Heading
* @typedef {import('mdast').PhrasingContent} PhrasingContent
* @typedef {import('unist-util-visit').Visitor<Heading>} HeadingVisitor
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"@types/extend": "^3.0.0",
"@types/github-slugger": "^1.0.0",
"@types/mdast": "^3.0.0",
"@types/unist": "^2.0.0",
"extend": "^3.0.0",
"github-slugger": "^1.0.0",
"mdast-util-to-string": "^3.1.0",
Expand Down
15 changes: 9 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@ npm install mdast-util-toc
Dependencies:

```javascript
/** @typedef {import('mdast').Root} Root */
import {u} from 'unist-builder'
import {toc} from 'mdast-util-toc'
```

Now running:

```javascript
const tree = u('root', [
u('heading', {depth: 1}, [u('text', 'Alpha')]),
u('heading', {depth: 2}, [u('text', 'Bravo')]),
u('heading', {depth: 3}, [u('text', 'Charlie')]),
u('heading', {depth: 2}, [u('text', 'Delta')])
])
const tree = /** @type {Root} */ (
u('root', [
u('heading', {depth: 1}, [u('text', 'Alpha')]),
u('heading', {depth: 2}, [u('text', 'Bravo')]),
u('heading', {depth: 3}, [u('text', 'Charlie')]),
u('heading', {depth: 2}, [u('text', 'Delta')])
])
)

const table = toc(tree)
```
Expand Down
36 changes: 23 additions & 13 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/**
* @typedef {import('unist').Node} Node
* @typedef {import('mdast').Root} Root
* @typedef {import('mdast').Blockquote} Blockquote
* @typedef {import('mdast').List} List
* @typedef {import('../index.js').Options} Options
*
* @typedef TestConfig
Expand Down Expand Up @@ -72,8 +74,9 @@ test('Fixtures', (t) => {
})
}

const actual = toc(processor.runSync(processor.parse(input)), options)
/** @type {Node} */
const tree = /** @type {Root} */ (processor.runSync(processor.parse(input)))
const actual = toc(tree, options)
/** @type {Root} */
const expected = JSON.parse(
String(fs.readFileSync(join(root, name, 'output.json')))
)
Expand All @@ -85,19 +88,26 @@ test('Fixtures', (t) => {
})

test('processing nodes', (t) => {
const rootNode = u('root', [
u('heading', {depth: 1}, [u('text', 'Alpha')]),
u('heading', {depth: 2}, [u('text', 'Bravo')])
])

const parentNode = u('parent', rootNode.children)
const rootNode = /** @type {Root} */ (
u('root', [
u('heading', {depth: 1}, [u('text', 'Alpha')]),
u('heading', {depth: 2}, [u('text', 'Bravo')])
])
)

const blockquoteNode = u('root', [
u('heading', {depth: 1}, [u('text', 'Charlie')]),
u('heading', {depth: 2}, [u('text', 'Delta')]),
const parentNode = /** @type {Blockquote} */ (
u('blockquote', rootNode.children)
])
)

const blockquoteNode = /** @type {Root} */ (
u('root', [
u('heading', {depth: 1}, [u('text', 'Charlie')]),
u('heading', {depth: 2}, [u('text', 'Delta')]),
u('blockquote', rootNode.children)
])
)

/** @type {List} */
const expectedRootMap = u('list', {ordered: false, spread: true}, [
u('listItem', {spread: true}, [
u('paragraph', [
Expand Down

0 comments on commit d119d4f

Please sign in to comment.