markdown-it-gladest

A Markdown-it plugin to render LaTeX mathematical formulas using the Typst typesetting system. It leverages a high-performance native Rust addon to generate SVG or PNG images from your math blocks.
Features
- Renders inline (
$ ... $
) and block ($$ ... $$
) LaTeX math.
- Powered by Typst for accurate and beautiful mathematical typesetting.
- Uses a native Rust addon for fast rendering.
- Outputs math as embedded SVG (default) or PNG images.
- Configurable PPI (Pixels Per Inch) for PNG output.
- Customizable fonts for body text and mathematical formulas.
- Easy integration with Markdown-it.
- Includes basic error handling display for rendering issues.
Installation
npm install markdown-it @fuuck/markdown-it-gladest
yarn add markdown-it @fuuck/markdown-it-gladest
bun add markdown-it @fuuck/markdown-it-gladest
Note: This package relies on a native Node.js addon built with Rust. Pre-compiled binaries for common platforms (Windows, macOS, Linux) are typically provided. If a pre-compiled binary is not available for your specific platform/architecture, you might need a Rust toolchain installed to build the addon during installation.
Usage
Basic Usage
import { writeFileSync } from "node:fs";
import MarkdownIt from "markdown-it";
import markdownItGladest from "@fuuck/markdown-it-gladest";
const md = new MarkdownIt().use(markdownItGladest, {
format: "svg",
ppi: 600,
});
const markdownContent = `
# Normal Distribution
The probability density function (PDF) is:
$$
f(x) = \\frac{1}{\\sigma\\sqrt{2\\pi}} e^{-\\frac{1}{2}\\left(\\frac{x-\\mu}{\\sigma}\\right)^2}
$$
Where $\\mu$ is the mean and $\\sigma$ is the standard deviation.
The standard normal distribution has $\\mu = 0$ and $\\sigma = 1$, denoted as $Z \\sim N(0,1)$.
An important transformation is standardization: $Z = \\frac{X-\\mu}{\\sigma}$.
`;
const htmlResult = md.render(markdownContent);
writeFileSync("output.html", htmlResult);
console.log("Rendered HTML saved to output.html");
Font Configuration
You can customize fonts used for rendering mathematical formulas by specifying system fonts or font files:
Using System Fonts
import MarkdownIt from "markdown-it";
import markdownItGladest from "@fuuck/markdown-it-gladest";
const md = new MarkdownIt().use(markdownItGladest, {
format: "svg",
fonts: {
bodyFont: {
system: "Times New Roman",
},
mathFont: {
system: "Latin Modern Math",
},
},
});
Using Font Files
import MarkdownIt from "markdown-it";
import markdownItGladest from "@fuuck/markdown-it-gladest";
const md = new MarkdownIt().use(markdownItGladest, {
format: "svg",
fonts: {
bodyFont: {
file: "/path/to/your/custom-font.ttf",
},
mathFont: {
file: "/path/to/your/math-font.otf",
},
},
});
Mixed Font Configuration
import MarkdownIt from "markdown-it";
import markdownItGladest from "@fuuck/markdown-it-gladest";
const md = new MarkdownIt().use(markdownItGladest, {
format: "png",
ppi: 300,
fonts: {
bodyFont: {
system: "Georgia",
},
mathFont: {
file: "/usr/share/fonts/opentype/lmodern/lmmath-regular.otf",
},
},
});
const content = `
Using custom fonts for better typography:
$$
\\sum_{i=1}^{n} \\frac{1}{i^2} = \\frac{\\pi^2}{6}
$$
The Euler's identity: $e^{i\\pi} + 1 = 0$.
`;
console.log(md.render(content));
Options
You can pass an options object when enabling the plugin with .use()
:
Syntax
The content between the delimiters is treated as LaTeX code and passed to Typst for rendering. Make sure to escape literal dollar signs in your Markdown text using a backslash: \$
.
How It Works
- Parsing: Markdown-it parses the input text.
markdown-it-gladest
registers inline and block rules to detect $ ... $
and $$ ... $$
sequences.
- Tokenization: When math delimiters are found, custom tokens (
gladst_inline_math
, gladst_block_math
) are generated, containing the LaTeX code within them.
- Rendering: The plugin provides renderer functions for these custom tokens.
- Native Call: The renderer function calls the
renderLatex
function exported by the native Rust addon, passing the LaTeX code, the delimiter type ($
or $$
), and the configured options (format
, ppi
).
- Typst Execution: The Rust addon invokes the Typst library to parse the LaTeX input and render it into the desired format (SVG or PNG).
- HTML Generation: The Rust addon returns an HTML string, typically an
<img>
tag with the image data embedded as a Base64 data URI in the src
attribute. If an error occurs during Typst rendering, an error message wrapped in a <span>
or <div>
is returned instead.
- Output: The plugin wraps the returned HTML in a
<span>
(for inline) or <div>
(for block) with appropriate CSS classes (gladst-inline
or gladst-block
) and inserts it into the final HTML output generated by Markdown-it.
Error Handling
If the Typst engine encounters an error while rendering a formula (e.g., invalid LaTeX syntax), the plugin will output an error message directly in the HTML instead of an image. This message will be wrapped in a <span class="gladst-error-inline">
or <div class="gladst-error-block">
and will often include the original formula and a summary of the error (potentially in the title
attribute for hover details). Check your browser's developer console for more detailed error messages logged by the plugin.
License
This package MIT licensed.