Skip to content

Commit bcda18d

Browse files
committed
Refactor to move implementation to lib/
1 parent 83f4ccf commit bcda18d

File tree

3 files changed

+98
-86
lines changed

3 files changed

+98
-86
lines changed

index.js

Lines changed: 6 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,16 @@
11
/**
2-
* @typedef {import('mdast-util-from-markdown').CompileContext} CompileContext
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-
* @typedef {import('estree-jsx').Program} Program
82
* @typedef {import('./complex-types.js').MdxFlowExpression} MdxFlowExpression
93
* @typedef {import('./complex-types.js').MdxTextExpression} MdxTextExpression
104
*/
115

126
/**
137
* @typedef {MdxFlowExpression} MDXFlowExpression
8+
* Deprecated: use `MdxFlowExpression`.
149
* @typedef {MdxTextExpression} MDXTextExpression
10+
* Deprecated: use `MdxFlowExpression`.
1511
*/
1612

17-
/** @type {FromMarkdownExtension} */
18-
export const mdxExpressionFromMarkdown = {
19-
enter: {
20-
mdxFlowExpression: enterMdxFlowExpression,
21-
mdxTextExpression: enterMdxTextExpression
22-
},
23-
exit: {
24-
mdxFlowExpression: exitMdxExpression,
25-
mdxFlowExpressionChunk: exitMdxExpressionData,
26-
mdxTextExpression: exitMdxExpression,
27-
mdxTextExpressionChunk: exitMdxExpressionData
28-
}
29-
}
30-
31-
/** @type {ToMarkdownExtension} */
32-
export const mdxExpressionToMarkdown = {
33-
handlers: {
34-
mdxFlowExpression: handleMdxExpression,
35-
mdxTextExpression: handleMdxExpression
36-
},
37-
unsafe: [
38-
{character: '{', inConstruct: ['phrasing']},
39-
{atBreak: true, character: '{'}
40-
]
41-
}
42-
43-
/**
44-
* @this {CompileContext}
45-
* @type {FromMarkdownHandle}
46-
*/
47-
function enterMdxFlowExpression(token) {
48-
this.enter({type: 'mdxFlowExpression', value: ''}, token)
49-
this.buffer()
50-
}
51-
52-
/**
53-
* @this {CompileContext}
54-
* @type {FromMarkdownHandle}
55-
*/
56-
function enterMdxTextExpression(token) {
57-
this.enter({type: 'mdxTextExpression', value: ''}, token)
58-
this.buffer()
59-
}
60-
61-
/**
62-
* @this {CompileContext}
63-
* @type {FromMarkdownHandle}
64-
*/
65-
function exitMdxExpression(token) {
66-
const value = this.resume()
67-
/** @type {Program|undefined} */
68-
// @ts-expect-error: estree.
69-
const estree = token.estree
70-
const node = /** @type {MDXFlowExpression|MDXTextExpression} */ (
71-
this.exit(token)
72-
)
73-
node.value = value
74-
75-
if (estree) {
76-
node.data = {estree}
77-
}
78-
}
79-
80-
/**
81-
* @this {CompileContext}
82-
* @type {FromMarkdownHandle}
83-
*/
84-
function exitMdxExpressionData(token) {
85-
this.config.enter.data.call(this, token)
86-
this.config.exit.data.call(this, token)
87-
}
88-
89-
/**
90-
* @type {ToMarkdownHandle}
91-
* @param {MDXFlowExpression|MDXTextExpression} node
92-
*/
93-
function handleMdxExpression(node) {
94-
const value = node.value || ''
95-
return '{' + value + '}'
96-
}
13+
export {
14+
mdxExpressionFromMarkdown,
15+
mdxExpressionToMarkdown
16+
} from './lib/index.js'

lib/index.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* @typedef {import('mdast-util-from-markdown').CompileContext} CompileContext
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+
* @typedef {import('estree-jsx').Program} Program
8+
* @typedef {import('../complex-types.js').MdxFlowExpression} MdxFlowExpression
9+
* @typedef {import('../complex-types.js').MdxTextExpression} MdxTextExpression
10+
*/
11+
12+
/** @type {FromMarkdownExtension} */
13+
export const mdxExpressionFromMarkdown = {
14+
enter: {
15+
mdxFlowExpression: enterMdxFlowExpression,
16+
mdxTextExpression: enterMdxTextExpression
17+
},
18+
exit: {
19+
mdxFlowExpression: exitMdxExpression,
20+
mdxFlowExpressionChunk: exitMdxExpressionData,
21+
mdxTextExpression: exitMdxExpression,
22+
mdxTextExpressionChunk: exitMdxExpressionData
23+
}
24+
}
25+
26+
/** @type {ToMarkdownExtension} */
27+
export const mdxExpressionToMarkdown = {
28+
handlers: {
29+
mdxFlowExpression: handleMdxExpression,
30+
mdxTextExpression: handleMdxExpression
31+
},
32+
unsafe: [
33+
{character: '{', inConstruct: ['phrasing']},
34+
{atBreak: true, character: '{'}
35+
]
36+
}
37+
38+
/**
39+
* @this {CompileContext}
40+
* @type {FromMarkdownHandle}
41+
*/
42+
function enterMdxFlowExpression(token) {
43+
this.enter({type: 'mdxFlowExpression', value: ''}, token)
44+
this.buffer()
45+
}
46+
47+
/**
48+
* @this {CompileContext}
49+
* @type {FromMarkdownHandle}
50+
*/
51+
function enterMdxTextExpression(token) {
52+
this.enter({type: 'mdxTextExpression', value: ''}, token)
53+
this.buffer()
54+
}
55+
56+
/**
57+
* @this {CompileContext}
58+
* @type {FromMarkdownHandle}
59+
*/
60+
function exitMdxExpression(token) {
61+
const value = this.resume()
62+
/** @type {Program|undefined} */
63+
// @ts-expect-error: estree.
64+
const estree = token.estree
65+
const node = /** @type {MdxFlowExpression|MdxTextExpression} */ (
66+
this.exit(token)
67+
)
68+
node.value = value
69+
70+
if (estree) {
71+
node.data = {estree}
72+
}
73+
}
74+
75+
/**
76+
* @this {CompileContext}
77+
* @type {FromMarkdownHandle}
78+
*/
79+
function exitMdxExpressionData(token) {
80+
this.config.enter.data.call(this, token)
81+
this.config.exit.data.call(this, token)
82+
}
83+
84+
/**
85+
* @type {ToMarkdownHandle}
86+
* @param {MdxFlowExpression|MdxTextExpression} node
87+
*/
88+
function handleMdxExpression(node) {
89+
const value = node.value || ''
90+
return '{' + value + '}'
91+
}

package.json

Lines changed: 1 addition & 0 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+
"lib/",
3435
"complex-types.d.ts",
3536
"index.d.ts",
3637
"index.js"

0 commit comments

Comments
 (0)