diff --git a/lib/index.js b/lib/index.js index cdd7102..73ef3f3 100644 --- a/lib/index.js +++ b/lib/index.js @@ -71,7 +71,7 @@ function configure(settings) { extension = extensions[index] unsafe = unsafe.concat(extension.unsafe || []) join = join.concat(extension.join || []) - handlers = Object.assign(handlers, extension.handlers || {}) + Object.assign(handlers, extension.handlers || {}) } return {unsafe: unsafe, join: join, handlers: handlers} diff --git a/package.json b/package.json index 5b3b1ae..483acc4 100644 --- a/package.json +++ b/package.json @@ -29,10 +29,13 @@ "Titus Wormer (https://wooorm.com)" ], "files": [ + "index.js", "lib/", - "index.js" + "types/index.d.ts" ], + "types": "types", "dependencies": { + "@types/unist": "^2.0.0", "longest-streak": "^2.0.0", "mdast-util-to-string": "^1.0.0", "parse-entities": "^2.0.0", @@ -42,6 +45,7 @@ }, "devDependencies": { "browserify": "^17.0.0", + "dtslint": "^4.0.0", "mdast-util-from-markdown": "^0.8.0", "nyc": "^15.0.0", "prettier": "^2.0.0", @@ -56,7 +60,8 @@ "build": "browserify . -s mdastUtilToMarkdown -p tinyify > mdast-util-to-markdown.min.js", "test-api": "node test", "test-coverage": "nyc --reporter lcov tape test.js", - "test": "npm run format && npm run build && npm run test-coverage" + "test-types": "dtslint types", + "test": "npm run format && npm run build && npm run test-coverage && npm run test-types" }, "nyc": { "check-coverage": true, @@ -77,7 +82,10 @@ "esnext": false, "rules": { "unicorn/prefer-includes": "off" - } + }, + "ignores": [ + "types/" + ] }, "remarkConfig": { "plugins": [ diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..95e4e04 --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,87 @@ +// Minimum TypeScript Version: 3.0 +import {Node, Parent} from 'unist' + +export = toMarkdown + +declare namespace toMarkdown { + interface SafeOptions { + before: string + after: string + } + + type Handle = ( + node: Node, + parent: Parent | null | undefined, + context: Context, + safeOptions: SafeOptions + ) => string + + interface Context { + stack: string[] + enter: (type: string) => () => void + options: Options + unsafePatterns: Unsafe[] + join: Join[] + handle: Handle + } + + interface Handlers { + [key: string]: Handler + } + + interface Handler { + peek?: Handle + ( + node: Node, + parent: Parent | null | undefined, + context: Context, + safeOptions: SafeOptions + ): string + } + + interface Unsafe { + character: string + inConstruct?: string | string[] + notInConstruct?: string | string[] + after?: string + before?: string + atBreak?: boolean + } + + type Join = ( + left: Node, + right: Node, + parent: Parent, + context: Context + ) => boolean | null | void + + interface Extension { + handlers?: Handlers + join?: Join[] + unsafe?: Unsafe[] + } + + interface Options { + bullet?: '-' | '*' | '+' + closeAtx?: boolean + emphasis?: '_' | '*' + fence?: '~' | '`' + fences?: boolean + incrementListMarker?: boolean + listItemIndent?: 'tab' | 'one' | 'mixed' + quote?: '"' | "'" + rule?: '-' | '_' | '*' + ruleRepeat?: number + ruleSpaces?: boolean + setext?: boolean + strong?: '_' | '*' + tightDefinitions?: boolean + + extensions?: Extension[] + handlers?: Handlers + join?: Join[] + unsafe?: Unsafe[] + } +} + +declare function toMarkdown(node: Node, options?: toMarkdown.Options): string diff --git a/types/test.ts b/types/test.ts new file mode 100644 index 0000000..b776991 --- /dev/null +++ b/types/test.ts @@ -0,0 +1,49 @@ +// This file is for https://github.com/microsoft/dtslint . +// Tests are type-checked, but not run. + +import * as toMarkdown from 'mdast-util-to-markdown' + +function main() { + const node = {type: 'root'} + const handleOk = () => '\\\n' + const handleNok = () => 1 + const joinOk = () => {} + const joinNok = () => { + return 1 + } + + joinOk.peek = () => '\\' + + // $ExpectType string + toMarkdown(node) + + // $ExpectError + toMarkdown(node, {unknown: '1'}) + + // $ExpectType string + toMarkdown(node, {bullet: '+'}) + // $ExpectError + toMarkdown(node, {bullet: '?'}) + + // $ExpectType string + toMarkdown(node, { + unsafe: [ + {atBreak: true, character: '_'}, + {atBreak: true, before: '\\d+', character: '.', after: '(?:[ \t\r\n]|$)'} + ] + }) + // $ExpectError + toMarkdown(node, {unsafe: [{unknown: true}]}) + + // $ExpectType string + toMarkdown(node, {join: [joinOk]}) + // $ExpectError + toMarkdown(node, {join: [joinNok]}) + + // $ExpectType string + toMarkdown(node, {handlers: {break: handleOk}}) + // $ExpectError + toMarkdown(node, {handlers: {break: handleNok}}) +} + +main() diff --git a/types/tsconfig.json b/types/tsconfig.json new file mode 100644 index 0000000..5677cb7 --- /dev/null +++ b/types/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "moduleResolution": "node", + "lib": [ + "ES5" + ], + "strict": true, + "baseUrl": ".", + "paths": { + "mdast-util-to-markdown": [ + "./index.d.ts" + ] + } + } +} diff --git a/types/tslint.json b/types/tslint.json new file mode 100644 index 0000000..70c4494 --- /dev/null +++ b/types/tslint.json @@ -0,0 +1,7 @@ +{ + "extends": "dtslint/dtslint.json", + "rules": { + "semicolon": false, + "whitespace": false + } +}