Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
micromark-extension-mdx-expression
Advanced tools
micromark extension to support MDX or MDX JS expressions
The micromark-extension-mdx-expression npm package is designed to extend the micromark Markdown parser to support MDX expressions. This allows developers to embed JSX-like expressions within their Markdown content, which is particularly useful when using MDX to create interactive documentation or components within Markdown files.
Parsing Inline Expressions
This feature allows the parsing of inline expressions embedded within curly braces. It is useful for inserting dynamic content directly into the text.
{`Hello, ${name}!`}
Parsing Block Expressions
This feature supports the use of block expressions, where more complex JavaScript logic can be embedded within Markdown, enabling the creation of dynamic and interactive content blocks.
{
const name = 'World';
return <div>Hello, {name}!</div>;
}
This package is similar to micromark-extension-mdx-expression as it also allows MDX capabilities in Markdown files. However, remark-mdx is built on top of the remark parser, which is a different Markdown parser compared to micromark used by micromark-extension-mdx-expression. This difference in underlying parsers can lead to variations in parsing behavior and plugin compatibility.
micromark extension to support MDX expressions ({2 * Math.PI}
).
This project contains three packages (it’s a monorepo).
micromark-extension-mdx-expression
— extension that adds support for expressions enabled by MDX to
micromark
micromark-factory-mdx-expression
— subroutine to parse the expressionsmicromark-util-events-to-acorn
— subroutine to turn parse micromark events with acornThis readme will focus on micromark-extension-mdx-expression
.
These tools are all low-level.
In many cases, you want to use remark-mdx
with remark instead.
When you are using mdx-js/mdx
, that is already included.
Even when you want to use micromark
, you likely want to use
micromark-extension-mdxjs
to support all MDX
features.
That extension includes this extension.
When working with mdast-util-from-markdown
, you
must combine this package with
mdast-util-mdx-expression
.
This package is ESM only. In Node.js (version 12.20+, 14.14+, 16.0+, or 18.0+), install with npm:
npm install micromark-extension-mdx-expression
In Deno with esm.sh
:
import {mdxExpression} from 'https://esm.sh/micromark-extension-mdx-expression@1'
In browsers with esm.sh
:
<script type="module">
import {mdxExpression} from 'https://esm.sh/micromark-extension-mdx-expression@1?bundle'
</script>
import * as acorn from 'acorn'
import {micromark} from 'micromark'
import {mdxExpression} from 'micromark-extension-mdx-expression'
// Agnostic (balanced braces):
const output = micromark('a {1 + 1} b', {extensions: [mdxExpression()]})
console.log(output)
// Gnostic (JavaScript):
micromark('a {!} b', {extensions: [mdxExpression({acorn})]})
Yields:
<p>a b</p>
[1:5: Could not parse expression with acorn: Unexpected token] {
reason: 'Could not parse expression with acorn: Unexpected token',
line: 1,
column: 5,
source: 'micromark-extension-mdx-expression',
ruleId: 'acorn',
position: {
start: { line: 1, column: 5, offset: 4 },
end: { line: null, column: null }
}
}
…which is useless: go to a syntax tree with
mdast-util-from-markdown
and
mdast-util-mdx-expression
instead.
This package exports the identifier mdxExpression
.
There is no default export.
The export map supports the endorsed development
condition.
Run node --conditions development module.js
to get instrumented dev code.
Without this condition, production code is loaded.
mdxExpression(options?)
Add support for MDX expressions.
Function called optionally with options to get a syntax extension for micromark
(passed in extensions
).
options
Configuration (optional).
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 can be set.
Positional info (loc
, range
) is set on ES nodes regardless of acorn options.
options.addResult
Whether to add an estree
field to mdxFlowExpression
and mdxTextExpression
tokens with the results from acorn (boolean
, default: false
).
Note that expressions can be empty or be just comments, in which case estree
will be undefined.
When authoring markdown with JavaScript, keep in mind that MDX is a whitespace sensitive and line-based language, while JavaScript is insensitive to whitespace. This affects how markdown and JavaScript interleave with eachother in MDX. For more info on how it works, see § Interleaving on the MDX site.
This extension supports MDX both agnostic and gnostic to JavaScript. Depending on whether acorn is passed, either valid JavaScript must be used in expressions, or arbitrary text could (such as Rust or so) can be used.
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
}
{
This error occurs if a {
was seen without a }
(source:
micromark-extension-mdx-expression
, rule id: unexpected-eof
).
For example:
a { b
This error occurs when there is more content after a JS expression (source:
micromark-extension-mdx-expression
, rule id: acorn
).
For example:
a {"b" "c"} d
This error occurs if acorn crashes (source: micromark-extension-mdx-expression
,
rule id: acorn
).
For example:
a {var b = "c"} d
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 contentThis package is fully typed with TypeScript.
It exports the additional type Options
.
This package is at least compatible with all maintained versions of Node.js. As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+. It also works in Deno and modern browsers.
This package deals with compiling JavaScript. If you do not trust the JavaScript, this package does nothing to change that.
micromark/micromark-extension-mdxjs
— micromark extension to support MDXsyntax-tree/mdast-util-mdx-expression
— mdast utility to support MDX expressionsremark-mdx
— remark plugin to support MDX syntaxSee 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.
FAQs
micromark extension to support MDX or MDX JS expressions
The npm package micromark-extension-mdx-expression receives a total of 1,420,278 weekly downloads. As such, micromark-extension-mdx-expression popularity was classified as popular.
We found that micromark-extension-mdx-expression demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.