Skip to content

Commit 345e61c

Browse files
authored
Add types
Closes GH-40. Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com> Reviewed-by: Titus Wormer <tituswormer@gmail.com>
1 parent 48d420d commit 345e61c

6 files changed

+147
-4
lines changed

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
coverage/
22
mdast-util-to-hast.js
33
mdast-util-to-hast.min.js
4+
*.md

package.json

+10-4
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@
2626
],
2727
"files": [
2828
"lib",
29-
"index.js"
29+
"index.js",
30+
"types/index.d.ts"
3031
],
32+
"types": "types/index.d.ts",
3133
"dependencies": {
34+
"@types/mdast": "^3.0.3",
3235
"collapse-white-space": "^1.0.0",
3336
"detab": "^2.0.0",
34-
"mdast-util-definitions": "^2.0.0",
37+
"dtslint": "^3.4.1",
38+
"mdast-util-definitions": "^3.0.0",
3539
"mdurl": "^1.0.0",
3640
"trim-lines": "^1.0.0",
3741
"unist-builder": "^2.0.0",
@@ -50,13 +54,14 @@
5054
"xo": "^0.28.0"
5155
},
5256
"scripts": {
53-
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
57+
"format": "remark . -qfo && prettier --write . && xo --fix",
5458
"build-bundle": "browserify index.js -s mdastUtilToHast > mdast-util-to-hast.js",
5559
"build-mangle": "browserify index.js -s mdastUtilToHast -p tinyify > mdast-util-to-hast.min.js",
5660
"build": "npm run build-bundle && npm run build-mangle",
5761
"test-api": "node test",
5862
"test-coverage": "nyc --reporter lcov tape test/index.js",
59-
"test": "npm run format && npm run build && npm run test-coverage"
63+
"test-types": "dtslint types",
64+
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
6065
},
6166
"nyc": {
6267
"check-coverage": true,
@@ -76,6 +81,7 @@
7681
"prettier": true,
7782
"esnext": false,
7883
"ignores": [
84+
"**/*.ts",
7985
"mdast-util-to-hast.js"
8086
],
8187
"rules": {

types/index.d.ts

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Minimum TypeScript Version: 3.2
2+
import {Definition} from 'mdast'
3+
import {DefinitionCache} from 'mdast-util-definitions'
4+
import {Node} from 'unist'
5+
6+
declare namespace toHast {
7+
interface H {
8+
dangerous?: boolean
9+
definition: DefinitionCache
10+
footNoteById: Definition
11+
footnoreOrder: string[]
12+
handlers: Handlers
13+
unknownHandler: Handler
14+
(node: Node, tagName: string, props?: object, children?: Node[]): Node
15+
augment(left: Node, right: Node): Node
16+
}
17+
18+
type Handler = (h: H, node: Node) => any
19+
20+
interface Handlers {
21+
[type: string]: Handler
22+
}
23+
24+
interface Options {
25+
/**
26+
* Whether to allow [`html`](https://github.com/syntax-tree/mdast#html) nodes and inject them as raw HTML
27+
*
28+
* Only do this when using [`hast-util-to-html`](https://github.com/syntax-tree/hast-util-to-html)
29+
* ([`rehype-stringify`](https://github.com/rehypejs/rehype/tree/master/packages/rehype-stringify)) or
30+
* [`hast-util-raw`](https://github.com/syntax-tree/hast-util-raw)
31+
* ([`rehype-raw`](https://github.com/rehypejs/rehype-raw)) later: `raw` nodes are not a standard part of
32+
* [hast](https://github.com/syntax-tree/hast).
33+
*
34+
* @default false
35+
*/
36+
allowDangerousHtml?: boolean
37+
38+
/**
39+
* Set to `true` to prefer the first when duplicate definitions are found.
40+
*
41+
* The default behavior is to prefer the last duplicate definition.
42+
*
43+
* @default false
44+
*/
45+
commonmark?: boolean
46+
47+
/**
48+
* Object mapping [mdast](https://github.com/syntax-tree/mdast)
49+
* [nodes](https://github.com/syntax-tree/mdast#nodes) to functions handling them.
50+
* Take a look at
51+
* [`lib/handlers/`](https://github.com/syntax-tree/mdast-util-to-hast/blob/master/lib/handlers)
52+
* for examples.
53+
*/
54+
handlers?: Handlers
55+
56+
/**
57+
* Handler for all unknown nodes.
58+
*
59+
* Default behavior:
60+
*
61+
* * Unknown nodes with [`children`][child] are transformed to `div` elements
62+
* * Unknown nodes with `value` are transformed to [`text`][hast-text] nodes
63+
*/
64+
unknownHandler?: Handler
65+
}
66+
}
67+
68+
/**
69+
* Transform the given [mdast](https://github.com/syntax-tree/mdast)
70+
* [tree](https://github.com/syntax-tree/unist#tree) to a
71+
* [hast](https://github.com/syntax-tree/hast) [tree](https://github.com/syntax-tree/unist#tree).
72+
*/
73+
declare function toHast(node: Node, options?: toHast.Options): Node
74+
75+
export = toHast

types/mdast-util-to-hast-tests.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import toHast = require('mdast-util-to-hast')
2+
3+
toHast({type: ''})
4+
toHast({type: ''}, {allowDangerousHtml: true})
5+
toHast({type: ''}, {commonmark: true})
6+
toHast(
7+
{type: ''},
8+
{
9+
handlers: {
10+
foo(h, node) {
11+
return h(node, 'p')
12+
}
13+
}
14+
}
15+
)
16+
toHast(
17+
{type: ''},
18+
{
19+
handlers: {
20+
foo(h, node) {
21+
return h(node, 'p', {foo: 'bar'})
22+
}
23+
}
24+
}
25+
)
26+
toHast(
27+
{type: ''},
28+
{
29+
handlers: {
30+
foo(h, node) {
31+
return h(node, 'p', {}, [{type: ''}])
32+
}
33+
}
34+
}
35+
)
36+
toHast(
37+
{type: ''},
38+
{
39+
unknownHandler(h, node) {
40+
return h(node, 'div')
41+
}
42+
}
43+
)

types/tsconfig.json

+10
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-to-hast": ["index.d.ts"]
8+
}
9+
}
10+
}

types/tslint.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "dtslint/dtslint.json",
3+
"rules": {
4+
"no-redundant-jsdoc": false,
5+
"semicolon": false,
6+
"whitespace": false
7+
}
8+
}

0 commit comments

Comments
 (0)