Skip to content

Commit ebe8a5a

Browse files
committed
Refactor to move implementation to lib/
1 parent c2a6057 commit ebe8a5a

File tree

5 files changed

+81
-75
lines changed

5 files changed

+81
-75
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
coverage/
22
node_modules/
33
*.log
4-
index.d.ts
5-
test.d.ts
4+
*.d.ts
65
.DS_Store
76
yarn.lock
7+
!/complex-types.d.ts

complex-types.d.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
import {type Literal} from 'mdast'
2-
import {type Program} from 'estree-jsx'
1+
import type {Literal} from 'mdast'
2+
import type {Program} from 'estree-jsx'
33

4-
export type MdxjsEsm = {
4+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
5+
export interface MdxjsEsm extends Literal {
56
type: 'mdxjsEsm'
6-
data?: {estree?: Program} & Literal['data']
7-
} & Literal
7+
data?: {estree?: Program}
8+
}
89

910
declare module 'mdast' {
10-
type BlockContentMap = {
11+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
12+
interface BlockContentMap {
1113
mdxjsEsm: MdxjsEsm
1214
}
1315
}
1416

1517
declare module 'hast' {
16-
type RootContentMap = {
18+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
19+
interface RootContentMap {
1720
mdxjsEsm: MdxjsEsm
1821
}
19-
type ElementContentMap = {
22+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
23+
interface ElementContentMap {
2024
mdxjsEsm: MdxjsEsm
2125
}
2226
}

index.js

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1 @@
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').MdxjsEsm} MdxjsEsm
9-
*
10-
* @typedef {MdxjsEsm} MDXJSEsm
11-
* Deprecated name, prefer `MdxjsEsm`.
12-
*/
13-
14-
/** @type {FromMarkdownExtension} */
15-
export const mdxjsEsmFromMarkdown = {
16-
enter: {mdxjsEsm: enterMdxjsEsm},
17-
exit: {mdxjsEsm: exitMdxjsEsm, mdxjsEsmData: exitMdxjsEsmData}
18-
}
19-
20-
/** @type {ToMarkdownExtension} */
21-
export const mdxjsEsmToMarkdown = {handlers: {mdxjsEsm: handleMdxjsEsm}}
22-
23-
/**
24-
* @this {CompileContext}
25-
* @type {FromMarkdownHandle}
26-
*/
27-
function enterMdxjsEsm(token) {
28-
this.enter({type: 'mdxjsEsm', value: ''}, token)
29-
this.buffer() // Capture EOLs
30-
}
31-
32-
/**
33-
* @this {CompileContext}
34-
* @type {FromMarkdownHandle}
35-
*/
36-
function exitMdxjsEsm(token) {
37-
const value = this.resume()
38-
const node = /** @type {MdxjsEsm} */ (this.exit(token))
39-
/** @type {Program|undefined} */
40-
// @ts-expect-error: custom.
41-
const estree = token.estree
42-
43-
node.value = value
44-
45-
if (estree) {
46-
node.data = {estree}
47-
}
48-
}
49-
50-
/**
51-
* @this {CompileContext}
52-
* @type {FromMarkdownHandle}
53-
*/
54-
function exitMdxjsEsmData(token) {
55-
this.config.enter.data.call(this, token)
56-
this.config.exit.data.call(this, token)
57-
}
58-
59-
/**
60-
* @type {ToMarkdownHandle}
61-
* @param {MdxjsEsm} node
62-
*/
63-
function handleMdxjsEsm(node) {
64-
return node.value || ''
65-
}
1+
export {mdxjsEsmFromMarkdown, mdxjsEsmToMarkdown} from './lib/index.js'

lib/index.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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').MdxjsEsm} MdxjsEsm
9+
*
10+
* @typedef {MdxjsEsm} MDXJSEsm
11+
* Deprecated name, prefer `MdxjsEsm`.
12+
*/
13+
14+
/** @type {FromMarkdownExtension} */
15+
export const mdxjsEsmFromMarkdown = {
16+
enter: {mdxjsEsm: enterMdxjsEsm},
17+
exit: {mdxjsEsm: exitMdxjsEsm, mdxjsEsmData: exitMdxjsEsmData}
18+
}
19+
20+
/** @type {ToMarkdownExtension} */
21+
export const mdxjsEsmToMarkdown = {handlers: {mdxjsEsm: handleMdxjsEsm}}
22+
23+
/**
24+
* @this {CompileContext}
25+
* @type {FromMarkdownHandle}
26+
*/
27+
function enterMdxjsEsm(token) {
28+
this.enter({type: 'mdxjsEsm', value: ''}, token)
29+
this.buffer() // Capture EOLs
30+
}
31+
32+
/**
33+
* @this {CompileContext}
34+
* @type {FromMarkdownHandle}
35+
*/
36+
function exitMdxjsEsm(token) {
37+
const value = this.resume()
38+
const node = /** @type {MdxjsEsm} */ (this.exit(token))
39+
/** @type {Program|undefined} */
40+
// @ts-expect-error: custom.
41+
const estree = token.estree
42+
43+
node.value = value
44+
45+
if (estree) {
46+
node.data = {estree}
47+
}
48+
}
49+
50+
/**
51+
* @this {CompileContext}
52+
* @type {FromMarkdownHandle}
53+
*/
54+
function exitMdxjsEsmData(token) {
55+
this.config.enter.data.call(this, token)
56+
this.config.exit.data.call(this, token)
57+
}
58+
59+
/**
60+
* @type {ToMarkdownHandle}
61+
* @param {MdxjsEsm} node
62+
*/
63+
function handleMdxjsEsm(node) {
64+
return node.value || ''
65+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"main": "index.js",
3434
"types": "index.d.ts",
3535
"files": [
36+
"lib/",
3637
"complex-types.d.ts",
3738
"index.d.ts",
3839
"index.js"

0 commit comments

Comments
 (0)