Build-time math and diagrams without client JS
Moving KaTeX, Pele, Shiki, and Pagefind indexing to the build step so article pages ship static HTML, CSS, and inline SVG.
Contents (5)
On technical blogs, the heaviest client payload rarely comes from prose. It comes from shipping a math engine and a diagram runtime to the browser. Running MathJax or mermaid.js on the client shifts layout after first paint and forces every visit to parse hundreds of kilobytes of JavaScript.
This site moves both transforms to the build step so article pages deliver static HTML, CSS, and inline SVG.
Build pipeline
Markdown files pass through an AST pipeline before Astro emits static HTML. Remark parses math blocks and compiles Mermaid fences into inline SVG via Pele. Rehype then runs Shiki and KaTeX. After Astro writes dist/, Pagefind scans the HTML and writes separate search indexes for Korean and English.
Request and search boundary
Reading an article executes no rendering code in the browser. Only the search route lazily loads Pagefind chunks matching the current page language.
Integrating Pele as a remark plugin
obsidianmd/pele converts Mermaid syntax directly into theme-aware SVG strings using a synchronous render() call. It needs neither a DOM nor a headless browser.
import { render } from 'pele';
import type { Code, Html, Parent, Root } from 'mdast';
import type { Plugin } from 'unified';
export const remarkPele: Plugin<[], Root> = () => {
return (tree: Root) => {
let index = 0;
const walk = (node: Parent) => {
for (let i = 0; i < node.children.length; i += 1) {
const child = node.children[i];
if (child.type === 'code' && child.lang === 'mermaid') {
const { svg } = render((child as Code).value, {
idPrefix: `pele-d${index++}-`,
responsive: true,
});
node.children[i] = {
type: 'html',
value: `<figure class="pele-diagram">${svg}</figure>`,
} as Html;
} else if ('children' in child && Array.isArray(child.children)) {
walk(child as Parent);
}
}
};
walk(tree);
};
};
When a page contains multiple diagrams, marker IDs inside SVG <defs> can collide. Passing a unique idPrefix per diagram prevents arrow markers from referencing another figure’s definitions.
Latency and transfer model
Total render latency can be approximated from network round-trip time , bandwidth , asset sizes , and main-thread parse cost :
Representing payload sizes as , client-side rendering (CR) and build-time rendering (BTR) compare as:
Under exponential network jitter (), the probability of completing within budget is:
Eliminating and removes main-thread parse stalls on mobile devices and tightens tail latency.
Stage comparison
| Feature | Client-side approach | Build-time approach |
|---|---|---|
| LaTeX math | Runtime parse and reflow | remark-math and rehype-katex static HTML |
| Mermaid diagrams | mermaid.js bundle |
pele inline SVG with CSS variables |
| Syntax highlighting | Browser highlighter | Astro built-in Shiki dual themes |
| Full-text search | External API or full JSON payload | pagefind chunked per-language index |
The entire setup requires six direct packages: Astro, the Markdown remark adapter, remark-math, rehype-katex, pele, and pagefind.