diff --git a/package.json b/package.json index 6fce64c..4011dfc 100644 --- a/package.json +++ b/package.json @@ -27,16 +27,20 @@ "Titus Wormer (https://wooorm.com)" ], "files": [ - "index.js" + "index.js", + "types/index.d.ts" ], + "types": "types", "dependencies": { + "@types/mdast": "^3.0.0", "mdast-util-to-string": "^1.0.0", - "micromark": "~2.9.0", + "micromark": "~2.10.0", "parse-entities": "^2.0.0" }, "devDependencies": { "browserify": "^16.0.0", "commonmark.json": "^0.29.0", + "dtslint": "^4.0.0", "gzip-size-cli": "^3.0.0", "hast-util-to-html": "^7.0.0", "mdast-util-to-hast": "^10.0.0", @@ -57,7 +61,8 @@ "generate": "npm run generate-size", "test-api": "node test", "test-coverage": "nyc --reporter lcov tape test/index.js", - "test": "npm run format && npm run generate && npm run test-coverage" + "test-types": "dtslint types", + "test": "npm run format && npm run generate && npm run test-coverage && npm run test-types" }, "nyc": { "check-coverage": true, diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..cb858ad --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,34 @@ +// Minimum TypeScript Version: 3.0 +import { + Buffer, + BufferEncoding, + SyntaxExtension, + Token +} from 'micromark/dist/shared-types' +import {Root} from 'mdast' +import {Type} from 'micromark/dist/constant/types' + +export = fromMarkdown + +declare namespace fromMarkdown { + interface MdastExtension { + enter: Record void> + exit: Record void> + } + + interface Options { + extensions?: SyntaxExtension[] + mdastExtensions?: MdastExtension[] + } +} + +declare function fromMarkdown( + value: string | Buffer, + options?: fromMarkdown.Options +): Root + +declare function fromMarkdown( + value: string | Buffer, + encoding?: BufferEncoding, + options?: fromMarkdown.Options +): Root diff --git a/types/mdast-util-from-markdown.test.ts b/types/mdast-util-from-markdown.test.ts new file mode 100644 index 0000000..a574e5c --- /dev/null +++ b/types/mdast-util-from-markdown.test.ts @@ -0,0 +1,25 @@ +// This file is for https://github.com/microsoft/dtslint . +// Tests are type-checked, but not run. + +import * as fromMarkdown from 'mdast-util-from-markdown' + +function main() { + const raw = '# text **strong**' + + // $ExpectType Root + fromMarkdown(raw) + + // $ExpectType Root + fromMarkdown(Buffer.alloc(8)) + + // $ExpectType Root + fromMarkdown(Buffer.alloc(8), {extensions: []}) + + // $ExpectType Root + fromMarkdown(Buffer.alloc(8), 'utf-8', {mdastExtensions: []}) + + // $ExpectError + fromMarkdown(Buffer.alloc(8), 'utf-8', {allowDangerousHtml: true}) +} + +main() diff --git a/types/tsconfig.json b/types/tsconfig.json new file mode 100644 index 0000000..9a36131 --- /dev/null +++ b/types/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "moduleResolution": "node", + "lib": [ + "ES5" + ], + "strict": true, + "baseUrl": ".", + "paths": { + "mdast-util-from-markdown": [ + "./index.d.ts" + ] + } + } +} diff --git a/types/tslint.json b/types/tslint.json new file mode 100644 index 0000000..9001796 --- /dev/null +++ b/types/tslint.json @@ -0,0 +1,7 @@ +{ + "extends": "dtslint/dtslint.json", + "rules": { + "semicolon": false, + "whitespace": false + } +}