Project: syntax-tree/mdast-util-math

Package: mdast-util-math@2.0.0

  1. Dependents: 1
  2. mdast extension to parse and serialize math
  1. util 143
  2. utility 139
  3. markdown 131
  4. unist 127
  5. mdast 85
  6. mdast-util 30
  7. markup 18
  8. math 5
  9. latex 5
  10. tex 5
  11. katex 4


Build Coverage Downloads Size Sponsors Backers Chat

Extension for mdast-util-from-markdown and/or mdast-util-to-markdown to support math in mdast. When parsing (from-markdown), must be combined with micromark-extension-math.

When to use this

Use this if you’re dealing with the AST manually. It might be better to use remark-math with remark, which includes this but provides a nicer interface and makes it easier to combine with hundreds of plugins.


This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.


npm install mdast-util-math


Say we have the following file, example.md:

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'
import {fromMarkdown} from 'mdast-util-from-markdown'
import {toMarkdown} from 'mdast-util-to-markdown'
import {math} from 'micromark-extension-math'
import {mathFromMarkdown, mathToMarkdown} from 'mdast-util-math'

const doc = fs.readFileSync('example.md')

const tree = fromMarkdown(doc, {
  extensions: [math()],
  mdastExtensions: [mathFromMarkdown()]


const out = toMarkdown(tree, {extensions: [mathToMarkdown()]})


Now, running node example yields (positional info 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', e: 'C_L', data: {/* … */}},
        {type: 'text', value: ') like the following equation.'}
    {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 following identifiers: mathFromMarkdown, mathToMarkdown. There is no default export.



Support math. These exports are functions that create extensions, respectively for mdast-util-from-markdown and mdast-util-to-markdown.


Whether to support math (text) with a single dollar (boolean, default: true). Single dollars work in Pandoc and many other places, but often interfere with “normal” dollars in text.


See 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