remark-mdx
!Buildbuild-badgebuild
!Coveragecoverage-badgecoverage
!Downloadsdownloads-badgedownloads
!Sizesize-badgesize
!Sponsorssponsors-badgecollective
!Backersbackers-badgecollective
!Chatchat-badgechatremark plugin to support the MDX syntax (JSX, export/import, expressions).
Contents
unified().use(remarkMdx[, options])
Options
What is this?
This package is a unified (remark) plugin to enable the extensions to markdown that MDX adds: JSX (<x/>
), export/import (export x from 'y'
), and
expression {{1 + 1}
}.
You can use this plugin to add support for parsing and serializing them.This plugin does not handle how MDX is compiled to JavaScript or evaluated and rendered to HTML. That’s done by
@mdx-js/mdx
mdx.When should I use this?
This plugin is useful if you’re dealing with the MDX syntax and integrating with remark, rehype, and the rest of unified. Some example use cases are when you want to lint the syntax or compile it to something other that JavaScript.If you don’t use plugins and want to access the syntax tree, you can use
mdast-util-from-markdown
mdast-util-from-markdown with
mdast-util-mdx
mdast-util-mdx.Typically though, you’d want to move a layer up:
@mdx-js/mdx
.
That package is the core compiler for turning MDX into JavaScript which
gives you the most control.
Or even higher: if you’re using a bundler (Rollup, esbuild, webpack), or a site
builder (Next.js) or build system (Vite) which comes with a bundler, you’re
better off using an integration: see § Integrationsintegrations.Install
This package is ESM onlyesm. In Node.js (version 16+), install with npm:npm install remark-mdx
In Deno with
esm.sh
esmsh:import remarkMdx from 'https://esm.sh/remark-mdx@3'
In browsers with
esm.sh
esmsh:<script type="module">
import remarkMdx from 'https://esm.sh/remark-mdx@3?bundle'
</script>
Use
import {remark} from 'remark'
import remarkMdx from 'remark-mdx'
const file = await remark()
.use(remarkMdx)
.process('import a from "b"\n\na <b /> c {1 + 1} d')
console.log(String(file))
Yields:
import a from "b"
a <b/> c {1 + 1} d
API
This package exports no identifiers. The default export isremarkMdx
api-remark-mdx.unified().use(remarkMdx[, options])
Add support for MDX (JSX: <Video id={123} />
, export/imports: `export {x}
from 'y'; and expressions:
{1 + 1}`).Parameters
options
(Options
api-options, optional)
Returns
Nothing (undefined
).Options
Configuration (TypeScript type).Fields
acornOptions
(AcornOptions
acorn-options, default:
{ecmaVersion: 2024, locations: true, sourceType: 'module'}
)
— configuration for acorn; all fields except locations
can be setprintWidth
(number
, default:Infinity
)
80
), the formatter will print
attributes on separate lines when a tag doesn’t fit on one line;
the normal behavior is to print attributes with spaces between them instead
of line endingsquote
('"'
or"'"
, default:'"'
)
quoteSmart
(boolean
, default:false
)
tightSelfClosing
(boolean
, default:false
)
<img/>
instead of <img />
Authoring
For recommendations on how to author MDX, see each corresponding readme:HTML
MDX has no representation in HTML. Though, when you are dealing with MDX, you will likely go through hast. You can enable passing MDX through to hast by configuringremark-rehype
remark-rehype with `passThrough: 'mdxjsEsm',
'mdxFlowExpression', 'mdxJsxFlowElement', 'mdxJsxTextElement', 'mdxTextExpression'`.Syntax
For info on the syntax of these features, see each corresponding readme:- ESM
- JSX
- expressions
- CommonMark features not in MDX: n/a
Syntax tree
For info on the syntax tree of these features, see each corresponding readme:- ESM
- JSX
- expressions
- CommonMark features not in MDX: n/a
Errors
For info on what errors are thrown, see each corresponding readme:- ESM
- JSX
- expressions
- CommonMark features not in MDX: n/a
Types
This package is fully typed with TypeScript. It exports the additional typeOptions
api-options.If you’re working with the syntax tree, you can register the new node types with
@types/mdast
by adding a reference:// Register MDX nodes in mdast:
/// <reference types="remark-mdx" />
import {visit} from 'unist-util-visit'
function myRemarkPlugin() {
/**
* @param {import('mdast').Root} tree
* Tree.
* @returns {undefined}
* Nothing.
*/
return function (tree) {
visit(tree, function (node) {
console.log(node) // `node` can now be one of the MDX nodes.
})
}
}
Compatibility
Projects maintained by the unified collective are compatible with maintained versions of Node.js.When we cut a new major release, we drop support for unmaintained versions of Node. This means we try to keep the current release line,
remark-mdx@^3
, compatible
with Node.js 16.Security
See § Securitysecurity on our website for information.Contribute
See § Contributecontribute on our website for ways to get started. See § Supportsupport for ways to get help.This project has a code of conductcoc. By interacting with this repository, organization, or community you agree to abide by its terms.