Skip to content

Commit f7671da

Browse files
authored
Add types
Reviewed-by: Christian Murphy <[email protected]> Closes GH-7.
1 parent 4556442 commit f7671da

File tree

6 files changed

+170
-4
lines changed

6 files changed

+170
-4
lines changed

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function configure(settings) {
7171
extension = extensions[index]
7272
unsafe = unsafe.concat(extension.unsafe || [])
7373
join = join.concat(extension.join || [])
74-
handlers = Object.assign(handlers, extension.handlers || {})
74+
Object.assign(handlers, extension.handlers || {})
7575
}
7676

7777
return {unsafe: unsafe, join: join, handlers: handlers}

package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@
2929
"Titus Wormer <[email protected]> (https://wooorm.com)"
3030
],
3131
"files": [
32+
"index.js",
3233
"lib/",
33-
"index.js"
34+
"types/index.d.ts"
3435
],
36+
"types": "types",
3537
"dependencies": {
38+
"@types/unist": "^2.0.0",
3639
"longest-streak": "^2.0.0",
3740
"mdast-util-to-string": "^1.0.0",
3841
"parse-entities": "^2.0.0",
@@ -42,6 +45,7 @@
4245
},
4346
"devDependencies": {
4447
"browserify": "^17.0.0",
48+
"dtslint": "^4.0.0",
4549
"mdast-util-from-markdown": "^0.8.0",
4650
"nyc": "^15.0.0",
4751
"prettier": "^2.0.0",
@@ -56,7 +60,8 @@
5660
"build": "browserify . -s mdastUtilToMarkdown -p tinyify > mdast-util-to-markdown.min.js",
5761
"test-api": "node test",
5862
"test-coverage": "nyc --reporter lcov tape test.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,
@@ -77,7 +82,10 @@
7782
"esnext": false,
7883
"rules": {
7984
"unicorn/prefer-includes": "off"
80-
}
85+
},
86+
"ignores": [
87+
"types/"
88+
]
8189
},
8290
"remarkConfig": {
8391
"plugins": [

types/index.d.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Minimum TypeScript Version: 3.0
2+
import {Node, Parent} from 'unist'
3+
4+
export = toMarkdown
5+
6+
declare namespace toMarkdown {
7+
interface SafeOptions {
8+
before: string
9+
after: string
10+
}
11+
12+
type Handle = (
13+
node: Node,
14+
parent: Parent | null | undefined,
15+
context: Context,
16+
safeOptions: SafeOptions
17+
) => string
18+
19+
interface Context {
20+
stack: string[]
21+
enter: (type: string) => () => void
22+
options: Options
23+
unsafePatterns: Unsafe[]
24+
join: Join[]
25+
handle: Handle
26+
}
27+
28+
interface Handlers {
29+
[key: string]: Handler
30+
}
31+
32+
interface Handler {
33+
peek?: Handle
34+
(
35+
node: Node,
36+
parent: Parent | null | undefined,
37+
context: Context,
38+
safeOptions: SafeOptions
39+
): string
40+
}
41+
42+
interface Unsafe {
43+
character: string
44+
inConstruct?: string | string[]
45+
notInConstruct?: string | string[]
46+
after?: string
47+
before?: string
48+
atBreak?: boolean
49+
}
50+
51+
type Join = (
52+
left: Node,
53+
right: Node,
54+
parent: Parent,
55+
context: Context
56+
) => boolean | null | void
57+
58+
interface Extension {
59+
handlers?: Handlers
60+
join?: Join[]
61+
unsafe?: Unsafe[]
62+
}
63+
64+
interface Options {
65+
bullet?: '-' | '*' | '+'
66+
closeAtx?: boolean
67+
emphasis?: '_' | '*'
68+
fence?: '~' | '`'
69+
fences?: boolean
70+
incrementListMarker?: boolean
71+
listItemIndent?: 'tab' | 'one' | 'mixed'
72+
quote?: '"' | "'"
73+
rule?: '-' | '_' | '*'
74+
ruleRepeat?: number
75+
ruleSpaces?: boolean
76+
setext?: boolean
77+
strong?: '_' | '*'
78+
tightDefinitions?: boolean
79+
80+
extensions?: Extension[]
81+
handlers?: Handlers
82+
join?: Join[]
83+
unsafe?: Unsafe[]
84+
}
85+
}
86+
87+
declare function toMarkdown(node: Node, options?: toMarkdown.Options): string

types/test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// This file is for https://github.com/microsoft/dtslint .
2+
// Tests are type-checked, but not run.
3+
4+
import * as toMarkdown from 'mdast-util-to-markdown'
5+
6+
function main() {
7+
const node = {type: 'root'}
8+
const handleOk = () => '\\\n'
9+
const handleNok = () => 1
10+
const joinOk = () => {}
11+
const joinNok = () => {
12+
return 1
13+
}
14+
15+
joinOk.peek = () => '\\'
16+
17+
// $ExpectType string
18+
toMarkdown(node)
19+
20+
// $ExpectError
21+
toMarkdown(node, {unknown: '1'})
22+
23+
// $ExpectType string
24+
toMarkdown(node, {bullet: '+'})
25+
// $ExpectError
26+
toMarkdown(node, {bullet: '?'})
27+
28+
// $ExpectType string
29+
toMarkdown(node, {
30+
unsafe: [
31+
{atBreak: true, character: '_'},
32+
{atBreak: true, before: '\\d+', character: '.', after: '(?:[ \t\r\n]|$)'}
33+
]
34+
})
35+
// $ExpectError
36+
toMarkdown(node, {unsafe: [{unknown: true}]})
37+
38+
// $ExpectType string
39+
toMarkdown(node, {join: [joinOk]})
40+
// $ExpectError
41+
toMarkdown(node, {join: [joinNok]})
42+
43+
// $ExpectType string
44+
toMarkdown(node, {handlers: {break: handleOk}})
45+
// $ExpectError
46+
toMarkdown(node, {handlers: {break: handleNok}})
47+
}
48+
49+
main()

types/tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"moduleResolution": "node",
4+
"lib": [
5+
"ES5"
6+
],
7+
"strict": true,
8+
"baseUrl": ".",
9+
"paths": {
10+
"mdast-util-to-markdown": [
11+
"./index.d.ts"
12+
]
13+
}
14+
}
15+
}

types/tslint.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "dtslint/dtslint.json",
3+
"rules": {
4+
"semicolon": false,
5+
"whitespace": false
6+
}
7+
}

0 commit comments

Comments
 (0)