micromark extension to support MDX or MDX JS expressions
Installations
npm install micromark-extension-mdx-expression
Developer Guide
Typescript
Yes
Module System
ESM
Node Version
20.8.1
NPM Version
10.1.0
Score
99
Supply Chain
99.5
Quality
75.9
Maintenance
100
Vulnerability
100
License
Releases
micromark-factory-mdx-expression@2.0.2
Updated on Sep 02, 2024
micromark-extension-mdx-expression@3.0.0
Updated on Oct 19, 2023
micromark-util-events-to-acorn@2.0.2
Updated on Oct 19, 2023
micromark-factory-mdx-expression@2.0.1
Updated on Jul 07, 2023
micromark-util-events-to-acorn@2.0.1
Updated on Jul 07, 2023
2.0.0
Updated on Jun 22, 2023
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Developer
Download Statistics
Total Downloads
163,427,584
Last Day
367,924
Last Week
1,918,429
Last Month
8,318,374
Last Year
81,348,731
GitHub Statistics
MIT License
11 Stars
157 Commits
2 Forks
7 Watchers
1 Branches
11 Contributors
Updated on Jan 29, 2025
Bundle Size
9.50 kB
Minified
3.70 kB
Minified + Gzipped
Sponsor this package
Package Meta Information
Latest Version
3.0.0
Package Id
micromark-extension-mdx-expression@3.0.0
Unpacked Size
33.07 kB
Size
7.63 kB
File Count
10
NPM Version
10.1.0
Node Version
20.8.1
Published on
Oct 19, 2023
Total Downloads
Cumulative downloads
Total Downloads
163,427,584
Last Day
-0.4%
367,924
Compared to previous day
Last Week
-4.1%
1,918,429
Compared to previous week
Last Month
47.3%
8,318,374
Compared to previous month
Last Year
71%
81,348,731
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
micromark-extension-mdx-expression
micromark extension to support MDX expressions ({Math.PI}
).
Contents
- What is this?
- When to use this
- Install
- Use
- API
- Authoring
- Syntax
- Errors
- Unexpected end of file in expression, expected a corresponding closing brace for
{
- Unexpected lazy line in expression in container, expected line to be prefixed…
- Unexpected
$type
in code: expected an object spread ({...spread}
) - Unexpected extra content in spread: only a single spread is supported
- Could not parse expression with acorn
- Unexpected end of file in expression, expected a corresponding closing brace for
- Tokens
- Types
- Compatibility
- Security
- Related
- Contribute
- License
What is this?
This package contains an extension that adds support for the expression syntax
enabled by MDX to micromark
.
These extensions are used inside MDX.
This package can be made aware or unaware of JavaScript syntax. When unaware, expressions could include Rust or variables or whatnot.
When to use this
This project is useful when you want to support expressions in markdown.
You can use this extension when you are working with micromark
.
To support all MDX features, use
micromark-extension-mdxjs
instead.
When you need a syntax tree, combine this package with
mdast-util-mdx-expression
.
All these packages are used in remark-mdx
, which focusses on
making it easier to transform content by abstracting these internals away.
When you are using mdx-js/mdx
, all of this is already included.
Install
This package is ESM only. In Node.js (version 16+), install with npm:
1npm install micromark-extension-mdx-expression
In Deno with esm.sh
:
1import {mdxExpression} from 'https://esm.sh/micromark-extension-mdx-expression@2'
In browsers with esm.sh
:
1<script type="module"> 2 import {mdxExpression} from 'https://esm.sh/micromark-extension-mdx-expression@2?bundle' 3</script>
Use
1import {Parser} from 'acorn' 2import acornJsx from 'acorn-jsx' 3import {micromark} from 'micromark' 4import {mdxExpression} from 'micromark-extension-mdx-expression' 5 6// Unaware of JavaScript (“agnostic”) (balanced braces): 7const output = micromark('a {1 + 1} b', {extensions: [mdxExpression()]}) 8 9console.log(output) 10 11// Aware of JavaScript: 12micromark('a {!} b', {extensions: [mdxExpression({acorn: Parser.extend(acornJsx())})]})
Yields:
1<p>a b</p>
1[1:5: Could not parse expression with acorn] { 2 ancestors: undefined, 3 cause: SyntaxError: Unexpected token 4 at pp$4.raise (file:///Users/tilde/Projects/oss/micromark-extension-mdx-expression/node_modules/acorn/dist/acorn.mjs:3547:13) 5 at pp$9.unexpected (file:///Users/tilde/Projects/oss/micromark-extension-mdx-expression/node_modules/acorn/dist/acorn.mjs:758:8) 6 … 7 pos: 4, 8 loc: { line: 1, column: 4 }, 9 raisedAt: 1 10 }, 11 column: 5, 12 fatal: undefined, 13 line: 1, 14 place: { line: 1, column: 5, offset: 4 }, 15 reason: 'Could not parse expression with acorn', 16 ruleId: 'acorn', 17 source: 'micromark-extension-mdx-expression', 18 url: 'https://github.com/micromark/micromark-extension-mdx-expression/tree/main/packages/micromark-extension-mdx-expression#could-not-parse-expression-with-acorn' 19}
…which is useless: go to a syntax tree with
mdast-util-from-markdown
and
mdast-util-mdx-expression
instead.
API
This package exports the identifier mdxExpression
.
There is no default export.
The export map supports the development
condition.
Run node --conditions development module.js
to get instrumented dev code.
Without this condition, production code is loaded.
mdxExpression(options?)
Create an extension for micromark
to enable MDX expression syntax.
Parameters
options
(Options
, optional) — configuration
Returns
Extension for micromark
that can be passed in extensions
to enable MDX
expression syntax (Extension
).
Options
Configuration (TypeScript type).
Fields
acorn
(Acorn
, optional) — acorn parser to useacornOptions
(AcornOptions
, default:{ecmaVersion: 2024, locations: true, sourceType: 'module'}
) — configuration for acorn; all fields exceptlocations
can be setaddResult
(boolean
, default:false
) — whether to addestree
fields to tokens with results from acorn
Authoring
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.
Syntax
This extension supports MDX 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.
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.
1Here is an expression in a heading: 2 3## Hello, {1 + 1}! 4 5In agnostic mode, balanced braces can occur: {a + {b} + c}. 6 7In gnostic mode, the value of the expression must be JavaScript, so 8this would fail: {!}. 9But, in gnostic mode, braces can be in comments, strings, or in other 10places: {1 /* { */ + 2}. 11 12The previous examples were text (inline, span) expressions, they can 13also be flow (block): 14 15{ 16 1 + 1 17} 18 19This is incorrect, because there are further characters: 20 21{ 22 1 + 1 23}!
1Blank lines cannot occur in text, because markdown has already split them in 2separate constructs, so this is incorrect: {1 + 3 41}
1In flow, you can have blank lines: 2 3{ 4 1 + 5 6 2 7}
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:
1a { b
Unexpected lazy line in expression in container, expected line to be prefixed…
This error occurs if a {
was seen in a container which then has lazy content
(source: micromark-extension-mdx-expression
, rule id: unexpected-lazy
).
For example:
1> {a 2b}
Unexpected $type
in code: expected an object spread ({...spread}
)
This error occurs if a spread was expected but something else was found
(source: micromark-extension-mdx-expression
, rule id: non-spread
).
For example:
1<a {b=c}={} d>
Unexpected extra content in spread: only a single spread is supported
This error occurs if a spread was expected but more was found after it
(source: micromark-extension-mdx-expression
, rule id: spread-extra
).
For example:
1<a {...b,c} d>
Could not parse expression with acorn
This error occurs if acorn crashes or when there is more content after a JS
expression (source: micromark-extension-mdx-expression
, rule id: acorn
).
For example:
1a {"b" "c"} d
1a {var 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
andmdxTextExpressionMarker
for the braceswhitespace
for markdown spaces and tabs in blank linesmdxFlowExpressionChunk
andmdxTextExpressionChunk
for chunks of expression content
Types
This package is fully typed with TypeScript.
It exports the additional type Options
.
Compatibility
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-expression@^2
, compatible with Node.js 16.
This package works with micromark
version 3
and later.
Security
This package is safe.
Related
micromark-extension-mdxjs
— support all MDX syntaxmdast-util-mdx-expression
— support MDX expressions in mdastremark-mdx
— support all MDX syntax in remark
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

No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: license:0
- Info: FSF or OSI recognized license: MIT License: license:0
Reason
0 existing vulnerabilities detected
Reason
security policy file detected
Details
- Info: security policy file detected: github.com/micromark/.github/security.md:1
- Info: Found linked content: github.com/micromark/.github/security.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: github.com/micromark/.github/security.md:1
- Info: Found text in security policy: github.com/micromark/.github/security.md:1
Reason
9 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 8
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/bb.yml:5: update your workflow using https://app.stepsecurity.io/secureworkflow/micromark/micromark-extension-mdx-expression/bb.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:6: update your workflow using https://app.stepsecurity.io/secureworkflow/micromark/micromark-extension-mdx-expression/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:7: update your workflow using https://app.stepsecurity.io/secureworkflow/micromark/micromark-extension-mdx-expression/main.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:12: update your workflow using https://app.stepsecurity.io/secureworkflow/micromark/micromark-extension-mdx-expression/main.yml/main?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/main.yml:11
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 2 third-party GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/bb.yml:1
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Score
4.8
/10
Last Scanned on 2025-02-17
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More