Skip to content

Commit

Permalink
Refactor to use @imports
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 5, 2024
1 parent 3819e96 commit 28b1f24
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules/
*.log
.DS_Store
yarn.lock
!/index.d.ts
8 changes: 8 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type {Options as MdxExpressionOptions} from 'micromark-extension-mdx-expression'

export {mdxjs} from './lib/index.js'

/**
* Configuration for `mdxjs` of `micromark-extension-mdxjs`.
*/
export interface Options extends MdxExpressionOptions {}
41 changes: 2 additions & 39 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,2 @@
/**
* @typedef {import('micromark-extension-mdx-expression').Options} Options
* @typedef {import('micromark-util-types').Extension} Extension
*/

import {Parser} from 'acorn'
import acornJsx from 'acorn-jsx'
import {mdxExpression} from 'micromark-extension-mdx-expression'
import {mdxJsx} from 'micromark-extension-mdx-jsx'
import {mdxMd} from 'micromark-extension-mdx-md'
import {mdxjsEsm} from 'micromark-extension-mdxjs-esm'
import {combineExtensions} from 'micromark-util-combine-extensions'

/**
* Create an extension for `micromark` to enable MDX syntax.
*
* @param {Options | null | undefined} [options]
* Configuration (optional).
* @returns {Extension}
* Extension for `micromark` that can be passed in `extensions` to enable MDX
* syntax.
*/
export function mdxjs(options) {
const settings = Object.assign(
{
acorn: Parser.extend(acornJsx()),
acornOptions: {ecmaVersion: 2024, sourceType: 'module'},
addResult: true
},
options
)

return combineExtensions([
mdxjsEsm(settings),
mdxExpression(settings),
mdxJsx(settings),
mdxMd()
])
}
// Note: types exposed from `index.d.ts`.
export {mdxjs} from './lib/index.js'
39 changes: 39 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @import {Options} from 'micromark-extension-mdxjs'
* @import {Extension} from 'micromark-util-types'
*/

import {Parser} from 'acorn'
import acornJsx from 'acorn-jsx'
import {mdxExpression} from 'micromark-extension-mdx-expression'
import {mdxJsx} from 'micromark-extension-mdx-jsx'
import {mdxMd} from 'micromark-extension-mdx-md'
import {mdxjsEsm} from 'micromark-extension-mdxjs-esm'
import {combineExtensions} from 'micromark-util-combine-extensions'

/**
* Create an extension for `micromark` to enable MDX syntax.
*
* @param {Options | null | undefined} [options]
* Configuration (optional).
* @returns {Extension}
* Extension for `micromark` that can be passed in `extensions` to enable MDX
* syntax.
*/
export function mdxjs(options) {
const settings = Object.assign(
{
acorn: Parser.extend(acornJsx()),
acornOptions: {ecmaVersion: 2024, sourceType: 'module'},
addResult: true
},
options
)

return combineExtensions([
mdxjsEsm(settings),
mdxExpression(settings),
mdxJsx(settings),
mdxMd()
])
}
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"type": "module",
"exports": "./index.js",
"files": [
"lib/",
"index.d.ts",
"index.js"
],
Expand Down Expand Up @@ -82,6 +83,31 @@
"strict": true
},
"xo": {
"overrides": [
{
"files": [
"**/*.d.ts"
],
"rules": {
"@typescript-eslint/array-type": [
"error",
{
"default": "generic"
}
],
"@typescript-eslint/ban-types": [
"error",
{
"extendDefaults": true
}
],
"@typescript-eslint/consistent-type-definitions": [
"error",
"interface"
]
}
}
],
"prettier": true
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"target": "es2022"
},
"exclude": ["coverage/", "node_modules/"],
"include": ["**/*.js"]
"include": ["**/*.js", "index.d.ts"]
}

0 comments on commit 28b1f24

Please sign in to comment.