Gathering detailed insights and metrics for mdast-util-mdx
Gathering detailed insights and metrics for mdast-util-mdx
Gathering detailed insights and metrics for mdast-util-mdx
Gathering detailed insights and metrics for mdast-util-mdx
mdast extension to parse and serialize MDX (or MDX.js)
npm install mdast-util-mdx
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
22 Stars
51 Commits
1 Forks
8 Watching
1 Branches
10 Contributors
Updated on 17 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-8.6%
298,953
Compared to previous day
Last week
2.6%
1,775,516
Compared to previous week
Last month
8.7%
7,552,256
Compared to previous month
Last year
77.2%
70,859,739
Compared to previous year
mdast extensions to parse and serialize MDX: ESM, JSX, and expressions.
This package contains two extensions that add support for MDX syntax in
markdown to mdast: ESM (import x from 'y'
), JSX (<a />
), and
expressions ({Math.PI}
).
These extensions plug into
mdast-util-from-markdown
(to support parsing
MDX in markdown into a syntax tree) and
mdast-util-to-markdown
(to support serializing
MDX 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
or micromark-extension-mdxjs
.
Instead of this package, you can also use the extensions separately:
mdast-util-mdx-expression
— support MDX expressionsmdast-util-mdx-jsx
— support MDX JSXmdast-util-mdxjs-esm
— support MDX ESMAll 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
In Deno with esm.sh
:
1import {mdxFromMarkdown, mdxToMarkdown} from 'https://esm.sh/mdast-util-mdx@3'
In browsers with esm.sh
:
1<script type="module"> 2 import {mdxFromMarkdown, mdxToMarkdown} from 'https://esm.sh/mdast-util-mdx@3?bundle' 3</script>
Say our document example.mdx
contains:
1import Box from "place" 2 3Here’s an expression: 4 5{ 6 1 + 1 /* } */ 7} 8 9Which you can also put inline: {1+1}. 10 11<Box> 12 <SmallerBox> 13 - Lists, which can be indented. 14 </SmallerBox> 15</Box>
…and our module example.js
looks as follows:
1import fs from 'node:fs/promises' 2import {mdxjs} from 'micromark-extension-mdxjs' 3import {fromMarkdown} from 'mdast-util-from-markdown' 4import {mdxFromMarkdown, mdxToMarkdown} from 'mdast-util-mdx' 5import {toMarkdown} from 'mdast-util-to-markdown' 6 7const doc = await fs.readFile('example.mdx') 8 9const tree = fromMarkdown(doc, { 10 extensions: [mdxjs()], 11 mdastExtensions: [mdxFromMarkdown()] 12}) 13 14console.log(tree) 15 16const out = toMarkdown(tree, {extensions: [mdxToMarkdown()]}) 17 18console.log(out)
…now running node example.js
yields (positional info removed for brevity):
1{ 2 type: 'root', 3 children: [ 4 { 5 type: 'mdxjsEsm', 6 value: 'import Box from "place"', 7 data: { 8 estree: { 9 type: 'Program', 10 body: [ 11 { 12 type: 'ImportDeclaration', 13 specifiers: [ 14 { 15 type: 'ImportDefaultSpecifier', 16 local: {type: 'Identifier', name: 'Box'} 17 } 18 ], 19 source: {type: 'Literal', value: 'place', raw: '"place"'} 20 } 21 ], 22 sourceType: 'module' 23 } 24 } 25 }, 26 { 27 type: 'paragraph', 28 children: [{type: 'text', value: 'Here’s an expression:'}] 29 }, 30 { 31 type: 'mdxFlowExpression', 32 value: '\n1 + 1 /* } */\n', 33 data: { 34 estree: { 35 type: 'Program', 36 body: [ 37 { 38 type: 'ExpressionStatement', 39 expression: { 40 type: 'BinaryExpression', 41 left: {type: 'Literal', value: 1, raw: '1'}, 42 operator: '+', 43 right: {type: 'Literal', value: 1, raw: '1'} 44 } 45 } 46 ], 47 sourceType: 'module' 48 } 49 } 50 }, 51 { 52 type: 'paragraph', 53 children: [ 54 {type: 'text', value: 'Which you can also put inline: '}, 55 { 56 type: 'mdxTextExpression', 57 value: '1+1', 58 data: { 59 estree: { 60 type: 'Program', 61 body: [ 62 { 63 type: 'ExpressionStatement', 64 expression: { 65 type: 'BinaryExpression', 66 left: {type: 'Literal', value: 1, raw: '1'}, 67 operator: '+', 68 right: {type: 'Literal', value: 1, raw: '1'} 69 } 70 } 71 ], 72 sourceType: 'module' 73 } 74 } 75 }, 76 {type: 'text', value: '.'} 77 ] 78 }, 79 { 80 type: 'mdxJsxFlowElement', 81 name: 'Box', 82 attributes: [], 83 children: [ 84 { 85 type: 'mdxJsxFlowElement', 86 name: 'SmallerBox', 87 attributes: [], 88 children: [ 89 { 90 type: 'list', 91 ordered: false, 92 start: null, 93 spread: false, 94 children: [ 95 { 96 type: 'listItem', 97 spread: false, 98 checked: null, 99 children: [ 100 { 101 type: 'paragraph', 102 children: [ 103 {type: 'text', value: 'Lists, which can be indented.'} 104 ] 105 } 106 ] 107 } 108 ] 109 } 110 ] 111 } 112 ] 113 } 114 ] 115}
1import Box from "place" 2 3Here’s an expression: 4 5{ 6 1 + 1 /* } */ 7} 8 9Which you can also put inline: {1+1}. 10 11<Box> 12 <SmallerBox> 13 * Lists, which can be indented. 14 </SmallerBox> 15</Box>
This package exports the identifiers mdxFromMarkdown
and mdxToMarkdown
.
There is no default export.
mdxFromMarkdown()
Create an extension for mdast-util-from-markdown
to enable MDX (ESM, JSX, expressions).
Extension for mdast-util-from-markdown
to enable MDX
(FromMarkdownExtension
).
When using the syntax extensions with addResult
, ESM and expression
nodes will have data.estree
fields set to ESTree Program
node.
mdxToMarkdown(options?)
Create an extension for mdast-util-to-markdown
to enable MDX (ESM, JSX, expressions).
Extension for mdast-util-to-markdown
.
options
(ToMarkdownOptions
)
— configurationExtension for mdast-util-to-markdown
to enable MDX
(FromMarkdownExtension
).
ToMarkdownOptions
Configuration (TypeScript type).
quote
('"'
or "'"
, default: '"'
)
— preferred quote to use around attribute valuesquoteSmart
(boolean
, default: false
)
— use the other quote if that results in less bytestightSelfClosing
(boolean
, default: false
)
— do not use an extra space when closing self-closing elements: <img/>
instead of <img />
printWidth
(number
, default: Infinity
)
— try and wrap syntax at this width.
When set to a finite number (say, 80
), the formatter will print
attributes on separate lines when a tag doesn’t fit on one line.
The normal behavior is to print attributes with spaces between them instead
of line endingsMDX has no representation in HTML.
Though, when you are dealing with MDX, you will likely go through hast.
You can enable passing MDX through to hast by configuring
mdast-util-to-hast
with passThrough: ['mdxjsEsm', 'mdxFlowExpression', 'mdxJsxFlowElement', 'mdxJsxTextElement', 'mdxTextExpression']
.
See Syntax in micromark-extension-mdxjs
.
This utility combines several mdast utilities. See their readmes for the node types supported in the tree:
mdast-util-mdx-expression
— support MDX expressionsmdast-util-mdx-jsx
— support MDX JSXmdast-util-mdxjs-esm
— support MDX ESMThis package is fully typed with TypeScript.
It exports the additional types
MdxFlowExpression
and MdxTextExpression
from mdast-util-mdx-expression
;
MdxJsxAttribute
,
MdxJsxAttributeValueExpression
,
MdxJsxExpressionAttribute
,
MdxJsxFlowElement
,
MdxJsxTextElement
,
and ToMarkdownOptions
from mdast-util-mdx-jsx
;
and MdxjsEsm
from mdast-util-mdxjs-esm
.
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 * @typedef {import('mdast-util-mdx')} 3 */ 4 5import {visit} from 'unist-util-visit' 6 7/** @type {import('mdast').Root} */ 8const tree = getMdastNodeSomeHow() 9 10visit(tree, function (node) { 11 // `node` can now be an expression, JSX, or ESM node. 12})
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@^3
,
compatible with Node.js 16.
This utility works with mdast-util-from-markdown
version 2+ and
mdast-util-to-markdown
version 2+.
remark-mdx
— remark plugin to support MDXmicromark-extension-mdx
— micromark extension to parse MDXmicromark-extension-mdxjs
— micromark extension to parse JavaScript-aware MDXSee 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.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
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 More