mdast-util-math

mdast extension to parse and serialize math

Usage no npm install needed!

<script type="module">
  import mdastUtilMath from 'https://cdn.skypack.dev/mdast-util-math';
</script>

README

mdast-util-math

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.

Install

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

npm:

npm install mdast-util-math

Use

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()]
})

console.log(tree)

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

console.log(out)

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
$

API

This package exports the following identifiers: mathFromMarkdown, mathToMarkdown. There is no default export.

mathFromMarkdown()

mathToMarkdown(toOptions?)

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

toOptions
toOptions.singleDollarTextMath

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.

Related

Contribute

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.

License

MIT © Titus Wormer