Skip to content

Commit 7a823fc

Browse files
BarryThePenguinwooorm
authored andcommitted
Add types
Closes GH-62. Reviewed-by: Titus Wormer <tituswormer@gmail.com> Reviewed-by: Junyoung Choi <fluke8259@gmail.com> Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com>
1 parent beca120 commit 7a823fc

File tree

7 files changed

+188
-29
lines changed

7 files changed

+188
-29
lines changed

package.json

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,38 @@
1515
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
1616
"Jonathan Haines <jonno.haines@gmail.com> (https://barrythepenguin.github.io)"
1717
],
18+
"types": "types/index.d.ts",
1819
"files": [
20+
"types/index.d.ts",
1921
"lib",
2022
"index.js"
2123
],
2224
"dependencies": {
25+
"@types/mdast": "^3.0.3",
26+
"@types/unist": "^2.0.3",
2327
"extend": "^3.0.2",
2428
"github-slugger": "^1.2.1",
2529
"mdast-util-to-string": "^1.0.5",
26-
"unist-util-is": "^3.0.0",
27-
"unist-util-visit": "^1.1.0"
30+
"unist-util-is": "^4.0.0",
31+
"unist-util-visit": "^2.0.0"
2832
},
2933
"devDependencies": {
3034
"browserify": "^16.2.3",
35+
"dtslint": "^1.0.2",
3136
"nyc": "^14.0.0",
3237
"prettier": "^1.15.2",
33-
"remark": "^10.0.0",
34-
"remark-attr": "^0.8.0",
35-
"remark-cli": "^6.0.0",
36-
"remark-parse": "^6.0.3",
37-
"remark-preset-wooorm": "^5.0.0",
38-
"remark-usage": "^6.1.3",
38+
"remark": "^11.0.1",
39+
"remark-attr": "^0.9.0",
40+
"remark-cli": "^7.0.0",
41+
"remark-parse": "^7.0.1",
42+
"remark-preset-wooorm": "^6.0.1",
43+
"remark-usage": "^7.0.1",
3944
"tape": "^4.10.1",
4045
"tinyify": "^2.5.0",
46+
"typescript": "^3.0.0",
4147
"unified": "^8.0.0",
42-
"unist-builder": "^1.0.3",
43-
"xo": "^0.24.0"
48+
"unist-builder": "^2.0.1",
49+
"xo": "^0.25.3"
4450
},
4551
"scripts": {
4652
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
@@ -49,7 +55,8 @@
4955
"build": "npm run build-bundle && npm run build-mangle",
5056
"test-api": "node test",
5157
"test-coverage": "nyc --reporter lcov tape test/index.js",
52-
"test": "npm run format && npm run build && npm run test-coverage"
58+
"test-types": "dtslint types",
59+
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
5360
},
5461
"nyc": {
5562
"check-coverage": true,

readme.md

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,21 @@ var tree = u('root', [
3636
u('heading', {depth: 3}, [u('text', 'Charlie')]),
3737
u('heading', {depth: 2}, [u('text', 'Delta')])
3838
])
39+
3940
var table = toc(tree)
4041
```
4142

4243
Yields:
4344

4445
```javascript
45-
46-
{
47-
index: null,
46+
{ index: null,
4847
endIndex: null,
49-
map: {
50-
type: 'list',
51-
ordered: false,
52-
spread: true,
53-
children: [
54-
{
55-
type: 'listItem',
56-
loose: true,
57-
spread: true,
58-
children: [Array]
59-
}
60-
]
61-
}
62-
}
48+
map:
49+
{ type: 'list',
50+
ordered: false,
51+
spread: true,
52+
children:
53+
[ { type: 'listItem', loose: true, spread: true, children: [Array] } ] } }
6354
```
6455

6556
## API

test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ test('Fixtures', function(t) {
4040

4141
try {
4242
config = JSON.parse(fs.readFileSync(join(root, name, 'config.json')))
43-
} catch (error) {}
43+
} catch (_) {}
4444

4545
processor.use(remarkParse, config.remarkParseOptions)
4646

types/index.d.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// TypeScript Version: 3.0
2+
3+
import {Node} from 'unist'
4+
import {Parent, Heading, Link, Paragraph, List, ListItem} from 'mdast'
5+
import {Test} from 'unist-util-is'
6+
7+
declare namespace mdastUtilToc {
8+
interface TOCOptions {
9+
/**
10+
* Heading to look for, wrapped in `new RegExp('^(' + value + ')$', 'i')`.
11+
*/
12+
heading?: string
13+
14+
/**
15+
* Maximum heading depth to include in the table of contents,
16+
* This is inclusive: when set to `3`,
17+
* level three headings are included (those with three hashes, `###`).
18+
*
19+
* @default 6
20+
*/
21+
maxDepth?: Heading['depth']
22+
23+
/**
24+
* Headings to skip, wrapped in `new RegExp('^(' + value + ')$', 'i')`.
25+
* Any heading matching this expression will not be present in the table of contents.
26+
*/
27+
skip?: string
28+
29+
/**
30+
* Whether to compile list-items tightly.
31+
*
32+
* @default false
33+
*/
34+
tight?: boolean
35+
36+
/**
37+
* Add a prefix to links to headings in the table of contents.
38+
* Useful for example when later going from mdast to hast and sanitizing with `hast-util-sanitize`.
39+
*
40+
* @default null
41+
*/
42+
prefix?: string
43+
44+
/**
45+
* Allows headings to be children of certain node types
46+
* Internally, uses `unist-util-is` to check, so `parents` can be any `is`-compatible test.
47+
*
48+
* For example, this would allow headings under either `root` or `blockquote` to be used:
49+
*
50+
* ```ts
51+
* toc(tree, {parents: ['root', 'blockquote']})
52+
* ```
53+
*
54+
* @default the to `toc` given `tree`, to only allow top-level headings
55+
*/
56+
parents?: Test<Node> | Array<Test<Node>>
57+
}
58+
59+
interface TOCResult {
60+
index: number | null
61+
endIndex: number | null
62+
map: List | null
63+
}
64+
}
65+
66+
/**
67+
* Generate a Table of Contents from a tree.
68+
*
69+
* @param node searched for headings
70+
* @param options configuration and settings
71+
*/
72+
declare function mdastUtilToc(
73+
node: Node,
74+
options?: mdastUtilToc.TOCOptions
75+
): mdastUtilToc.TOCResult
76+
77+
export = mdastUtilToc

types/mdast-util-toc-tests.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import {Link, Paragraph, List, ListItem} from 'mdast'
2+
3+
import unified = require('unified')
4+
import u = require('unist-builder')
5+
import is = require('unist-util-is')
6+
import toc = require('mdast-util-toc')
7+
8+
const tree = u('root', [
9+
u('heading', {depth: 1}, [u('text', 'Alpha')]),
10+
u('heading', {depth: 2}, [u('text', 'Bravo')]),
11+
u('heading', {depth: 3}, [u('text', 'Charlie')]),
12+
u('heading', {depth: 2}, [u('text', 'Delta')])
13+
])
14+
15+
const {map} = toc(tree)
16+
17+
if (is<List>(map, 'list')) {
18+
const [firstListItem] = map.children
19+
20+
if (is<ListItem>(firstListItem, 'listItem')) {
21+
const [firstParagraph] = firstListItem.children
22+
23+
if (is<Paragraph>(firstParagraph, 'paragraph')) {
24+
const [firstLink] = firstParagraph.children
25+
26+
is<Link>(firstLink, 'link')
27+
}
28+
}
29+
}
30+
31+
toc(tree, {
32+
heading: 'Table Of Contents'
33+
})
34+
35+
toc(tree, {
36+
maxDepth: 2
37+
})
38+
39+
toc(tree, {
40+
skip: 'skip heading'
41+
})
42+
43+
toc(tree, {
44+
tight: true
45+
})
46+
47+
toc(tree, {
48+
prefix: '/prefix'
49+
})
50+
51+
toc(tree, {
52+
parents: ['root', 'blockquote']
53+
})
54+
55+
/*=== usable in unified transform ===*/
56+
unified().use(() => tree => {
57+
const table = toc(tree)
58+
59+
if (is<List>(table.map, 'list')) {
60+
// do something
61+
}
62+
63+
return tree
64+
})

types/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["es2015"],
4+
"strict": true,
5+
"baseUrl": ".",
6+
"paths": {
7+
"mdast-util-toc": ["index.d.ts"]
8+
}
9+
}
10+
}

types/tslint.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "dtslint/dtslint.json",
3+
"rules": {
4+
"no-redundant-jsdoc": false,
5+
"semicolon": false,
6+
"unified-signatures": true,
7+
"whitespace": false,
8+
"strict-export-declare-modifiers": false
9+
}
10+
}

0 commit comments

Comments
 (0)