Gathering detailed insights and metrics for mdast-util-mdx-expression
Gathering detailed insights and metrics for mdast-util-mdx-expression
Gathering detailed insights and metrics for mdast-util-mdx-expression
Gathering detailed insights and metrics for mdast-util-mdx-expression
mdast extension to parse and serialize MDX or MDX.js expressions
npm install mdast-util-mdx-expression
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
4 Stars
76 Commits
8 Watching
1 Branches
10 Contributors
Updated on 14 Sept 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-7.9%
525,966
Compared to previous day
Last week
3.5%
3,082,655
Compared to previous week
Last month
10.6%
12,861,283
Compared to previous month
Last year
150.6%
106,127,831
Compared to previous year
mdast extensions to parse and serialize MDX expressions ({Math.PI}
).
This package contains two extensions that add support for MDX expression syntax
in markdown to mdast.
These extensions plug into
mdast-util-from-markdown
(to support parsing
expressions in markdown into a syntax tree) and
mdast-util-to-markdown
(to support serializing
expressions in syntax trees to markdown).
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-expression
.
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:
1npm install mdast-util-mdx-expression
In Deno with esm.sh
:
1import {mdxExpressionFromMarkdown, mdxExpressionToMarkdown} from 'https://esm.sh/mdast-util-mdx-expression@2'
In browsers with esm.sh
:
1<script type="module"> 2 import {mdxExpressionFromMarkdown, mdxExpressionToMarkdown} from 'https://esm.sh/mdast-util-mdx-expression@2?bundle' 3</script>
Say our document example.mdx
contains:
1{ 2 a + 1 3} 4 5b {true}.
…and our module example.js
looks as follows:
1import fs from 'node:fs/promises' 2import * as acorn from 'acorn' 3import {mdxExpression} from 'micromark-extension-mdx-expression' 4import {fromMarkdown} from 'mdast-util-from-markdown' 5import {mdxExpressionFromMarkdown, mdxExpressionToMarkdown} from 'mdast-util-mdx-expression' 6import {toMarkdown} from 'mdast-util-to-markdown' 7 8const doc = await fs.readFile('example.mdx') 9 10const tree = fromMarkdown(doc, { 11 extensions: [mdxExpression({acorn, addResult: true})], 12 mdastExtensions: [mdxExpressionFromMarkdown()] 13}) 14 15console.log(tree) 16 17const out = toMarkdown(tree, {extensions: [mdxExpressionToMarkdown()]}) 18 19console.log(out)
…now running node example.js
yields (positional info removed for brevity):
1{ 2 type: 'root', 3 children: [ 4 { 5 type: 'mdxFlowExpression', 6 value: '\na + 1\n', 7 data: { 8 estree: { 9 type: 'Program', 10 body: [ 11 { 12 type: 'ExpressionStatement', 13 expression: { 14 type: 'BinaryExpression', 15 left: {type: 'Identifier', name: 'a'}, 16 operator: '+', 17 right: {type: 'Literal', value: 1, raw: '1'} 18 } 19 } 20 ], 21 sourceType: 'module' 22 } 23 } 24 }, 25 { 26 type: 'paragraph', 27 children: [ 28 {type: 'text', value: 'b '}, 29 { 30 type: 'mdxTextExpression', 31 value: 'true', 32 data: { 33 estree: { 34 type: 'Program', 35 body: [ 36 { 37 type: 'ExpressionStatement', 38 expression: {type: 'Literal', value: true, raw: 'true'} 39 } 40 ], 41 sourceType: 'module' 42 } 43 } 44 }, 45 {type: 'text', value: '.'} 46 ] 47 } 48 ] 49}
1{ 2 a + 1 3} 4 5b {true}.
This package exports the identifiers
mdxExpressionFromMarkdown
and
mdxExpressionToMarkdown
.
There is no default export.
mdxExpressionFromMarkdown()
Create an extension for mdast-util-from-markdown
to enable MDX expressions in markdown.
When using the micromark syntax extension with addResult
, nodes
will have a data.estree
field set to an ESTree Program
node.
Extension for mdast-util-from-markdown
to enable MDX expressions
(FromMarkdownExtension
).
mdxExpressionToMarkdown()
Create an extension for mdast-util-to-markdown
to enable MDX expressions in markdown.
Extension for mdast-util-to-markdown
to enable MDX expressions
(ToMarkdownExtension
).
MdxFlowExpression
MDX expression node, occurring in flow (block) (TypeScript type).
1import type {Program} from 'estree-jsx' 2import type {Data, Literal} from 'mdast' 3 4interface MdxFlowExpression extends Literal { 5 type: 'mdxFlowExpression' 6 data?: MdxFlowExpressionData | undefined 7} 8 9interface MdxFlowExpressionData extends Data { 10 estree?: Program | null | undefined 11}
MdxTextExpression
MDX expression node, occurring in text (block) (TypeScript type).
1import type {Program} from 'estree-jsx' 2import type {Data, Literal} from 'mdast' 3 4interface MdxTextExpression extends Literal { 5 type: 'mdxTextExpression' 6 data?: MdxTextExpressionData | undefined 7} 8 9interface MdxTextExpressionData extends Data { 10 estree?: Program | null | undefined 11}
MdxFlowExpressionHast
Same as MdxFlowExpression
, but registered with
@types/hast
(TypeScript type).
1import type {Program} from 'estree-jsx' 2import type {Data, Literal} from 'hast' 3 4interface MdxFlowExpressionHast extends Literal { 5 type: 'mdxFlowExpression' 6 data?: MdxFlowExpressionData | undefined 7} 8 9interface MdxFlowExpressionData extends Data { 10 estree?: Program | null | undefined 11}
MdxTextExpressionHast
Same as MdxTextExpression
, but registered with
@types/hast
(TypeScript type).
1import type {Program} from 'estree-jsx' 2import type {Data, Literal} from 'hast' 3 4interface MdxTextExpressionHast extends Literal { 5 type: 'mdxTextExpression' 6 data?: MdxTextExpressionData | undefined 7} 8 9interface MdxTextExpressionData extends Data { 10 estree?: Program | null | undefined 11}
MDX expressions have no representation in HTML.
Though, when you are dealing with MDX, you will likely go through hast.
You can enable passing MDX expressions through to hast by configuring
mdast-util-to-hast
with
passThrough: ['mdxFlowExpression', 'mdxTextExpression']
.
See Syntax in micromark-extension-mdx-expression
.
The following interfaces are added to mdast by this utility.
MdxFlowExpression
1interface MdxFlowExpression <: Literal { 2 type: 'mdxFlowExpression' 3}
MdxFlowExpression (Literal) represents a JavaScript
expression embedded in flow (block).
It can be used where flow content is expected.
Its content is represented by its value
field.
For example, the following markdown:
1{ 2 1 + 1 3}
Yields:
1{type: 'mdxFlowExpression', value: '\n1 + 1\n'}
MdxTextExpression
1interface MdxTextExpression <: Literal { 2 type: 'mdxTextExpression" 3}
MdxTextExpression (Literal) represents a JavaScript
expression embedded in text (span, inline).
It can be used where phrasing content is expected.
Its content is represented by its value
field.
For example, the following markdown:
1a {1 + 1} b.
Yields:
1{type: 'mdxTextExpression', value: '1 + 1'}
FlowContent
(MDX expression)1type FlowContentMdxExpression = MdxFlowExpression | FlowContent
PhrasingContent
(MDX expression)1type PhrasingContentMdxExpression = MdxTextExpression | PhrasingContent
This package is fully typed with TypeScript.
It exports the additional types MdxFlowExpression
,
MdxFlowExpressionHast
,
MdxTextExpression
, and
MdxTextExpressionHast
.
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.
1/** 2 * @import {} from 'mdast-util-mdx-expression' 3 * @import {Root} from 'mdast' 4 */ 5 6import {visit} from 'unist-util-visit' 7 8/** @type {Root} */ 9const tree = getMdastNodeSomeHow() 10 11visit(tree, function (node) { 12 // `node` can now be an expression node. 13})
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-expression@^2
, compatible with Node.js 16.
This utility works with mdast-util-from-markdown
version 2+ and
mdast-util-to-markdown
version 2+.
remarkjs/remark-mdx
— remark plugin to support MDXsyntax-tree/mdast-util-mdx
— mdast utility to support MDXmicromark/micromark-extension-mdx-expression
— micromark extension to parse MDX expressionsSee 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.
MIT © Titus Wormer
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
security policy file detected
Details
Reason
6 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 5
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2024-11-18
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 Moremdast-util-mdx
mdast extension to parse and serialize MDX (or MDX.js)
mdast-util-mdx-jsx
mdast extension to parse and serialize MDX or MDX.js JSX
mdast-util-mdxjs-esm
mdast extension to parse and serialize MDX.js ESM (import/exports)
mdast-util-to-string
mdast utility to get the plain text content of a node