Skip to content

Commit

Permalink
Refactor to move implementation to lib/
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 30, 2023
1 parent c2a6057 commit ebe8a5a
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 75 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
coverage/
node_modules/
*.log
index.d.ts
test.d.ts
*.d.ts
.DS_Store
yarn.lock
!/complex-types.d.ts
20 changes: 12 additions & 8 deletions complex-types.d.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import {type Literal} from 'mdast'
import {type Program} from 'estree-jsx'
import type {Literal} from 'mdast'
import type {Program} from 'estree-jsx'

export type MdxjsEsm = {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
export interface MdxjsEsm extends Literal {
type: 'mdxjsEsm'
data?: {estree?: Program} & Literal['data']
} & Literal
data?: {estree?: Program}
}

declare module 'mdast' {
type BlockContentMap = {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface BlockContentMap {
mdxjsEsm: MdxjsEsm
}
}

declare module 'hast' {
type RootContentMap = {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface RootContentMap {
mdxjsEsm: MdxjsEsm
}
type ElementContentMap = {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface ElementContentMap {
mdxjsEsm: MdxjsEsm
}
}
66 changes: 1 addition & 65 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,65 +1 @@
/**
* @typedef {import('mdast-util-from-markdown').CompileContext} CompileContext
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension
* @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle
* @typedef {import('estree-jsx').Program} Program
* @typedef {import('./complex-types.js').MdxjsEsm} MdxjsEsm
*
* @typedef {MdxjsEsm} MDXJSEsm
* Deprecated name, prefer `MdxjsEsm`.
*/

/** @type {FromMarkdownExtension} */
export const mdxjsEsmFromMarkdown = {
enter: {mdxjsEsm: enterMdxjsEsm},
exit: {mdxjsEsm: exitMdxjsEsm, mdxjsEsmData: exitMdxjsEsmData}
}

/** @type {ToMarkdownExtension} */
export const mdxjsEsmToMarkdown = {handlers: {mdxjsEsm: handleMdxjsEsm}}

/**
* @this {CompileContext}
* @type {FromMarkdownHandle}
*/
function enterMdxjsEsm(token) {
this.enter({type: 'mdxjsEsm', value: ''}, token)
this.buffer() // Capture EOLs
}

/**
* @this {CompileContext}
* @type {FromMarkdownHandle}
*/
function exitMdxjsEsm(token) {
const value = this.resume()
const node = /** @type {MdxjsEsm} */ (this.exit(token))
/** @type {Program|undefined} */
// @ts-expect-error: custom.
const estree = token.estree

node.value = value

if (estree) {
node.data = {estree}
}
}

/**
* @this {CompileContext}
* @type {FromMarkdownHandle}
*/
function exitMdxjsEsmData(token) {
this.config.enter.data.call(this, token)
this.config.exit.data.call(this, token)
}

/**
* @type {ToMarkdownHandle}
* @param {MdxjsEsm} node
*/
function handleMdxjsEsm(node) {
return node.value || ''
}
export {mdxjsEsmFromMarkdown, mdxjsEsmToMarkdown} from './lib/index.js'
65 changes: 65 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @typedef {import('mdast-util-from-markdown').CompileContext} CompileContext
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension
* @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle
* @typedef {import('estree-jsx').Program} Program
* @typedef {import('../complex-types.js').MdxjsEsm} MdxjsEsm
*
* @typedef {MdxjsEsm} MDXJSEsm
* Deprecated name, prefer `MdxjsEsm`.
*/

/** @type {FromMarkdownExtension} */
export const mdxjsEsmFromMarkdown = {
enter: {mdxjsEsm: enterMdxjsEsm},
exit: {mdxjsEsm: exitMdxjsEsm, mdxjsEsmData: exitMdxjsEsmData}
}

/** @type {ToMarkdownExtension} */
export const mdxjsEsmToMarkdown = {handlers: {mdxjsEsm: handleMdxjsEsm}}

/**
* @this {CompileContext}
* @type {FromMarkdownHandle}
*/
function enterMdxjsEsm(token) {
this.enter({type: 'mdxjsEsm', value: ''}, token)
this.buffer() // Capture EOLs
}

/**
* @this {CompileContext}
* @type {FromMarkdownHandle}
*/
function exitMdxjsEsm(token) {
const value = this.resume()
const node = /** @type {MdxjsEsm} */ (this.exit(token))
/** @type {Program|undefined} */
// @ts-expect-error: custom.
const estree = token.estree

node.value = value

if (estree) {
node.data = {estree}
}
}

/**
* @this {CompileContext}
* @type {FromMarkdownHandle}
*/
function exitMdxjsEsmData(token) {
this.config.enter.data.call(this, token)
this.config.exit.data.call(this, token)
}

/**
* @type {ToMarkdownHandle}
* @param {MdxjsEsm} node
*/
function handleMdxjsEsm(node) {
return node.value || ''
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"main": "index.js",
"types": "index.d.ts",
"files": [
"lib/",
"complex-types.d.ts",
"index.d.ts",
"index.js"
Expand Down

0 comments on commit ebe8a5a

Please sign in to comment.