micromark-extension-mdx-expression
micromark extension to support MDX or MDX JS expressions.
This package provides the low-level modules for integrating with the micromark
tokenizer but has no handling of compiling to HTML: go to a syntax tree instead.
You should use this with mdast-util-mdx-expression
(mdast).
Alternatively, use micromark-extension-mdx
with mdast-util-mdx
or
micromark-extension-mdxjs
with mdast-util-mdxjs
to support all of MDX (JS).
Or, use it through remark-mdx
or remark-mdxjs
(remark).
Install
npm:
npm install micromark-extension-mdx-expression
Use
See mdast-util-mdx-expression
for an example.
API
syntax(options?)
Support MDX (JS) expressions.
The export of syntax
is a function that can be called with options and returns
an extension for the micromark parser (to tokenize expressions; can be passed in
extensions
).
options
options.acorn
Acorn parser to use (Acorn
, optional).
options.acornOptions
Options to pass to acorn (Object
, default: {ecmaVersion: 2020, locations: true, sourceType: 'module'}
).
All fields except for locations
can be set.
options.addResult
Whether to add an estree
field to the mdxFlowExpression
and
mdxTextExpression
tokens with the results from acorn (boolean
, default:
false
).
Note that expressions can be empty or be just comments, in which cases estree
will be undefined.
Syntax
This extensions support both vanilla MDX and MDX JS.
The first is agnostic to the programming language (it could contain Rust or
so), the last is specific to JavaScript.
To turn on gnostic mode, pass acorn
.
There are two types of expressions: in text (inline, span) or in flow (block).
They start with {
.
Depending on whether acorn
is passed, expressions are either parsed in several
tries until whole JavaScript is found (as in, nested curly braces depend on JS
expression nesting), or they are counted and must be balanced.
Expressions end with }
.
For flow (block) expressions, optionally markdown spaces (
or \t
) can occur
after the closing brace, and finally a markdown line ending (\r
, \n
) or the
end of the file must follow.
While markdown typically knows no errors, for MDX it is decided to instead
throw on invalid syntax.
Here is an expression in a heading:
## Hello, {1 + 1}!
In agnostic mode, balanced braces can occur: {a + {b} + c}.
In gnostic mode, the value of the expression must be JavaScript, so
this would fail: {!}.
But, in gnostic mode, braces can be in comments, strings, or in other
places: {1 /* { */ + 2}.
The previous examples were text (inline, span) expressions, they can
also be flow (block):
{
1 + 1
}
This is incorrect, because there are further characters:
{
1 + 1
}!
Blank lines cannot occur in text, because markdown has already split them in
separate constructs, so this is incorrect: {1 +
1}
In flow, you can have blank lines:
{
1 +
2
}
Errors
Unexpected end of file in expression, expected a corresponding closing brace for {
This error occurs if a {
was seen without a }
(source:
micromark-extension-mdx-expression
, rule id: unexpected-eof
).
For example:
a { b
Could not parse expression with acorn: $error
This error occurs if acorn crashes (source: micromark-extension-mdx-expression
,
rule id: acorn
).
For example:
a {var b = "c"} d
Unexpected content after expression, expected }
This error occurs when there is more content after a JS expression (source:
micromark-extension-mdx-expression
, rule id: unexpected-content
).
For example:
a {"b" "c"} d
Tokens
Two tokens are used, mdxFlowExpression
and mdxTextExpression
, to reflect
flow and text expressions.
They include:
lineEnding
for the markdown line endings \r
, \n
, and \r\n
mdxFlowExpressionMarker
and mdxTextExpressionMarker
for the braceswhitespace
for markdown spaces and tabs in blank linesmdxFlowExpressionChunk
and mdxTextExpressionChunk
for chunks of
expression content
Related
remarkjs/remark
— markdown processor powered by pluginsmicromark/micromark
— the smallest commonmark-compliant markdown parser that existsmicromark/micromark-extension-mdx
— micromark extension to support all of MDXmicromark/micromark-extension-mdxjs
— micromark extension to support all of MDX JSmicromark/micromark-extension-mdx-jsx
— micromark extension to support the JSX of MDXmicromark/micromark-extension-mdxjs-esm
— micromark extension to support the import/exports of MDX JSmicromark/micromark-extension-mdx-md
— micromark extension to support the changes to markdown of MDXsyntax-tree/mdast-util-mdx
— mdast utility to support all of MDXsyntax-tree/mdast-util-mdxjs
— mdast utility to support all of MDX JSsyntax-tree/mdast-util-from-markdown
— mdast parser using micromark
to create mdast from markdownsyntax-tree/mdast-util-to-markdown
— mdast serializer to create markdown from mdast
Contribute
See contributing.md
in micromark/.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.
License
MIT © Titus Wormer