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.

static-webarchitecture
Contents (5)
  1. Build pipeline
  2. Request and search boundary
  3. Integrating Pele as a remark plugin
  4. Latency and transfer model
  5. Stage comparison

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.

Markdown sourceremark-math + remark-peleremark-rehypeShiki dual-themerehype-katexStatic HTML + inline SVGPagefind per-languageindexCloudflare Pages

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.

GET /en/posts/build-time-rendering/200 OK (HTML + KaTeX + inline SVG)GET /en/search/200 OK (Search page)Lazy import /pagefind/pagefind.jsEnglish index fragmentsBrowserCloudflare EdgePagefind ChunksBrowserCloudflare EdgePagefind Chunks

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 TtotalT_{\text{total}} can be approximated from network round-trip time RR, bandwidth WW, asset sizes BB, and main-thread parse cost cjsc_{\text{js}}:

Ttotal=R+Bhtml+Bcss+BjsW+cjsBjsT_{\text{total}} = R + \frac{B_{\text{html}} + B_{\text{css}} + B_{\text{js}}}{W} + c_{\text{js}} B_{\text{js}}

Representing payload sizes as b=[Bhtml,Bcss,Bjs]⊤\mathbf{b} = [B_{\text{html}}, B_{\text{css}}, B_{\text{js}}]^{\top}, client-side rendering (CR) and build-time rendering (BTR) compare as:

[TCRTBTR]=[1/W1/W1/W+cjs1/W1/W0][BhtmlBcssBjs]+[Thydrate0]\begin{bmatrix} T_{\text{CR}} \\ T_{\text{BTR}} \end{bmatrix} = \begin{bmatrix} 1/W & 1/W & 1/W + c_{\text{js}} \\ 1/W & 1/W & 0 \end{bmatrix} \begin{bmatrix} B_{\text{html}} \\ B_{\text{css}} \\ B_{\text{js}} \end{bmatrix} + \begin{bmatrix} T_{\text{hydrate}} \\ 0 \end{bmatrix}

Under exponential network jitter (λ>0\lambda > 0), the probability of completing within budget τ\tau is:

P(T≤τ)=∫0τλe−λt dt=1−e−λτP(T \le \tau) = \int_{0}^{\tau} \lambda e^{-\lambda t}\,dt = 1 - e^{-\lambda \tau}

Eliminating BjsB_{\text{js}} and ThydrateT_{\text{hydrate}} 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.