Skip to content
This repository was archived by the owner on Aug 9, 2023. It is now read-only.

Commit 4fe70ac

Browse files
committed
Add JSDoc based types
1 parent 9353e53 commit 4fe70ac

File tree

5 files changed

+85
-15
lines changed

5 files changed

+85
-15
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: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1+
/**
2+
* @typedef {import('mdast').Footnote} Footnote
3+
* @typedef {import('mdast').FootnoteReference} FootnoteReference
4+
* @typedef {import('mdast').FootnoteDefinition} FootnoteDefinition
5+
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension
6+
* @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle
7+
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension
8+
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle
9+
* @typedef {import('mdast-util-to-markdown').Map} Map
10+
*/
11+
112
import {normalizeIdentifier} from 'micromark-util-normalize-identifier'
2-
import association from 'mdast-util-to-markdown/lib/util/association.js'
3-
import phrasing from 'mdast-util-to-markdown/lib/util/container-phrasing.js'
4-
import flow from 'mdast-util-to-markdown/lib/util/container-flow.js'
5-
import indentLines from 'mdast-util-to-markdown/lib/util/indent-lines.js'
6-
import safe from 'mdast-util-to-markdown/lib/util/safe.js'
13+
import {association} from 'mdast-util-to-markdown/lib/util/association.js'
14+
import {containerPhrasing} from 'mdast-util-to-markdown/lib/util/container-phrasing.js'
15+
import {containerFlow} from 'mdast-util-to-markdown/lib/util/container-flow.js'
16+
import {indentLines} from 'mdast-util-to-markdown/lib/util/indent-lines.js'
17+
import {safe} from 'mdast-util-to-markdown/lib/util/safe.js'
718

19+
/** @type {FromMarkdownExtension} */
820
export const footnoteFromMarkdown = {
921
canContainEols: ['footnote'],
1022
enter: {
@@ -23,6 +35,7 @@ export const footnoteFromMarkdown = {
2335
}
2436
}
2537

38+
/** @type {ToMarkdownExtension} */
2639
export const footnoteToMarkdown = {
2740
// This is on by default already.
2841
unsafe: [{character: '[', inConstruct: ['phrasing', 'label', 'reference']}],
@@ -32,17 +45,20 @@ export const footnoteToMarkdown = {
3245
footnoteReference.peek = footnoteReferencePeek
3346
footnote.peek = footnotePeek
3447

48+
/** @type {FromMarkdownHandle} */
3549
function enterFootnoteDefinition(token) {
3650
this.enter(
3751
{type: 'footnoteDefinition', identifier: '', label: '', children: []},
3852
token
3953
)
4054
}
4155

56+
/** @type {FromMarkdownHandle} */
4257
function enterFootnoteDefinitionLabelString() {
4358
this.buffer()
4459
}
4560

61+
/** @type {FromMarkdownHandle} */
4662
function exitFootnoteDefinitionLabelString(token) {
4763
const label = this.resume()
4864
this.stack[this.stack.length - 1].label = label
@@ -51,18 +67,22 @@ function exitFootnoteDefinitionLabelString(token) {
5167
).toLowerCase()
5268
}
5369

70+
/** @type {FromMarkdownHandle} */
5471
function exitFootnoteDefinition(token) {
5572
this.exit(token)
5673
}
5774

75+
/** @type {FromMarkdownHandle} */
5876
function enterFootnoteCall(token) {
5977
this.enter({type: 'footnoteReference', identifier: '', label: ''}, token)
6078
}
6179

80+
/** @type {FromMarkdownHandle} */
6281
function enterFootnoteCallString() {
6382
this.buffer()
6483
}
6584

85+
/** @type {FromMarkdownHandle} */
6686
function exitFootnoteCallString(token) {
6787
const label = this.resume()
6888
this.stack[this.stack.length - 1].label = label
@@ -71,18 +91,25 @@ function exitFootnoteCallString(token) {
7191
).toLowerCase()
7292
}
7393

94+
/** @type {FromMarkdownHandle} */
7495
function exitFootnoteCall(token) {
7596
this.exit(token)
7697
}
7798

99+
/** @type {FromMarkdownHandle} */
78100
function enterNote(token) {
79101
this.enter({type: 'footnote', children: []}, token)
80102
}
81103

104+
/** @type {FromMarkdownHandle} */
82105
function exitNote(token) {
83106
this.exit(token)
84107
}
85108

109+
/**
110+
* @type {ToMarkdownHandle}
111+
* @param {FootnoteReference} node
112+
*/
86113
function footnoteReference(node, _, context) {
87114
const exit = context.enter('footnoteReference')
88115
const subexit = context.enter('reference')
@@ -92,34 +119,46 @@ function footnoteReference(node, _, context) {
92119
return '[^' + reference + ']'
93120
}
94121

122+
/** @type {ToMarkdownHandle} */
95123
function footnoteReferencePeek() {
96124
return '['
97125
}
98126

127+
/**
128+
* @type {ToMarkdownHandle}
129+
* @param {Footnote} node
130+
*/
99131
function footnote(node, _, context) {
100132
const exit = context.enter('footnote')
101133
const subexit = context.enter('label')
102-
const value = '^[' + phrasing(node, context, {before: '[', after: ']'}) + ']'
134+
const value =
135+
'^[' + containerPhrasing(node, context, {before: '[', after: ']'}) + ']'
103136
subexit()
104137
exit()
105138
return value
106139
}
107140

141+
/** @type {ToMarkdownHandle} */
108142
function footnotePeek() {
109143
return '^'
110144
}
111145

146+
/**
147+
* @type {ToMarkdownHandle}
148+
* @param {FootnoteDefinition} node
149+
*/
112150
function footnoteDefinition(node, _, context) {
113151
const exit = context.enter('footnoteDefinition')
114152
const subexit = context.enter('label')
115153
const label =
116154
'[^' + safe(context, association(node), {before: '^', after: ']'}) + ']:'
117155
subexit()
118-
const value = indentLines(flow(node, context), map)
156+
const value = indentLines(containerFlow(node, context), map)
119157
exit()
120158

121159
return value
122160

161+
/** @type {Map} */
123162
function map(line, index, blank) {
124163
if (index) {
125164
return (blank ? '' : ' ') + line

package.json

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,36 @@
2828
"sideEffects": false,
2929
"type": "module",
3030
"main": "index.js",
31+
"types": "index.d.ts",
3132
"files": [
33+
"index.d.ts",
3234
"index.js"
3335
],
3436
"dependencies": {
35-
"mdast-util-to-markdown": "^0.6.0",
36-
"micromark-util-normalize-identifier": "^1.0.0-alpha.3"
37+
"@types/mdast": "^3.0.0",
38+
"mdast-util-to-markdown": "^1.0.0",
39+
"micromark-util-normalize-identifier": "^1.0.0"
3740
},
3841
"devDependencies": {
42+
"@types/tape": "^4.0.0",
3943
"c8": "^7.0.0",
40-
"mdast-util-from-markdown": "^0.8.0",
41-
"micromark-extension-footnote": "~0.3.0",
44+
"mdast-util-from-markdown": "^1.0.0",
45+
"micromark-extension-footnote": "^1.0.0",
4246
"prettier": "^2.0.0",
4347
"remark-cli": "^9.0.0",
4448
"remark-preset-wooorm": "^8.0.0",
49+
"rimraf": "^3.0.0",
4550
"tape": "^5.0.0",
51+
"type-coverage": "^2.0.0",
52+
"typescript": "^4.0.0",
4653
"xo": "^0.39.0"
4754
},
4855
"scripts": {
56+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
4957
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
5058
"test-api": "node --conditions development test.js",
5159
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",
52-
"test": "npm run format && npm run test-coverage"
60+
"test": "npm run build && npm run format && npm run test-coverage"
5361
},
5462
"prettier": {
5563
"tabWidth": 2,
@@ -66,5 +74,11 @@
6674
"plugins": [
6775
"preset-wooorm"
6876
]
77+
},
78+
"typeCoverage": {
79+
"atLeast": 100,
80+
"detail": true,
81+
"strict": true,
82+
"ignoreCatch": true
6983
}
7084
}

test.js

Lines changed: 3 additions & 3 deletions
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 footnote from 'micromark-extension-footnote'
2+
import {fromMarkdown} from 'mdast-util-from-markdown'
3+
import {toMarkdown} from 'mdast-util-to-markdown'
4+
import {footnote} from 'micromark-extension-footnote'
55
import {footnoteFromMarkdown, footnoteToMarkdown} from './index.js'
66

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

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)