micromark extension to support MDX or MDX.js JSX
npm install micromark-extension-mdx-jsx[![Build][badge-build-image]][badge-build-url]
[![Coverage][badge-coverage-image]][badge-coverage-url]
[![Downloads][badge-downloads-image]][badge-downloads-url]
[![Size][badge-size-image]][badge-size-url]
[micromark][github-micromark] extension to support [MDX][mdxjs] JSX
().
* What is this?
* When to use this
* Install
* Use
* API
* mdxJsx(options?)
* Options
* Authoring
* Syntax
* Errors
* Unexpected end of file $at, expected $expect
* Unexpected character $at, expected $expect
* Unexpected lazy line in container, expected line to be…
* Tokens
* Types
* Compatibility
* Security
* Related
* Contribute
* License
This package contains an extension that adds support for the JSX syntax enabled
by [MDX][mdxjs] to [micromark][github-micromark].
These extensions are used inside MDX.
It mostly matches how JSX works in most places that support it
(TypeScript, Babel, esbuild, SWC, etc).
This package can be made aware or unaware of JavaScript syntax.
When unaware,
expressions could include Rust or variables or whatnot.
This project is useful when you want to support JSX in markdown.
You can use this extension when you are working with
[micromark][github-micromark].
To support all MDX features,
use [micromark-extension-mdxjs][github-micromark-extension-mdxjs] instead.
When you need a syntax tree,
combine this package with [mdast-util-mdx-jsx][github-mdast-util-mdx-jsx].
All these packages are used in [remark-mdx][mdxjs-remark-mdx],
which focusses on making it easier to transform content by abstracting these
internals away.
When you are using [mdx-js/mdx][mdxjs],
all of this is already included.
This package is [ESM only][github-gist-esm].
In Node.js (version 16+),
install with [npm][npmjs-install]:
``sh`
npm install micromark-extension-mdx-jsx
In Deno with [esm.sh][esmsh]:
`js`
import {mdxJsx} from 'https://esm.sh/micromark-extension-mdx-jsx@2'
In browsers with [esm.sh][esmsh]:
`html`
`js
import {micromark} from 'micromark'
import {mdxJsx} from 'micromark-extension-mdx-jsx'
const output = micromark('a f', {extensions: [mdxJsx()]})
console.log(output)
`
Yields:
` a fhtml`
…which is useless:
go to a syntax tree with
[mdast-util-from-markdown][github-mdast-util-from-markdown] andmdast-util-mdx-jsx
[][github-mdast-util-mdx-jsx] instead.
This package exports the identifier [mdxJsx][api-mdx-jsx].
There is no default export.
The export map supports the [development condition][nodejs-api-conditions].node --conditions development module.js
Run to get instrumented dev code.
Without this condition,
production code is loaded.
Create an extension for micromark to enable MDX JSX syntax.
###### Parameters
* optionsOptions
([][api-options], optional)
— configuration
###### Returns
Extension for micromark that can be passed in extensions to enable MDXExtension
JSX syntax ([][github-micromark-extension]).
Configuration (TypeScript type).
###### Fields
* acornAcorn
([][github-acorn], optional)acornOptions
— acorn parser to use
* AcornOptions
([][github-acorn-options],{ecmaVersion: 2024, locations: true, sourceType: 'module'}
default:
)locations
— configuration for acorn;
all fields except can be setaddResult
* boolean
(, default: false)estree
— whether to add fields to tokens with results from acorn
When authoring markdown with JSX,
keep in mind that MDX is a whitespace sensitive and line-based language,
while JavaScript is insensitive to whitespace.
This affects how markdown and JSX interleave with eachother in MDX.
For more info on how it works,
see [§ Interleaving][mdxjs-interleaving] on the MDX site.
###### Comments inside tags
JavaScript comments in JSX are not supported.
Incorrect:
`mdx-invalid`comment!//>/>
Correct:
`mdx`
A PR that adds support for them would be accepted.
###### Element or fragment attribute values
JSX elements or JSX fragments as attribute values are not supported.
The reason for this change is that it would be confusing whether markdown
would work.
Incorrect:
`mdx-invalid`
Correct:
`mdx`
###### Greater than (>) and right curly brace (})
JSX does not allow U+003E GREATER THAN (>) or U+007D RIGHT CURLY BRACE}
() literally in text,>
they need to be encoded as character references
(or expressions).
There is no good reason for this (some JSX parsers agree with us and don’t
crash either).
Therefore,
in MDX,
U+003E GREATER THAN () and U+007D RIGHT CURLY BRACE (}) are fine literally
and don’t need to be encoded.
JSX forms with the following BNF:
mdx_jsx_flow ::= mdx_jsx space_or_tab [mdx_jsx space_or_tab]space_or_tab | eol
mdx_jsx_text ::= mdx_jsx; constraint: markdown whitespace (
) is NOT<
; allowed directly afterin order to allow1 < 3in markdown.
mdx_jsx ::=
'<' [closing]
[*whitespace name [attributes_after_identifier] [closing]]
*whitespace '>'attributes_after_identifier ::=
1*whitespace (attributes_boolean | attributes_value) |
*whitespace attributes_expression |
attributes_after_value ::=
*whitespace (attributes_boolean | attributes_expression | attributes_value)
attributes_boolean ::= key [attributes_after_identifier]
; Note: in gnostic mode the value of the expression must instead be a single valid ES spread
; expression
attributes_expression ::= expression [attributes_after_value]
attributes_value ::= key initializer [attributes_after_value]closing ::= *whitespace '/'
name ::= identifier [local | members]
key ::= identifier [local]
local ::= whitespace ':' whitespace identifier
members ::= member *member
member ::= whitespace '.' whitespace identifieridentifier ::= identifier_start *identifier_part
initializer ::= whitespace '=' whitespace value
value ::= double_quoted | single_quoted | expression
; Note: in gnostic mode the value must instead be a single valid ES expression
expression ::= '{' *(expression_text | expression) '}'double_quoted ::= '"' *double_quoted_text '"'
single_quoted ::= "'" *single_quoted_text "'"whitespace ::= es_whitespace
double_quoted_text ::= char - '"'
single_quoted_text ::= char - "'"
expression_text ::= char - '{' - '}'
identifier_start ::= es_identifier_start
identifier_part ::= es_identifier_part | '-'space_or_tab ::= '\t' | ' '
eol ::= '\n' | '\r' | '\r\n'; ECMAScript
; See “IdentifierStart”: <https://tc39.es/ecma262/#prod-IdentifierStart>
es_identifier_start ::= ?
; See “IdentifierPart”: <https://tc39.es/ecma262/#prod-IdentifierPart>
es_identifier_part ::= ?
; See “Whitespace”: <https://tc39.es/ecma262/#prod-WhiteSpace>
es_whitespace ::= ?
As the flow construct occurs in flow,
like all flow constructs,
it must be followed by an eol (line ending) or eof (end of file).
The grammar for JSX in markdown is much stricter than that of HTML in
markdown.
The primary benefit of this is that tags are parsed into tokens,
and thus can be processed.
Another,
arguable,
benefit of this is that it comes with syntax errors:
if an author types something that is nonsensical,
an error is thrown with information about where it happened,
what occurred,
and what was expected instead.
This extension supports expressions both aware and unaware to JavaScript
(respectively gnostic and agnostic).
Depending on whether acorn is passed,
either valid JavaScript must be used in expressions,
or arbitrary text (such as Rust code or so) can be used.
More on this can be found in
[§ Syntax of micromark-extension-mdx-expression][github-expression-syntax].
In aware (gnostic) mode,
expressions are parsed with
[micromark-extension-mdx-expression][github-micromark-expression],
which throws some more errors.
This error occurs for many different reasons if something was opened but not
closed
(source: micromark-extension-mdx-jsx, rule id: unexpected-eof).
Some examples are:
This error occurs for many different reasons if an unexpected character is seen
(source: micromark-extension-mdx-jsx, rule id: unexpected-character).
Some examples are:
This error occurs if a < was seen in a container which then has lazy contentmicromark-extension-mdx-jsx
(source: , rule id: unexpected-lazy).
For example:
Many tokens are used:
* mdxJsxFlowTag for the whole JSX tag ()mdxJsxTextTag
* ^mdxJsxFlowTagMarker
* for the tag markers (<, >)mdxJsxTextTagMarker
* ^mdxJsxFlowTagClosingMarker
* for the / marking a closing tag ()mdxJsxTextTagClosingMarker
* ^mdxJsxFlowTagSelfClosingMarker
* for the / marking a self-closing tag
()mdxJsxTextTagSelfClosingMarker
* ^mdxJsxFlowTagName
* for the whole tag name (a:b in )mdxJsxTextTagName
* ^mdxJsxFlowTagNamePrimary
* for the first name (a in )mdxJsxTextTagNamePrimary
* ^mdxJsxFlowTagNameMemberMarker
* for the . marking in members ()mdxJsxTextTagNameMemberMarker
* ^mdxJsxFlowTagNameMember
* for member names (b in )mdxJsxTextTagNameMember
* ^mdxJsxFlowTagNamePrefixMarker
* for the : between primary and local
()mdxJsxTextTagNamePrefixMarker
* ^mdxJsxFlowTagNameLocal
* for the local name (b in )mdxJsxTextTagNameLocal
* ^mdxJsxFlowTagExpressionAttribute
* for whole expression attributes
()mdxJsxTextTagExpressionAttribute
* ^mdxJsxFlowTagExpressionAttributeMarker
* for {, } in expressionmdxJsxTextTagExpressionAttributeMarker
attributes
* ^mdxJsxFlowTagExpressionAttributeValue
* for chunks of what’s insidemdxJsxTextTagExpressionAttributeValue
expression attributes
* ^mdxJsxFlowTagAttribute
* for a whole normal attribute ()mdxJsxTextTagAttribute
* ^mdxJsxFlowTagAttributeName
* for the whole name of an attribute (b:c in
)mdxJsxTextTagAttributeName
* ^mdxJsxFlowTagAttributeNamePrimary
* for the first name of an attribute (b
in )mdxJsxTextTagAttributeNamePrimary
* ^mdxJsxFlowTagAttributeNamePrefixMarker
* for the : between primary and
local ()mdxJsxTextTagAttributeNamePrefixMarker
* ^mdxJsxFlowTagAttributeNameLocal
* for the local name of an attribute (c
in )mdxJsxTextTagAttributeNameLocal
* ^mdxJsxFlowTagAttributeInitializerMarker
* for the = between an attributemdxJsxTextTagAttributeInitializerMarker
name and value
* ^mdxJsxFlowTagAttributeValueLiteral
* for a string attribute value
()mdxJsxTextTagAttributeValueLiteral
* ^mdxJsxFlowTagAttributeValueLiteralMarker
* for the quotes around a string"
attribute value ( or ')mdxJsxTextTagAttributeValueLiteralMarker
* ^mdxJsxFlowTagAttributeValueLiteralValue
* for chunks of what’s insidemdxJsxTextTagAttributeValueLiteralValue
string attribute values
* ^mdxJsxFlowTagAttributeValueExpression
* for an expression attribute value
()mdxJsxTextTagAttributeValueExpression
* ^mdxJsxFlowTagAttributeValueExpressionMarker
* for the { and } ofmdxJsxTextTagAttributeValueExpressionMarker
expression attribute values
* ^mdxJsxFlowTagAttributeValueExpressionValue
* for chunks of what’s insidemdxJsxTextTagAttributeValueExpressionValue
expression attribute values
* ^
This package is fully typed with [TypeScript][].
It exports the additional type [Options][api-options].
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,
micromark-extension-mdx-jsx@2,
compatible with Node.js 16.
This package works with micromark version 3 and later.
This package is safe.
* [micromark-extension-mdxjs][github-micromark-extension-mdxjs]mdast-util-mdx-jsx
— support all MDX syntax
* [][github-mdast-util-mdx-jsx]remark-mdx
— support MDX JSX in mdast
* [][mdxjs-remark-mdx]
— support all MDX syntax in remark
See [contributing.md in micromark/.github][health-contributing] for wayssupport.md`][health-support] for ways to get help.
to get started.
See [
This project has a [code of conduct][health-coc].
By interacting with this repository,
organization,
or community you agree to abide by its terms.
[MIT][file-license] © [Titus Wormer][wooorm]
[api-mdx-jsx]: #mdxjsxoptions
[api-options]: #options
[badge-build-image]: https://github.com/micromark/micromark-extension-mdx-jsx/workflows/main/badge.svg
[badge-build-url]: https://github.com/micromark/micromark-extension-mdx-jsx/actions
[badge-coverage-image]: https://img.shields.io/codecov/c/github/micromark/micromark-extension-mdx-jsx.svg
[badge-coverage-url]: https://codecov.io/github/micromark/micromark-extension-mdx-jsx
[badge-downloads-image]: https://img.shields.io/npm/dm/micromark-extension-mdx-jsx.svg
[badge-downloads-url]: https://www.npmjs.com/package/micromark-extension-mdx-jsx
[badge-size-image]: https://img.shields.io/bundlejs/size/micromark-extension-mdx-jsx
[badge-size-url]: https://bundlejs.com/?q=micromark-extension-mdx-jsx
[esmsh]: https://esm.sh
[file-license]: license
[github-acorn]: https://github.com/acornjs/acorn
[github-acorn-options]: https://github.com/acornjs/acorn/blob/96c721dbf89d0ccc3a8c7f39e69ef2a6a3c04dfa/acorn/dist/acorn.d.ts#L16
[github-expression-syntax]: https://github.com/micromark/micromark-extension-mdx-expression/blob/main/packages/micromark-extension-mdx-expression/readme.md#syntax
[github-gist-esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[github-mdast-util-from-markdown]: https://github.com/syntax-tree/mdast-util-from-markdown
[github-mdast-util-mdx-jsx]: https://github.com/syntax-tree/mdast-util-mdx-jsx
[github-micromark]: https://github.com/micromark/micromark
[github-micromark-expression]: https://github.com/micromark/micromark-extension-mdx-expression
[github-micromark-extension]: https://github.com/micromark/micromark#syntaxextension
[github-micromark-extension-mdxjs]: https://github.com/micromark/micromark-extension-mdxjs
[health-coc]: https://github.com/micromark/.github/blob/main/code-of-conduct.md
[health-contributing]: https://github.com/micromark/.github/blob/main/contributing.md
[health-support]: https://github.com/micromark/.github/blob/main/support.md
[mdxjs]: https://mdxjs.com
[mdxjs-interleaving]: https://mdxjs.com/docs/what-is-mdx/#interleaving
[mdxjs-remark-mdx]: https://mdxjs.com/packages/remark-mdx/
[nodejs-api-conditions]: https://nodejs.org/api/packages.html#packages_resolving_user_conditions
[npmjs-install]: https://docs.npmjs.com/cli/install
[typescript]: https://www.typescriptlang.org
[wooorm]: https://wooorm.com