Security News
New Proposed CISA Mandate Would Require Critical Infrastructure to Report Ransom Payments Within 24 Hours
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
remark-mdx
Advanced tools
remark plugin to support MDX syntax
Weekly downloads
Package description
The remark-mdx npm package is a plugin for remark, a markdown processor powered by plugins part of the unified collective. It enables MDX syntax in markdown documents processed by remark. MDX is an authorable format that lets you seamlessly write JSX in your markdown documents. This package allows you to parse and serialize MDX files, making it possible to use MDX with the remark ecosystem.
Parsing MDX syntax
This feature allows you to parse strings of MDX and transform them into a markdown abstract syntax tree (AST).
const remark = require('remark');
const remarkMdx = require('remark-mdx');
remark().use(remarkMdx).processSync('<h1>Hello, MDX!</h1>').toString();
Serializing MDX
This feature enables you to serialize the markdown AST back into a string of MDX, which can then be saved to a file or processed further.
const remark = require('remark');
const remarkMdx = require('remark-mdx');
const vfile = require('to-vfile');
const file = vfile.readSync('example.mdx');
const result = remark().use(remarkMdx).processSync(file).toString();
console.log(result);
Remark is the core library that powers the remark ecosystem, which includes remark-mdx. It is a markdown processor that can be extended with plugins but does not support MDX syntax out of the box.
MDX is a full-fledged JSX-in-markdown implementation that goes beyond what remark-mdx offers by providing a complete ecosystem for working with MDX, including compiling to JavaScript, handling imports, and providing custom components.
Rehype is another processor in the unified collective that works with HTML instead of markdown. It can be used in conjunction with remark-mdx to transform the markdown AST into a rehype (HTML) AST and manipulate it further.
Readme
remark plugin to support the MDX syntax (JSX, export/import, expressions).
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
.
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
with
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 § Integrations.
This package is ESM only. In Node.js (version 16+), install with npm:
npm install remark-mdx
In Deno with esm.sh
:
import remarkMdx from 'https://esm.sh/remark-mdx@3'
In browsers with esm.sh
:
<script type="module">
import remarkMdx from 'https://esm.sh/remark-mdx@3?bundle'
</script>
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
This package exports no identifiers.
The default export is remarkMdx
.
unified().use(remarkMdx[, options])
Add support for MDX (JSX: <Video id={123} />
, export/imports: export {x} from 'y'
; and expressions: {1 + 1}
).
options
(Options
, optional)
— configurationNothing (undefined
).
Options
Configuration (TypeScript type).
acornOptions
(AcornOptions
, default:
{ecmaVersion: 2024, locations: true, sourceType: 'module'}
)
— configuration for acorn; all fields except locations
can be setprintWidth
(number
, default: Infinity
)
— try and wrap syntax at this width;
when set to a finite number (say, 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: '"'
)
— preferred quote to use around attribute valuesquoteSmart
(boolean
, default: false
)
— use the other quote if that results in less bytestightSelfClosing
(boolean
, default: false
)
— do not use an extra space when closing self-closing elements: <img/>
instead of <img />
For recommendations on how to author MDX, see each corresponding readme:
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 configuring
remark-rehype
with passThrough: ['mdxjsEsm', 'mdxFlowExpression', 'mdxJsxFlowElement', 'mdxJsxTextElement', 'mdxTextExpression']
.
For info on the syntax of these features, see each corresponding readme:
For info on the syntax tree of these features, see each corresponding readme:
For info on what errors are thrown, see each corresponding readme:
This package is fully typed with TypeScript.
It exports the additional type 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.
})
}
}
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.
See § Security on our website for information.
See § Contribute on our website for ways to get started. See § Support for ways to get help.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.
FAQs
remark plugin to support MDX syntax
The npm package remark-mdx receives a total of 2,414,246 weekly downloads. As such, remark-mdx popularity was classified as popular.
We found that remark-mdx demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
Security News
Redis is no longer OSS, breaking its explicit commitment to remain under the BSD 3-Clause License forever. This has angered contributors who are now working to fork the software.
Product
Socket AI now enables 'AI detected potential malware' alerts by default, ensuring users benefit from AI-powered state-of-the-art malware detection without needing to opt-in.