Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
mdast-util-mdx-jsx
Advanced tools
The mdast-util-mdx-jsx package is a utility for working with MDX (Markdown for the component era) JSX nodes within the mdast (Markdown Abstract Syntax Tree) format. It allows for parsing, manipulating, and serializing MDX JSX nodes in the context of mdast, making it a powerful tool for developers working with MDX content in JavaScript and TypeScript projects.
Parsing MDX JSX nodes
This feature allows for the parsing of MDX JSX nodes from a Markdown string. The code sample demonstrates how to use the package in conjunction with `mdast-util-from-markdown` to parse a string containing an MDX JSX component into an mdast syntax tree.
"use strict";
var fromMarkdown = require('mdast-util-from-markdown');
var mdxJsx = require('mdast-util-mdx-jsx');
var doc = '<Button>Click me!</Button>';
var tree = fromMarkdown(doc, {
extensions: [mdxJsx.fromMarkdown()],
});
console.log(tree);
Serializing MDX JSX nodes
This feature enables the serialization of MDX JSX nodes back into a Markdown string with embedded JSX. The code sample shows how to convert an mdast syntax tree containing an MDX JSX element back into a Markdown string using `mdast-util-to-markdown` with the mdx-jsx extension.
"use strict";
var toMarkdown = require('mdast-util-to-markdown');
var mdxJsx = require('mdast-util-mdx-jsx');
var tree = {
type: 'root',
children: [
{
type: 'mdxJsxFlowElement',
name: 'Button',
children: [{type: 'text', value: 'Click me!'}]
}
]
};
var doc = toMarkdown(tree, {
extensions: [mdxJsx.toMarkdown()],
});
console.log(doc);
remark-mdx is a plugin for the remark Markdown processor that adds support for parsing and serializing MDX. It is similar to mdast-util-mdx-jsx in that it deals with MDX content, but it operates within the remark ecosystem, providing a higher-level abstraction compared to the lower-level mdast utilities.
rehype-react is a plugin that allows you to transform rehype (HTML AST) trees to React components. While it operates in a different part of the unified ecosystem, focusing on HTML rather than Markdown, it shares the concept of transforming syntax trees (in this case, HTML ASTs) into JSX components, similar to how mdast-util-mdx-jsx works with MDX JSX nodes.
mdast extensions to parse and serialize MDX JSX (<a />
).
This package contains two extensions that add support for MDX JSX syntax in
markdown to mdast.
These extensions plug into
mdast-util-from-markdown
(to support parsing
JSX in markdown into a syntax tree) and
mdast-util-to-markdown
(to support serializing
JSX in syntax trees to markdown).
JSX is an XML-like syntax extension to ECMAScript (JavaScript), which MDX brings to markdown. For more info on MDX, see What is MDX?
You can use these extensions when you are working with
mdast-util-from-markdown
and mdast-util-to-markdown
already.
When working with mdast-util-from-markdown
, you must combine this package
with micromark-extension-mdx-jsx
.
When you are working with syntax trees and want all of MDX, use
mdast-util-mdx
instead.
All these packages are used in remark-mdx
, which
focusses on making it easier to transform content by abstracting these
internals away.
This package is ESM only. In Node.js (version 16+), install with npm:
npm install mdast-util-mdx-jsx
In Deno with esm.sh
:
import {mdxJsxFromMarkdown, mdxJsxToMarkdown} from 'https://esm.sh/mdast-util-mdx-jsx@3'
In browsers with esm.sh
:
<script type="module">
import {mdxJsxFromMarkdown, mdxJsxToMarkdown} from 'https://esm.sh/mdast-util-mdx-jsx@3?bundle'
</script>
Say our document example.mdx
contains:
<Box>
- a list
</Box>
<MyComponent {...props} />
<abbr title="Hypertext Markup Language">HTML</abbr> is a lovely language.
…and our module example.js
looks as follows:
import fs from 'node:fs/promises'
import * as acorn from 'acorn'
import {mdxJsx} from 'micromark-extension-mdx-jsx'
import {fromMarkdown} from 'mdast-util-from-markdown'
import {mdxJsxFromMarkdown, mdxJsxToMarkdown} from 'mdast-util-mdx-jsx'
import {toMarkdown} from 'mdast-util-to-markdown'
const doc = await fs.readFile('example.mdx')
const tree = fromMarkdown(doc, {
extensions: [mdxJsx({acorn, addResult: true})],
mdastExtensions: [mdxJsxFromMarkdown()]
})
console.log(tree)
const out = toMarkdown(tree, {extensions: [mdxJsxToMarkdown()]})
console.log(out)
…now running node example.js
yields (positional info removed for brevity):
{
type: 'root',
children: [
{
type: 'mdxJsxFlowElement',
name: 'Box',
attributes: [],
children: [
{
type: 'list',
ordered: false,
start: null,
spread: false,
children: [
{
type: 'listItem',
spread: false,
checked: null,
children: [
{type: 'paragraph', children: [{type: 'text', value: 'a list'}]}
]
}
]
}
]
},
{
type: 'mdxJsxFlowElement',
name: 'MyComponent',
attributes: [
{
type: 'mdxJsxExpressionAttribute',
value: '...props',
data: {
estree: {
type: 'Program',
body: [
{
type: 'ExpressionStatement',
expression: {
type: 'ObjectExpression',
properties: [
{
type: 'SpreadElement',
argument: {type: 'Identifier', name: 'props'}
}
]
}
}
],
sourceType: 'module'
}
}
}
],
children: []
},
{
type: 'paragraph',
children: [
{
type: 'mdxJsxTextElement',
name: 'abbr',
attributes: [
{
type: 'mdxJsxAttribute',
name: 'title',
value: 'Hypertext Markup Language'
}
],
children: [{type: 'text', value: 'HTML'}]
},
{type: 'text', value: ' is a lovely language.'}
]
}
]
}
<Box>
* a list
</Box>
<MyComponent {...props} />
<abbr title="Hypertext Markup Language">HTML</abbr> is a lovely language.
This package exports the identifiers
mdxJsxFromMarkdown
and
mdxJsxToMarkdown
.
There is no default export.
mdxJsxFromMarkdown()
Create an extension for
mdast-util-from-markdown
to enable MDX JSX.
Extension for mdast-util-from-markdown
to enable MDX JSX
(FromMarkdownExtension
).
When using the micromark syntax extension with
addResult
, nodes will have a data.estree
field set to an ESTree
Program
node.
mdxJsxToMarkdown(options?)
Create an extension for
mdast-util-to-markdown
to enable MDX JSX.
This extension configures mdast-util-to-markdown
with
options.fences: true
and
options.resourceLink: true
too, do not
overwrite them!
options
(ToMarkdownOptions
)
— configurationExtension for mdast-util-to-markdown
to enable MDX JSX
(FromMarkdownExtension
).
MdxJsxAttribute
MDX JSX attribute with a key (TypeScript type).
import type {Literal} from 'mdast'
interface MdxJsxAttribute extends Literal {
type: 'mdxJsxAttribute'
name: string
value?: MdxJsxAttributeValueExpression | string | null | undefined
}
MdxJsxAttributeValueExpression
MDX JSX attribute value set to an expression (TypeScript type).
import type {Program} from 'estree-jsx'
import type {Literal} from 'mdast'
interface MdxJsxAttributeValueExpression extends Literal {
type: 'mdxJsxAttributeValueExpression'
data?: {estree?: Program | null | undefined} & Literal['data']
}
MdxJsxExpressionAttribute
MDX JSX attribute as an expression (TypeScript type).
import type {Program} from 'estree-jsx'
import type {Literal} from 'mdast'
interface MdxJsxExpressionAttribute extends Literal {
type: 'mdxJsxExpressionAttribute'
data?: {estree?: Program | null | undefined} & Literal['data']
}
MdxJsxFlowElement
MDX JSX element node, occurring in flow (block) (TypeScript type).
import type {BlockContent, DefinitionContent, Parent} from 'mdast'
export interface MdxJsxFlowElement extends Parent {
type: 'mdxJsxFlowElement'
name: string | null
attributes: Array<MdxJsxAttribute | MdxJsxExpressionAttribute>
children: Array<BlockContent | DefinitionContent>
}
MdxJsxFlowElementHast
Same as MdxJsxFlowElement
, but registered with
@types/hast
(TypeScript type).
import type {ElementContent, Parent} from 'hast'
export interface MdxJsxFlowElementHast extends Parent {
type: 'mdxJsxFlowElement'
name: string | null
attributes: Array<MdxJsxAttribute | MdxJsxExpressionAttribute>
children: Array<ElementContent>
}
MdxJsxTextElement
MDX JSX element node, occurring in text (phrasing) (TypeScript type).
import type {Parent, PhrasingContent} from 'mdast'
export interface MdxJsxTextElement extends Parent {
type: 'mdxJsxTextElement'
name: string | null
attributes: Array<MdxJsxAttribute | MdxJsxExpressionAttribute>
children: Array<PhrasingContent>
}
MdxJsxTextElementHast
Same as MdxJsxTextElement
, but registered with
@types/hast
(TypeScript type).
import type {ElementContent, Parent} from 'hast'
export interface MdxJsxTextElementHast extends Parent {
type: 'mdxJsxTextElement'
name: string | null
attributes: Array<MdxJsxAttribute | MdxJsxExpressionAttribute>
children: Array<ElementContent>
}
ToMarkdownOptions
Configuration (TypeScript type).
quote
('"'
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 />
printWidth
(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 endingsMDX JSX has no representation in HTML.
Though, when you are dealing with MDX, you will likely go through hast.
You can enable passing MDX JSX through to hast by configuring
mdast-util-to-hast
with
passThrough: ['mdxJsxFlowElement', 'mdxJsxTextElement']
.
See Syntax in micromark-extension-mdx-jsx
.
The following interfaces are added to mdast by this utility.
MdxJsxFlowElement
interface MdxJsxFlowElement <: Parent {
type: 'mdxJsxFlowElement'
}
MdxJsxFlowElement includes MdxJsxElement
MdxJsxFlowElement (Parent) represents JSX in flow (block). It can be used where flow content is expected. It includes the mixin MdxJsxElement.
For example, the following markdown:
<w x="y">
z
</w>
Yields:
{
type: 'mdxJsxFlowElement',
name: 'w',
attributes: [{type: 'mdxJsxAttribute', name: 'x', value: 'y'}],
children: [{type: 'paragraph', children: [{type: 'text', value: 'z'}]}]
}
MdxJsxTextElement
interface MdxJsxTextElement <: Parent {
type: 'mdxJsxTextElement'
}
MdxJsxTextElement includes MdxJsxElement
MdxJsxTextElement (Parent) represents JSX in text (span, inline). It can be used where phrasing content is expected. It includes the mixin MdxJsxElement.
For example, the following markdown:
a <b c>d</b> e.
Yields:
{
type: 'mdxJsxTextElement',
name: 'b',
attributes: [{type: 'mdxJsxAttribute', name: 'c', value: null}],
children: [{type: 'text', value: 'd'}]
}
MdxJsxElement
interface mixin MdxJsxElement {
name: string?
attributes: [MdxJsxExpressionAttribute | MdxJsxAttribute]
}
interface MdxJsxExpressionAttribute <: Literal {
type: 'mdxJsxExpressionAttribute'
}
interface MdxJsxAttribute <: Node {
type: 'mdxJsxAttribute'
name: string
value: MdxJsxAttributeValueExpression | string?
}
interface MdxJsxAttributeValueExpression <: Literal {
type: 'mdxJsxAttributeValueExpression'
}
MdxJsxElement represents a JSX element.
The name
field can be present and represents an identifier.
Without name
, the element represents a fragment, in which case no attributes
must be present.
The attributes
field represents information associated with the node.
The value of the attributes
field is a list of MdxJsxExpressionAttribute
and MdxJsxAttribute nodes.
MdxJsxExpressionAttribute represents an expression (typically in a programming language) that when evaluated results in multiple attributes.
MdxJsxAttribute represents a single attribute.
The name
field must be present.
The value
field can be present, in which case it is either a string (a static
value) or an expression (typically in a programming language) that when
evaluated results in an attribute value.
FlowContent
(MDX JSX)type MdxJsxFlowContent = MdxJsxFlowElement | FlowContent
PhrasingContent
(MDX JSX)type MdxJsxPhrasingContent = MdxJsxTextElement | PhrasingContent
This package is fully typed with TypeScript.
It exports the additional types MdxJsxAttribute
,
MdxJsxAttributeValueExpression
,
MdxJsxExpressionAttribute
,
MdxJsxFlowElement
,
MdxJsxFlowElementHast
,
MdxJsxTextElement
,
MdxJsxTextElementHast
, and
ToMarkdownOptions
.
It also registers the node types with @types/mdast
and @types/hast
.
If you’re working with the syntax tree, make sure to import this utility
somewhere in your types, as that registers the new node types in the tree.
/**
* @import {} from 'mdast-util-mdx-jsx'
* @import {Root} from 'mdast'
*/
import {visit} from 'unist-util-visit'
/** @type {Root} */
const tree = getMdastNodeSomeHow()
visit(tree, function (node) {
// `node` can now be one of the JSX 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, mdast-util-mdx-jsx@3
,
compatible with Node.js 16.
This utility works with mdast-util-from-markdown
version 2+ and
mdast-util-to-markdown
version 2+.
micromark/micromark-extension-mdx-jsx
— support MDX JSX in micromarksyntax-tree/mdast-util-mdx
— support MDX in mdastremarkjs/remark-mdx
— support MDX in remarkSee contributing.md
in syntax-tree/.github
for
ways to get started.
See support.md
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
mdast extension to parse and serialize MDX or MDX.js JSX
We found that mdast-util-mdx-jsx demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.