Skip to content

Commit 3d392a6

Browse files
committed
Add JSDoc based types
1 parent 14b286a commit 3d392a6

File tree

5 files changed

+97
-33
lines changed

5 files changed

+97
-33
lines changed

Diff for: .gitignore

+1
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

Diff for: index.js

+59-26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
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').Options} ToMarkdownExtension
6+
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle
7+
*
8+
* @typedef {Literal & {type: 'math', lang?: string|null, meta?: string|null}} Math
9+
* @typedef {Literal & {type: 'inlineMath'}} InlineMath
10+
*/
11+
112
import {longestStreak} from 'longest-streak'
2-
import safe from 'mdast-util-to-markdown/lib/util/safe.js'
13+
import {safe} from 'mdast-util-to-markdown/lib/util/safe.js'
314

15+
/** @type {FromMarkdownExtension} */
416
export const mathFromMarkdown = {
517
enter: {
618
mathFlow: enterMathFlow,
@@ -17,6 +29,7 @@ export const mathFromMarkdown = {
1729
}
1830
}
1931

32+
/** @type {ToMarkdownExtension} */
2033
export const mathToMarkdown = {
2134
unsafe: [
2235
{character: '\r', inConstruct: ['mathFlowMeta']},
@@ -29,79 +42,94 @@ export const mathToMarkdown = {
2942

3043
inlineMath.peek = inlineMathPeek
3144

45+
/** @type {FromMarkdownHandle} */
3246
function enterMathFlow(token) {
33-
this.enter(
34-
{
35-
type: 'math',
36-
meta: null,
37-
value: '',
38-
data: {
39-
hName: 'div',
40-
hProperties: {className: ['math', 'math-display']},
41-
hChildren: [{type: 'text', value: ''}]
42-
}
43-
},
44-
token
45-
)
47+
/** @type {Math} */
48+
const node = {
49+
type: 'math',
50+
meta: null,
51+
value: '',
52+
data: {
53+
hName: 'div',
54+
hProperties: {className: ['math', 'math-display']},
55+
hChildren: [{type: 'text', value: ''}]
56+
}
57+
}
58+
// @ts-expect-error: custom node.
59+
this.enter(node, token)
4660
}
4761

62+
/** @type {FromMarkdownHandle} */
4863
function enterMathFlowMeta() {
4964
this.buffer()
5065
}
5166

67+
/** @type {FromMarkdownHandle} */
5268
function exitMathFlowMeta() {
5369
const data = this.resume()
5470
this.stack[this.stack.length - 1].meta = data
5571
}
5672

73+
/** @type {FromMarkdownHandle} */
5774
function exitMathFlowFence() {
5875
// Exit if this is the closing fence.
5976
if (this.getData('mathFlowInside')) return
6077
this.buffer()
6178
this.setData('mathFlowInside', true)
6279
}
6380

81+
/** @type {FromMarkdownHandle} */
6482
function exitMathFlow(token) {
6583
const data = this.resume().replace(/^(\r?\n|\r)|(\r?\n|\r)$/g, '')
6684
const node = this.exit(token)
6785
node.value = data
86+
// @ts-expect-error: we defined it.
6887
node.data.hChildren[0].value = data
6988
this.setData('mathFlowInside')
7089
}
7190

91+
/** @type {FromMarkdownHandle} */
7292
function enterMathText(token) {
73-
this.enter(
74-
{
75-
type: 'inlineMath',
76-
value: '',
77-
data: {
78-
hName: 'span',
79-
hProperties: {className: ['math', 'math-inline']},
80-
hChildren: [{type: 'text', value: ''}]
81-
}
82-
},
83-
token
84-
)
93+
/** @type {InlineMath} */
94+
const node = {
95+
type: 'inlineMath',
96+
value: '',
97+
data: {
98+
hName: 'span',
99+
hProperties: {className: ['math', 'math-inline']},
100+
hChildren: [{type: 'text', value: ''}]
101+
}
102+
}
103+
// @ts-expect-error: custom node.
104+
this.enter(node, token)
85105
this.buffer()
86106
}
87107

108+
/** @type {FromMarkdownHandle} */
88109
function exitMathText(token) {
89110
const data = this.resume()
90111
const node = this.exit(token)
91112
node.value = data
113+
// @ts-expect-error: we defined it.
92114
node.data.hChildren[0].value = data
93115
}
94116

117+
/** @type {FromMarkdownHandle} */
95118
function exitMathData(token) {
96119
this.config.enter.data.call(this, token)
97120
this.config.exit.data.call(this, token)
98121
}
99122

123+
/**
124+
* @type {ToMarkdownHandle}
125+
* @param {Math} node
126+
*/
100127
function math(node, _, context) {
101128
const raw = node.value || ''
102129
const fence = '$'.repeat(Math.max(longestStreak(raw, '$') + 1, 2))
103130
const exit = context.enter('mathFlow')
104131
let value = fence
132+
/** @type {ReturnType<context['enter']>} */
105133
let subexit
106134

107135
if (node.meta) {
@@ -121,6 +149,10 @@ function math(node, _, context) {
121149
return value
122150
}
123151

152+
/**
153+
* @type {ToMarkdownHandle}
154+
* @param {InlineMath} node
155+
*/
124156
function inlineMath(node) {
125157
const value = node.value || ''
126158
let size = 1
@@ -147,6 +179,7 @@ function inlineMath(node) {
147179
return sequence + pad + value + pad + sequence
148180
}
149181

182+
/** @type {ToMarkdownHandle} */
150183
function inlineMathPeek() {
151184
return '$'
152185
}

Diff for: package.json

+18-4
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,36 @@
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": {
38+
"@types/mdast": "^3.0.0",
3639
"longest-streak": "^3.0.0",
37-
"mdast-util-to-markdown": "^0.6.0"
40+
"mdast-util-to-markdown": "^1.0.0"
3841
},
3942
"devDependencies": {
43+
"@types/tape": "^4.0.0",
4044
"c8": "^7.0.0",
41-
"mdast-util-from-markdown": "^0.8.0",
42-
"micromark-extension-math": "^0.1.0",
45+
"mdast-util-from-markdown": "^1.0.0",
46+
"micromark-extension-math": "^1.0.0",
4347
"prettier": "^2.0.0",
4448
"remark-cli": "^9.0.0",
4549
"remark-preset-wooorm": "^8.0.0",
50+
"rimraf": "^3.0.0",
4651
"tape": "^5.0.0",
52+
"type-coverage": "^2.0.0",
53+
"typescript": "^4.0.0",
4754
"xo": "^0.39.0"
4855
},
4956
"scripts": {
57+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
5058
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
5159
"test-api": "node --conditions development test.js",
5260
"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"
61+
"test": "npm run build && npm run format && npm run test-coverage"
5462
},
5563
"prettier": {
5664
"tabWidth": 2,
@@ -67,5 +75,11 @@
6775
"plugins": [
6876
"preset-wooorm"
6977
]
78+
},
79+
"typeCoverage": {
80+
"atLeast": 100,
81+
"detail": true,
82+
"strict": true,
83+
"ignoreCatch": true
7084
}
7185
}

Diff for: test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import test from 'tape'
2-
import fromMarkdown from 'mdast-util-from-markdown'
3-
import toMarkdown from 'mdast-util-to-markdown'
4-
import math from 'micromark-extension-math'
2+
import {fromMarkdown} from 'mdast-util-from-markdown'
3+
import {toMarkdown} from 'mdast-util-to-markdown'
4+
import {math} from 'micromark-extension-math'
55
import {mathFromMarkdown, mathToMarkdown} from './index.js'
66

77
test('markdown -> mdast', (t) => {

Diff for: tsconfig.json

+16
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)