Skip to content

Commit 452cf21

Browse files
committed
Add JSDoc based types
1 parent 400bfe0 commit 452cf21

File tree

5 files changed

+91
-9
lines changed

5 files changed

+91
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
coverage/
22
node_modules/
33
.DS_Store
4+
*.d.ts
45
*.log
56
yarn.lock

index.js

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
1-
import matters from 'micromark-extension-frontmatter/lib/matters.js'
1+
/**
2+
* @typedef {import('mdast').Literal} Literal
3+
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension
4+
* @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle
5+
* @typedef {import('mdast-util-to-markdown/lib/types.js').Options} ToMarkdownExtension
6+
* @typedef {import('mdast-util-to-markdown/lib/types.js').Handle} ToMarkdownHandle
7+
* @typedef {import('mdast-util-to-markdown/lib/util/indent-lines.js').Map} Map
8+
*
9+
* @typedef {import('micromark-extension-frontmatter/matters.js').Options} Options
10+
* @typedef {import('micromark-extension-frontmatter/matters.js').Matter} Matter
11+
* @typedef {import('micromark-extension-frontmatter/matters.js').Info} Info
12+
*/
213

14+
import {matters} from 'micromark-extension-frontmatter/matters.js'
15+
16+
/**
17+
* @param {Options} [options]
18+
* @returns {FromMarkdownExtension}
19+
*/
320
export function frontmatterFromMarkdown(options) {
421
const settings = matters(options)
22+
/** @type {FromMarkdownExtension['enter']} */
523
const enter = {}
24+
/** @type {FromMarkdownExtension['exit']} */
625
const exit = {}
726
let index = -1
827

@@ -16,27 +35,41 @@ export function frontmatterFromMarkdown(options) {
1635
return {enter, exit}
1736
}
1837

38+
/**
39+
* @param {Matter} matter
40+
* @returns {FromMarkdownHandle} enter
41+
*/
1942
function opener(matter) {
2043
return open
44+
/** @type {FromMarkdownHandle} */
2145
function open(token) {
46+
// @ts-expect-error: custom.
2247
this.enter({type: matter.type, value: ''}, token)
2348
this.buffer()
2449
}
2550
}
2651

52+
/** @type {FromMarkdownHandle} */
2753
function close(token) {
2854
const data = this.resume()
2955
// Remove the initial and final eol.
3056
this.exit(token).value = data.replace(/^(\r?\n|\r)|(\r?\n|\r)$/g, '')
3157
}
3258

59+
/** @type {FromMarkdownHandle} */
3360
function value(token) {
3461
this.config.enter.data.call(this, token)
3562
this.config.exit.data.call(this, token)
3663
}
3764

65+
/**
66+
* @param {Options} [options]
67+
* @returns {ToMarkdownExtension}
68+
*/
3869
export function frontmatterToMarkdown(options) {
70+
/** @type {ToMarkdownExtension['unsafe']} */
3971
const unsafe = []
72+
/** @type {ToMarkdownExtension['handlers']} */
4073
const handlers = {}
4174
const settings = matters(options)
4275
let index = -1
@@ -50,23 +83,42 @@ export function frontmatterToMarkdown(options) {
5083
return {unsafe, handlers}
5184
}
5285

86+
/**
87+
* @param {Matter} matter
88+
* @returns {(node: Literal) => string} enter
89+
*/
5390
function handler(matter) {
5491
const open = fence(matter, 'open')
5592
const close = fence(matter, 'close')
5693

5794
return handle
5895

96+
/**
97+
* @type {ToMarkdownHandle}
98+
* @param {Literal} node
99+
*/
59100
function handle(node) {
60101
return open + (node.value ? '\n' + node.value : '') + '\n' + close
61102
}
62103
}
63104

105+
/**
106+
* @param {Matter} matter
107+
* @param {'open'|'close'} prop
108+
* @returns {string}
109+
*/
64110
function fence(matter, prop) {
65111
return matter.marker
66112
? pick(matter.marker, prop).repeat(3)
67-
: pick(matter.fence, prop)
113+
: // @ts-expect-error: They’re mutually exclusive.
114+
pick(matter.fence, prop)
68115
}
69116

117+
/**
118+
* @param {Info|string} schema
119+
* @param {'open'|'close'} prop
120+
* @returns {string}
121+
*/
70122
function pick(schema, prop) {
71123
return typeof schema === 'string' ? schema : schema[prop]
72124
}

package.json

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,35 @@
2929
"sideEffects": false,
3030
"type": "module",
3131
"main": "index.js",
32+
"types": "index.d.ts",
3233
"files": [
34+
"index.d.ts",
3335
"index.js"
3436
],
3537
"dependencies": {
36-
"micromark-extension-frontmatter": "^0.2.0"
38+
"micromark-extension-frontmatter": "^1.0.0"
3739
},
3840
"devDependencies": {
41+
"@types/tape": "^4.0.0",
3942
"c8": "^7.0.0",
40-
"mdast-util-from-markdown": "^0.8.0",
41-
"mdast-util-to-markdown": "^0.6.0",
43+
"mdast-util-from-markdown": "^1.0.0",
44+
"mdast-util-to-markdown": "^1.0.0",
4245
"prettier": "^2.0.0",
4346
"remark-cli": "^9.0.0",
4447
"remark-preset-wooorm": "^8.0.0",
48+
"rimraf": "^3.0.0",
4549
"tape": "^5.0.0",
50+
"type-coverage": "^2.0.0",
51+
"typescript": "^4.0.0",
4652
"unist-util-remove-position": "^4.0.0",
4753
"xo": "^0.39.0"
4854
},
4955
"scripts": {
56+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
5057
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
5158
"test-api": "node --conditions development test.js",
5259
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",
53-
"test": "npm run format && npm run test-coverage"
60+
"test": "npm run build && npm run format && npm run test-coverage"
5461
},
5562
"prettier": {
5663
"tabWidth": 2,
@@ -67,5 +74,11 @@
6774
"plugins": [
6875
"preset-wooorm"
6976
]
77+
},
78+
"typeCoverage": {
79+
"atLeast": 100,
80+
"detail": true,
81+
"strict": true,
82+
"ignoreCatch": true
7083
}
7184
}

test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import test from 'tape'
2-
import fromMarkdown from 'mdast-util-from-markdown'
3-
import toMarkdown from 'mdast-util-to-markdown'
2+
import {fromMarkdown} from 'mdast-util-from-markdown'
3+
import {toMarkdown} from 'mdast-util-to-markdown'
44
import {removePosition} from 'unist-util-remove-position'
5-
import frontmatter from 'micromark-extension-frontmatter'
5+
import {frontmatter} from 'micromark-extension-frontmatter'
66
import {frontmatterFromMarkdown, frontmatterToMarkdown} from './index.js'
77

88
const custom = {type: 'custom', marker: {open: '<', close: '>'}}

tsconfig.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"include": ["*.js"],
3+
"compilerOptions": {
4+
"target": "ES2020",
5+
"lib": ["ES2020"],
6+
"module": "ES2020",
7+
"moduleResolution": "node",
8+
"allowJs": true,
9+
"checkJs": true,
10+
"declaration": true,
11+
"emitDeclarationOnly": true,
12+
"allowSyntheticDefaultImports": true,
13+
"skipLibCheck": true,
14+
"strict": true
15+
}
16+
}

0 commit comments

Comments
 (0)