micromarkextensionmath
micromark extension to support math ($C_L$
).
As there is no spec for math in markdown, this extension stays as close to code in text and fenced code in flow in CommonMark, but using dollar signs.
This package provides the lowlevel modules for integrating with the micromark tokenizer and the micromark HTML compiler.
You probably shouldn’t use the HTML parts of this package directly, but instead
use mdastutilmath
with mdast.
Math (text) can start with one or more dollar signs, so long as they match:
With one: $\alpha$, two: $$\beta$$, or three: $$$\gamma$$$.
This is useful, because like code, typical markdown escapes don’t work.
For dollars inside math, use more dollars around it: $$\raisebox{0.25em}{$\frac
a b$}$$.
If the math starts and ends with a space (or EOL), those are removed: $$ \$ $$.
Math (flow) starts at two or more dollars:
$$
\Delta
$$
You can hide some stuff in the meta of the opening fence (but no dollars):
$$hidden information
$$
Math which doesn’t have a closing fence, still works, like fenced code:
> $$
> this is
> all math
…but at the end of their container (block quote, list item, or document), they
are closed.
Install
npm:
npm install micromarkextensionmath
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 micromark = require('micromark')
var math = require('micromarkextensionmath')
var mathHtml = require('micromarkextensionmath/html')
var doc = fs.readFileSync('example.md')
var result = micromark(doc, {
extensions: [math],
htmlExtensions: [mathHtml()]
})
console.log(result)
Now, running node example
yields (abbreviated):
<p>Lift(<span class="math mathinline"><span class="katex">…</span></span>) can be determined by Lift Coefficient (<span class="math mathinline"><span class="katex">…</span></span>) like the following equation.</p>
<div class="math mathdisplay"><span class="katexdisplay"><span class="katex">…</span></span></div>
API
html(options?)
syntax
Note:
syntax
is the default export of this module,html
is available atmicromarkextensionmath/html
.
Support math.
The export of syntax
is an extension for the micromark parser (to tokenize
math in text and flow; can be passed in extensions
).
The export of html
is a function that can be called with options and returns
an extension for the default HTML compiler (to compile math with KaTeX; can
be passed in htmlExtensions
).
options
Passed to katex.renderToString
.
displayMode
is overwritten by this plugin, to false
for math in text, and
true
for math in flow.
Related

remarkjs/remark
— markdown processor powered by plugins 
micromark/micromark
— the smallest commonmarkcompliant markdown parser that exists 
syntaxtree/mdastutilmath
— mdast utility to support math 
syntaxtree/mdastutilfrommarkdown
— mdast parser usingmicromark
to create mdast from markdown 
syntaxtree/mdastutiltomarkdown
— mdast serializer to create markdown from mdast
Contribute
See contributing.md
in micromark/.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.