Skip to content

Commit

Permalink
Add JSDoc based types
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 22, 2021
1 parent 1480585 commit 02ce0b1
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 48 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
coverage/
node_modules/
*.log
*.d.ts
.DS_Store
yarn.lock
52 changes: 14 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,23 @@
import mdxExpressionFromMarkdown from 'mdast-util-mdx-expression/from-markdown.js'
import mdxExpressionToMarkdown from 'mdast-util-mdx-expression/to-markdown.js'
import mdxJsxFromMarkdown from 'mdast-util-mdx-jsx/from-markdown.js'
import mdxJsxToMarkdown from 'mdast-util-mdx-jsx/to-markdown.js'
import mdxjsEsmFromMarkdown from 'mdast-util-mdxjs-esm/from-markdown.js'
import mdxjsEsmToMarkdown from 'mdast-util-mdxjs-esm/to-markdown.js'
/**
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension
*/

const own = {}.hasOwnProperty
import {
mdxExpressionFromMarkdown,
mdxExpressionToMarkdown
} from 'mdast-util-mdx-expression'
import {mdxJsxFromMarkdown, mdxJsxToMarkdown} from 'mdast-util-mdx-jsx'
import {mdxjsEsmFromMarkdown, mdxjsEsmToMarkdown} from 'mdast-util-mdxjs-esm'

export const mdxFromMarkdown = configure([
/** @type {FromMarkdownExtension[]} */
export const mdxFromMarkdown = [
mdxExpressionFromMarkdown,
mdxJsxFromMarkdown,
mdxjsEsmFromMarkdown
])

function configure(extensions) {
const config = {canContainEols: []}
let index = -1

while (++index < extensions.length) {
extension(config, extensions[index])
}

return config
}

function extension(config, extension) {
let key
let left

for (key in extension) {
if (own.call(extension, key)) {
left = own.call(config, key) ? config[key] : (config[key] = {})
const right = extension[key]

if (key === 'canContainEols') {
config[key] = [].concat(left, right)
} else {
Object.assign(left, right)
}
}
}
}
]

/** @type {ToMarkdownExtension} */
export const mdxToMarkdown = {
extensions: [mdxExpressionToMarkdown, mdxJsxToMarkdown, mdxjsEsmToMarkdown]
}
28 changes: 21 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,38 @@
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
],
"dependencies": {
"mdast-util-mdx-expression": "~0.1.0",
"mdast-util-mdx-jsx": "~0.1.0",
"mdast-util-mdxjs-esm": "~0.1.0"
"mdast-util-mdx-expression": "^1.0.0",
"mdast-util-mdx-jsx": "^1.0.0",
"mdast-util-mdxjs-esm": "^1.0.0"
},
"devDependencies": {
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"mdast-util-from-markdown": "^0.8.0",
"micromark-extension-mdx": "^0.2.0",
"micromark-extension-mdxjs": "^0.3.0",
"mdast-util-from-markdown": "^1.0.0",
"mdast-util-to-markdown": "^1.0.0",
"micromark-extension-mdx": "^1.0.0",
"micromark-extension-mdxjs": "^1.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.39.0"
},
"scripts": {
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node --conditions development test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",
"test": "npm run format && npm run test-coverage"
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -70,5 +78,11 @@
"plugins": [
"preset-wooorm"
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true,
"ignoreCatch": true
}
}
6 changes: 3 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'tape'
import fromMarkdown from 'mdast-util-from-markdown'
import toMarkdown from 'mdast-util-to-markdown'
import mdxjs from 'micromark-extension-mdxjs'
import {fromMarkdown} from 'mdast-util-from-markdown'
import {toMarkdown} from 'mdast-util-to-markdown'
import {mdxjs} from 'micromark-extension-mdxjs'
import {mdxFromMarkdown, mdxToMarkdown} from './index.js'

test('markdown -> mdast', (t) => {
Expand Down
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"include": ["*.js"],
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2020"],
"module": "ES2020",
"moduleResolution": "node",
"allowJs": true,
"checkJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"strict": true
}
}

0 comments on commit 02ce0b1

Please sign in to comment.