
Security News
MCP Community Begins Work on Official MCP Metaregistry
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
mdast-util-math
Advanced tools
The `mdast-util-math` package is a utility for working with math content in Markdown Abstract Syntax Trees (MDAST). It provides tools to parse and serialize math content, making it easier to handle mathematical expressions within Markdown documents.
Parse Math Content
This feature allows you to parse Markdown content that includes inline and block math expressions. The code sample demonstrates how to use `mdast-util-math` with `remark-parse` and `remark-math` to parse a Markdown string containing a math expression.
const { fromMarkdown } = require('mdast-util-math');
const { unified } = require('unified');
const markdown = require('remark-parse');
const math = require('remark-math');
const processor = unified()
.use(markdown)
.use(math);
const tree = processor.parse('Here is some math: $E=mc^2$');
console.log(tree);
Serialize Math Content
This feature allows you to serialize MDAST containing math expressions back into Markdown. The code sample shows how to convert an MDAST tree with an inline math node back into a Markdown string.
const { toMarkdown } = require('mdast-util-math');
const { unified } = require('unified');
const markdown = require('remark-stringify');
const math = require('remark-math');
const tree = {
type: 'root',
children: [
{ type: 'paragraph', children: [
{ type: 'text', value: 'Here is some math: ' },
{ type: 'inlineMath', value: 'E=mc^2' }
]}
]
};
const processor = unified()
.use(markdown)
.use(math);
const result = processor.stringify(tree);
console.log(result);
The `remark-math` package is a plugin for `remark` that adds support for parsing and rendering math in Markdown. It works well with `mdast-util-math` but can also be used independently to handle math content in Markdown documents. Compared to `mdast-util-math`, `remark-math` focuses more on integrating math parsing and rendering into the `remark` ecosystem.
The `rehype-katex` package is a plugin for `rehype` that renders math using KaTeX. It is used to convert math content in HTML documents into beautifully rendered math expressions. While `mdast-util-math` focuses on parsing and serializing math in MDAST, `rehype-katex` is more about rendering math in HTML.
mdast extensions to parse and serialize math ($C_L$
).
This package contains two extensions that add support for math syntax in
markdown to mdast.
These extensions plug into
mdast-util-from-markdown
(to support parsing
math in markdown into a syntax tree) and
mdast-util-to-markdown
(to support serializing
math in syntax trees to markdown).
This project is useful when you want to support math in markdown. Extending markdown with a syntax extension makes the markdown less portable. LaTeX equations are also quite hard. But this mechanism works well when you want authors, that have some LaTeX experience, to be able to embed rich diagrams of math in scientific text.
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-math
.
When you don’t need a syntax tree, you can use micromark
directly with
micromark-extension-math
.
All these packages are used remark-math
, which
focusses on making it easier to transform content by abstracting these
internals away.
This utility adds fields on nodes so that the utility responsible for
turning mdast (markdown) nodes into hast (HTML) nodes,
mdast-util-to-hast
, turns text (inline) math nodes into
<code class="language-math math-inline">…</code>
and flow (block) math nodes
into <pre><code class="language-math math-display">…</code></pre>
.
This package is ESM only. In Node.js (version 16+), install with npm:
npm install mdast-util-math
In Deno with esm.sh
:
import {mathFromMarkdown, mathToMarkdown} from 'https://esm.sh/mdast-util-math@3'
In browsers with esm.sh
:
<script type="module">
import {mathFromMarkdown, mathToMarkdown} from 'https://esm.sh/mdast-util-math@3?bundle'
</script>
Say our document example.md
contains:
Lift($L$) can be determined by Lift Coefficient ($C_L$) like the following
equation.
$$
L = \frac{1}{2} \rho v^2 S C_L
$$
…and our module example.js
looks as follows:
import fs from 'node:fs/promises'
import {math} from 'micromark-extension-math'
import {fromMarkdown} from 'mdast-util-from-markdown'
import {mathFromMarkdown, mathToMarkdown} from 'mdast-util-math'
import {toMarkdown} from 'mdast-util-to-markdown'
const doc = await fs.readFile('example.md')
const tree = fromMarkdown(doc, {
extensions: [math()],
mdastExtensions: [mathFromMarkdown()]
})
console.log(tree)
const out = toMarkdown(tree, {extensions: [mathToMarkdown()]})
console.log(out)
…now running node example.js
yields (positional info and data removed for
brevity):
{
type: 'root',
children: [
{
type: 'paragraph',
children: [
{type: 'text', value: 'Lift('},
{type: 'inlineMath', value: 'L', data: {/* … */}},
{type: 'text', value: ') can be determined by Lift Coefficient ('},
{type: 'inlineMath', value: 'C_L', data: {/* … */}},
{type: 'text', value: ') like the following\nequation.'}
]
},
{type: 'math', meta: null, value: 'L = \\frac{1}{2} \\rho v^2 S C_L', data: {/* … */}}
]
}
Lift($L$) can be determined by Lift Coefficient ($C_L$) like the following
equation.
$$
L = \frac{1}{2} \rho v^2 S C_L
$$
This package exports the identifiers
mathFromMarkdown
and mathToMarkdown
.
There is no default export.
mathFromMarkdown()
Create an extension for mdast-util-from-markdown
.
Extension for mdast-util-from-markdown
(FromMarkdownExtension
).
mathToMarkdown(options?)
Create an extension for mdast-util-to-markdown
.
options
(ToOptions
, optional)
— configurationExtension for mdast-util-to-markdown
(ToMarkdownExtension
).
InlineMath
Math (text) (TypeScript type).
import type {Data, Literal} from 'mdast'
interface InlineMath extends Literal {
type: 'inlineMath'
data?: InlineMathData | undefined
}
export interface InlineMathData extends Data {}
Math
Math (flow) (TypeScript type).
import type {Data, Literal} from 'mdast'
interface Math extends Literal {
type: 'math'
meta?: string | null | undefined
data?: MathData | undefined
}
export interface MathData extends Data {}
ToOptions
Configuration (TypeScript type).
singleDollarTextMath
(boolean
, default: true
)
— whether to support math (text) with a single dollar.
Single dollars work in Pandoc and many other places, but often interfere
with “normal” dollars in text.
If you turn this off, you can still use two or more dollars for text mathThis plugin integrates with mdast-util-to-hast
.
When mdast is turned into hast the math nodes are turned into
<code class="language-math math-inline">…</code>
and
<pre><code class="language-math math-display">…</code></pre>
elements.
See Syntax in micromark-extension-frontmatter
.
The following interfaces are added to mdast by this utility.
Math
interface Math <: Literal {
type: 'code'
meta: string?
}
Math (Literal) represents a block of math, such as LaTeX mathematical expressions.
Math can be used where flow content is expected.
Its content is represented by its value
field.
This node relates to the phrasing content concept InlineMath.
A meta
field can be present.
It represents custom information relating to the node.
For example, the following markdown:
$$
L = \frac{1}{2} \rho v^2 S C_L
$$
Yields:
{
type: 'math',
meta: null,
value: 'L = \\frac{1}{2} \\rho v^2 S C_L',
data: {/* … */}
}
InlineMath
interface InlineMath <: Literal {
type: 'inlineMath'
}
InlineMath (Literal) represents a fragment of computer code, such as a file name, computer program, or anything a computer could parse.
InlineMath can be used where phrasing content
is expected.
Its content is represented by its value
field.
This node relates to the flow content concept Math.
For example, the following markdown:
$L$
Yields:
{type: 'inlineMath', value: 'L', data: {/* … */}}
FlowContent
(math)type FlowContentMath = Math | FlowContent
PhrasingContent
(math)type PhrasingContentMath = InlineMath | PhrasingContent
This package is fully typed with TypeScript.
It exports the additional types InlineMath
,
Math
, and ToOptions
.
It also registers the node types with @types/mdast
.
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.
/**
* @typedef {import('mdast-util-math')}
*/
import {visit} from 'unist-util-visit'
/** @type {import('mdast').Root} */
const tree = getMdastNodeSomeHow()
visit(tree, function (node) {
// `node` can now be one of the nodes for math.
})
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-math@^3
,
compatible with Node.js 16.
This plugin works with mdast-util-from-markdown
version 2+ and
mdast-util-to-markdown
version 2+.
remark-math
— remark plugin to support mathmicromark-extension-math
— micromark extension to parse mathSee 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.
FAQs
mdast extension to parse and serialize math
The npm package mdast-util-math receives a total of 405,767 weekly downloads. As such, mdast-util-math popularity was classified as popular.
We found that mdast-util-math demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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 MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.
Research
Security News
Malicious npm packages posing as developer tools target macOS Cursor IDE users, stealing credentials and modifying files to gain persistent backdoor access.