MDX Export Definition Utility
unist-util-mdx-define is a utility for the Unist ecosystem designed to simplify the process of defining exports within an MDX Abstract Syntax Tree (AST). It abstracts away the complexities of AST manipulation, allowing developers to easily expose variables from remark (mdast), rehype (hast), or recma (estree/esast) plugins. As of the current stable version 1.1.2, the package maintains a steady release cadence for minor and patch updates, focusing on robustness and compatibility within the `unified` and `MDX.js` ecosystems. Its key differentiator is its ability to uniformly inject export declarations across different stages of the MDX compilation pipeline (mdast, hast, estree), ensuring that variables defined at any stage are correctly available in the final MDX module without requiring manual AST traversal or complex insertion logic.
Common errors
-
Error: Cannot define variable 'exports' because it conflicts with an MDX internal.
cause Attempting to define a variable with a name reserved by MDX internals.fixChoose a different variable name that does not conflict with MDX's internal keywords or exported symbols. -
ReferenceError: myVariable is not defined
cause Trying to use a variable defined via `unist-util-mdx-define` in generated JavaScript code within the same plugin context, rather than in user-written MDX expressions.fixVariables defined by `unist-util-mdx-define` are intended for use by user-defined MDX expressions. If you need to pass data between plugin stages, consider `file.data` or defining variables at a later stage (like recma) if internal generated code needs access.
Warnings
- breaking As of v1.1.0, attempting to define a variable with a name that conflicts with MDX internal identifiers will now throw an error. Previously, this might have resulted in silent failures or unexpected behavior.
- gotcha When using `unist-util-mdx-define` with mdast (remark) or hast (rehype) ASTs, the variable declarations are prepended to the root. While this ensures user-defined MDX expressions can use these variables, generated expressions within the same plugin are not guaranteed to be able to reference other variables defined by `unist-util-mdx-define`.
- gotcha Version 1.1.0 introduced improved handling for invalid identifier names. While this prevents errors, ensure that the keys in the object passed to `define` conform to valid JavaScript identifier rules to avoid unexpected behavior or unaccessible variables.
Install
-
npm install unist-util-mdx-define -
yarn add unist-util-mdx-define -
pnpm add unist-util-mdx-define
Imports
- define
const { define } = require('unist-util-mdx-define')import { define } from 'unist-util-mdx-define' - Plugin
import { type Plugin } from 'unified' - mdast.Root
import type * as mdast from 'mdast'
Quickstart
import { compile } from '@mdx-js/mdx'
import type * as estree from 'estree'
import type * as hast from 'hast'
import type * as mdast from 'mdast'
import { type Plugin } from 'unified'
import { define } from 'unist-util-mdx-define'
const yourRemarkMdxPlugin: Plugin<[], mdast.Root> = () => (ast, file) => {
define(ast, file, { remarkVariable: { type: 'Literal', value: 'Hello remark plugin!' } })
}
const yourRehypeMdxPlugin: Plugin<[], hast.Root> = () => (ast, file) => {
define(ast, file, { rehypeVariable: { type: 'Literal', value: 'Hello rehype plugin!' } })
}
const yourRecmaMdxPlugin: Plugin<[], estree.Program> = () => (ast, file) => {
define(ast, file, { recmaVariable: { type: 'Literal', value: 'Hello recma plugin!' } })
}
async function runExample() {
const { value } = await compile('{remarkVariable} {rehypeVariable} {recmaVariable}', {
remarkPlugins: [yourRemarkMdxPlugin],
rehypePlugins: [yourRehypeMdxPlugin],
recmaPlugins: [yourRecmaMdxPlugin]
})
console.log(value)
}
runExample()