Skip to content

Add types #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@
"Titus Wormer <tituswormer@gmail.com> (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",
Expand All @@ -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",
Expand All @@ -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,
Expand All @@ -77,7 +82,10 @@
"esnext": false,
"rules": {
"unicorn/prefer-includes": "off"
}
},
"ignores": [
"types/"
]
},
"remarkConfig": {
"plugins": [
Expand Down
87 changes: 87 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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
49 changes: 49 additions & 0 deletions types/test.ts
Original file line number Diff line number Diff line change
@@ -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()
15 changes: 15 additions & 0 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"moduleResolution": "node",
"lib": [
"ES5"
],
"strict": true,
"baseUrl": ".",
"paths": {
"mdast-util-to-markdown": [
"./index.d.ts"
]
}
}
}
7 changes: 7 additions & 0 deletions types/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"semicolon": false,
"whitespace": false
}
}