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.
npm install unist-util-mdx-defineVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to use `unist-util-mdx-define` within `remark`, `rehype`, and `recma` MDX plugins to export variables, which are then accessible in the MDX content.
Ensure that the variable names you define do not clash with reserved MDX keywords or internal identifiers. Check your `define` calls for potential conflicts.
If your generated expressions require access to other defined variables, consider restructuring your plugin logic or defining variables at a later stage (e.g., recma) where the module scope is more fully resolved.
Use valid JavaScript identifier names for all keys in the object passed as the third argument to the `define` function (e.g., `myVariable`, not `my-variable` or `1variable`).
Choose a different variable name that does not conflict with MDX's internal keywords or exported symbols.
Variables 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.
No dependency data recorded yet.