math()
Enable math expressions with inline and display syntax.
import { math } from "@unifast/node";Signature
function math(): UnifastPluginParameters
None.
Usage
import { compile, math } from "@unifast/node";
const md = `
Einstein's famous equation: $E = mc^2$
The sum of integers from 1 to n:
$$
\sum_{i=1}^{n} i = \frac{n(n+1)}{2}
$$
`;
const result = compile(md, { plugins: [math()] });Examples
Math expressions
Inline math
Use single $ delimiters for inline math expressions.
import { compile, math } from "@unifast/node";
const md = `
The quadratic formula is $x = \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}$ and is used to solve quadratic equations.
`;
const result = compile(md, { plugins: [math()] });Display math
Use $$ delimiters or a code block with math language for display math.
import { compile, math } from "@unifast/node";
const md = `
$$
\\int_{0}^{\\infty} e^{-x^2} dx = \\frac{\\sqrt{\\pi}}{2}
$$
`;
const result = compile(md, { plugins: [math()] });Including KaTeX CSS
The plugin converts math syntax into the appropriate HTML structure, but you need to include KaTeX CSS separately for proper rendering in the browser.
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css"
crossorigin="anonymous"
/>