Skip to content

Commit ca24d81

Browse files
committed
Add expressions to mdast node type registry
When using this utility, the expression types will automatically be added to mdast in the correct places. See DefinitelyTyped/DefinitelyTyped#54421 for more information.
1 parent 87d02d7 commit ca24d81

File tree

5 files changed

+47
-19
lines changed

5 files changed

+47
-19
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
coverage/
22
node_modules/
3-
*.d.ts
3+
index.d.ts
4+
test.d.ts
45
*.log
56
.DS_Store
67
yarn.lock

complex-types.d.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {Literal} from 'mdast'
2+
import {Program} from 'estree-jsx'
3+
4+
export interface MDXFlowExpression extends Literal {
5+
type: 'mdxFlowExpression'
6+
data?: {
7+
estree?: Program
8+
} & Literal['data']
9+
}
10+
11+
export interface MDXTextExpression extends Literal {
12+
type: 'mdxTextExpression'
13+
data?: {
14+
estree?: Program
15+
} & Literal['data']
16+
}
17+
18+
declare module 'mdast' {
19+
interface StaticPhrasingContentMap {
20+
mdxTextExpression: MDXTextExpression
21+
}
22+
23+
interface BlockContentMap {
24+
mdxFlowExpression: MDXFlowExpression
25+
}
26+
}

index.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
/**
2-
* @typedef {import('mdast').Literal} Literal
32
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension
43
* @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle
54
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension
65
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle
7-
* @typedef {import('estree-jsx').Program} Estree
8-
*
9-
* @typedef {Literal & {type: 'mdxFlowExpression', data: {estree?: Estree}}} MDXFlowExpression
10-
* @typedef {Literal & {type: 'mdxSpanExpression', data: {estree?: Estree}}} MDXSpanExpression
6+
* @typedef {import('estree-jsx').Program} Program
7+
* @typedef {import('./complex-types').MDXFlowExpression} MDXFlowExpression
8+
* @typedef {import('./complex-types').MDXTextExpression} MDXTextExpression
119
*/
1210

1311
import stripIndent from 'strip-indent'
@@ -42,29 +40,29 @@ export const mdxExpressionToMarkdown = {
4240

4341
/** @type {FromMarkdownHandle} */
4442
function enterMdxFlowExpression(token) {
45-
// @ts-expect-error: fine.
4643
this.enter({type: 'mdxFlowExpression', value: ''}, token)
4744
this.buffer()
4845
}
4946

5047
/** @type {FromMarkdownHandle} */
5148
function enterMdxTextExpression(token) {
52-
// @ts-expect-error: fine.
5349
this.enter({type: 'mdxTextExpression', value: ''}, token)
5450
this.buffer()
5551
}
5652

5753
/** @type {FromMarkdownHandle} */
5854
function exitMdxExpression(token) {
5955
const value = this.resume()
60-
const node = this.exit(token)
61-
56+
/** @type {Program|undefined} */
57+
// @ts-expect-error: estree.
58+
const estree = token.estree
59+
const node = /** @type {MDXFlowExpression|MDXTextExpression} */ (
60+
this.exit(token)
61+
)
6262
node.value = token.type === 'mdxFlowExpression' ? dedent(value) : value
6363

64-
// @ts-expect-error: estree.
65-
if (token.estree) {
66-
// @ts-expect-error: estree.
67-
node.data = {estree: token.estree}
64+
if (estree) {
65+
node.data = {estree}
6866
}
6967
}
7068

@@ -76,7 +74,7 @@ function exitMdxExpressionData(token) {
7674

7775
/**
7876
* @type {ToMarkdownHandle}
79-
* @param {MDXFlowExpression|MDXSpanExpression} node
77+
* @param {MDXFlowExpression|MDXTextExpression} node
8078
*/
8179
function handleMdxExpression(node) {
8280
const value = node.value || ''

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"main": "index.js",
3232
"types": "index.d.ts",
3333
"files": [
34+
"complex-types.d.ts",
3435
"index.d.ts",
3536
"index.js"
3637
],
@@ -39,7 +40,7 @@
3940
"strip-indent": "^4.0.0"
4041
},
4142
"devDependencies": {
42-
"@types/acorn": "^4.0.5",
43+
"@types/acorn": "^4.0.0",
4344
"@types/tape": "^4.0.0",
4445
"acorn": "^8.0.0",
4546
"c8": "^7.0.0",
@@ -57,7 +58,7 @@
5758
"xo": "^0.39.0"
5859
},
5960
"scripts": {
60-
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
61+
"build": "rimraf \"{index,test}.d.ts\" && tsc && type-coverage",
6162
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
6263
"test-api": "node --conditions development test.js",
6364
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",

test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ test('mdast -> markdown', (t) => {
419419
children: [
420420
{type: 'mdxFlowExpression', value: 'a + b'},
421421
{type: 'mdxFlowExpression', value: '\nc +\n1\n'},
422+
// @ts-expect-error: `value` missing.
422423
{type: 'mdxFlowExpression'},
423424
{type: 'paragraph', children: [{type: 'text', value: 'd'}]}
424425
]
@@ -439,6 +440,7 @@ test('mdast -> markdown', (t) => {
439440
{type: 'text', value: ', d '},
440441
{type: 'mdxTextExpression', value: 'e + 1'},
441442
{type: 'text', value: ', f '},
443+
// @ts-expect-error: `value` missing.
442444
{type: 'mdxTextExpression'},
443445
{type: 'text', value: '.'}
444446
]
@@ -460,10 +462,10 @@ test('mdast -> markdown', (t) => {
460462

461463
t.deepEqual(
462464
toMarkdown(
463-
{type: 'definition', url: 'x', title: 'a\n{\nb'},
465+
{type: 'definition', identifier: 'a', url: 'x', title: 'a\n{\nb'},
464466
{extensions: [mdxExpressionToMarkdown]}
465467
),
466-
'[]: x "a\n\\{\nb"\n',
468+
'[a]: x "a\n\\{\nb"\n',
467469
'should escape `{` at the start of a line'
468470
)
469471

0 commit comments

Comments
 (0)