A markdown-it plugin for fenced block math
npm install markdown-it-mathblock
!Coverage



> A markdown-it plugin to render block math that feels like markdown.
Pairs well with [markdown-it-mathspan][markdown-it-mathspan].
This is a markdown-it plugin which renders block math (delimited by$$ by default) with the same behavior as [fenced code
blocks][commonmark#fenced-code-blocks] ( ` ) according to the
commonmark spec.
``md`
$$
x = -b+-sqrt(b^2 - 4ac) / 2a
$$
This is useful if you are more familiar with markdown than LaTeX or
sometimes need to include dollar signs in your math expressions.
`md`
- Function application:
$$
f$0 = 1
$$
- Calculating dollars:
$$
$a + $b = $c
$$
- Tribble dollars $$$:
$$$$
$$$
$$$$
- Also fine like this:
$$
1 + $$$ + 2
$$
`bash`
npm install --save markdown-it markdown-it-mathblock
Note: This plugin does not include a math renderer. So you must
provide your own: Here are some excellent choices:
- [mathup][mathup] (an AsciiMath Dialect):
`bash`
npm install --save mathup
`
- [Temml][temml] (LaTeX):
bash`
npm install --save temml
# And if you plan on using the
npm install --save temml-custom-element
See [mathup][mathup] for the renderer reference.
`js
import markdownIt from "markdown-it";
import markdownItMathblock from "markdown-it-mathblock";
// Optional (for default mathup renderer)
import "mathup/custom-element";
// Optional, with defaults
const options = {
minDelims: 2,
renderer, // See below
customElement, // See below
};
const md = markdownIt().use(markdownItMathblock, options);
md.render(
With block math:
$$ With block math:
x = -b+-sqrt(b^2 - 4ac) / 2a
$$);`
// Document
//
//
See [Temml][temml] for the renderer reference and
[temml-custom-element][temml-custom-element] for reference on the custom element.
`js
import markdownIt from "markdown-it";
import markdownItMathblock from "markdown-it-mathblock";
import "temml-custom-element";
const md = markdownIt().use(markdownItMathblock, {
// Do this if you want to use custom macros
customElement: ["la-tex", { display: "block", macros: "persist" }],
});
md.render(
$$ preample
\def\E{\mathbb{E}}
\newcommand\d[0]{\operatorname{d}\!}
$$
With block math:
$$ With block math:
\E[X] = \int_{-\infty}^{\infty} xf(x) \d{x}
$$);`
// Document
//
//
// \def\E{\mathbb{E}}
// \newcommand\d[0]{\operatorname{d}\!}
//
//
//
//
//
// \E[X] = \int_{-\infty}^{\infty} xf(x) \d{x}
//
- minDelims: The minimum required number of delimiters around a
math block. Set this to 3 if you want mathblocks to behave exactly
like code fences.
- renderer: The math renderer. Accepts the source, the parsed
MarkdownIt token, and the markdownIt instance. Defaults to a`
function that surrounds and escapes with a custom element (see below).
js
{
renderer: (src, token, md) =>
,`
}
customElement
- : If you want to specify which custom element to["tag-name", { attribute: "value" }]
render into, this is a convenience option to do so. Accepts a pair of
The default is:`
js`
{
customElement: ["math-up", { display: "block" }],
}
The default renderer depends on which custom element depends on which
elements are in in your [custom element registry][custom-element-registry].
It will try the following in order:
1. Use with [markdown-it-mathspan][markdown-it-mathspan]: const md = markdownIt().use(markdownItMathblock).use(markdownItMathspan); md.render( $$ Inline math Enforce thicker delimiters: ` const md = markdownIt().use(markdownItMath, { minDelims: 3 }); md.render( $$$ Use the info string. ` const md = markdownIt().use(markdownItMathblock, { return md.render( $$ latex Render the expression straight into MathML using [mathup][mathup]. You ` const md = markdownIt().use(markdownItMathblock, { md.render( Render the expression from LaTeX into MathML using [Temml][temml]. You ` const md = markdownIt().use(markdownItMathblock, { md.render( Pass in custom attributes to the renderer ` const md = markdownIt().use(markdownItMathblock, { md.render( - [markdown-it-math][markdown-it-math] - For a more LaTeX like plugin. [commonmark#fenced-code-blocks]: https://spec.commonmark.org/0.31.2/#fenced-code-blocks ([see mathup][mathup])
2. ([see Temml][temml] and
[temml-custom-element][temml-custom-element])
3. If none is found it will default to `Examples
js
import markdownIt from "markdown-it";
import markdownItMathblock from "markdown-it-mathblock";
import markdownItSpan from "markdown-it-mathspan";
import "mathup/custom-element";
Inline math $a^2 + b^2 = c^2$. And block math:
x = -b+-sqrt(b^2 - 4ac) / 2a
$$);`
//
// js
import markdownIt from "markdown-it";
import markdownItMathblock from "markdown-it-mathblock";
import "mathup/custom-element";
$$
This is not math
$$
[b; u; t] * t[h; i; s] = i_s
$$$
);
`js
import markdownIt from "markdown-it";
import markdownItMathblock from "markdown-it-mathblock";
import "mathup/custom-element";
renderer(src, token, md) {
if (token.info.trim().toLowerCase().startsWith("latex")) {
return ;
};
},
});
$$ mathup
sin x
$$
\cos \theta
$$);`
//
//
might want to include the stylesheet from mathup for this.js
import markdownIt from "markdown-it";
import markdownItMathblock from "markdown-it-mathblock";
import mathup from "mathup";
renderer: (src) => mathup(src, { display: "block" }).toString(),
});
$$
pi ~~ 3.14159
$$);`
//
might want to include the stylesheet and fonts from Temml for this.js
import markdownIt from "markdown-it";
import markdownItMathblock from "markdown-it-mathblock";
import temml from "temml";
renderer: (src) => temml.renderToString(src, { displayMode: true }),
});
$$
\sin x
$$);`
// element:js
import markdownIt from "markdown-it";
import markdownItMathblock from "markdown-it-mathblock";
import "mathup/custom-element";
customElement: ["math-up", { display: "block", "decimal-mark": "," }],
});
$$
pi ~~ 3,14159
$$);``
// See Also and References
- [markdown-it-mathspan][markdown-it-mathspan] - If you also want to include inline math.
- [mathup][mathup] - An AsciiMath dialect renderer.
- [Temml][temml] - A LaTeX math renderer.
- [Commonmark spec for fenced code blocks][commonmark#fenced-code-blocks]
[custom-element-registry]: https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry
[markdown-it-math]: https://github.com/runarberg/math
[markdown-it-mathspan]: https://github.com/runarberg/mathspan
[mathup]: https://mathup.xyz/
[temml]: https://temml.org/
[temml-custom-element]: https://github.com/runarberg/temml-custom-element