unified

Project: syntax-tree/mdast-util-math

Package: mdast-util-math@0.1.2

  1. Dependencies: 0·Dependents: 1
  2. mdast extension to parse and serialize math
  1. markdown 142
  2. util 128
  3. utility 124
  4. unist 120
  5. mdast 89
  6. mdast-util 29
  7. markup 17
  8. math 6
  9. latex 6
  10. tex 6
  11. katex 5

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.

You probably shouldn’t use this package directly, but instead use remark-math with remark.

Install

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 script, example.js, looks as follows:

var fs = require('fs')
var fromMarkdown = require('mdast-util-from-markdown')
var toMarkdown = require('mdast-util-to-markdown')
var syntax = require('micromark-extension-math')
var math = require('mdast-util-math')

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

var tree = fromMarkdown(doc, {
  extensions: [syntax],
  mdastExtensions: [math.fromMarkdown]
})

console.log(tree)

var out = toMarkdown(tree, {extensions: [math.toMarkdown]})

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

math.fromMarkdown

math.toMarkdown

Note: the separate extensions are also available at mdast-util-math/from-markdown and mdast-util-math/to-markdown.

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

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