Mermaid and KaTeX

~4 min read

Mermaid and KaTeX

Diagrams and math, no plugins needed.

Mermaid diagrams

Use a fenced code block with language mermaid:

```mermaid
flowchart LR
    Idea --> Note
    Note --> Draft
    Draft --> Published
    Draft --> Archived
```

Lettuce ships Mermaid 11 bundled in the app and the static site, so diagrams render without an internet connection. Supported diagram types include:

Full reference: https://mermaid.js.org/intro/.

Theme

Mermaid uses Lettuce’s current theme automatically (light or dark). Override per-diagram:

```mermaid
%%{init: { 'theme': 'forest' } }%%
flowchart LR
    A --> B
```

Data-driven charts

For quantitative data (rather than node-and-edge diagrams), use a fenced code block with language chart and a small JSON spec. Lettuce renders it to an inline SVG with a bundled, dependency-free renderer — no network, works in the app, the static site, and slide decks:

```chart
{
  "type": "bar",
  "title": "Notes added per month",
  "labels": ["Jan", "Feb", "Mar"],
  "series": [{ "name": "2026", "data": [12, 19, 14] }]
}
```

If the spec isn’t valid JSON the block degrades to its readable source text rather than failing. Charts inherit the page’s text color (currentColor) so they track light/dark automatically. For flowcharts, sequence diagrams, and the like, use Mermaid above; for data series, use chart.

Dynamic note queries

A query (or dataview) fenced block resolves at publish time into a static table or list of the notes that match — so an index page can list “every note tagged #fruit, sorted by date” without you hand-maintaining the list. The spec is line-based key: value:

```query
from: #fruit
fields: title, status
sort: -date
limit: 20
```

The block is resolved by lettuce publish (which has every note’s frontmatter), so it reflects the vault at build time — re-publish to refresh it. In the in-app reader the block shows its readable spec rather than the resolved table; the live table appears on the published site. See The LLM-wiki pattern for using queries to build self-updating index pages.

KaTeX math

Inline math with $...$:

Einstein famously wrote $E = mc^2$.

Display math with $$...$$:

$$
\int_{-\infty}^{\infty} e^{-x^2}\,dx = \sqrt{\pi}
$$

KaTeX is bundled (no CDN call needed). It supports a large subset of LaTeX — see the KaTeX function list.

Performance

Both Mermaid and KaTeX render lazily — only when the block is visible in the viewport. A note with 100 diagrams stays responsive; only the on-screen ones cost render time.

Gotchas