From 2e2228f9c5e131d67e55965bba6b18ba86a9258f Mon Sep 17 00:00:00 2001
From: wallpants <47203170+wallpants@users.noreply.github.com>
Date: Thu, 29 Jan 2026 14:03:47 -0600
Subject: [PATCH] fix(latex): add support for "math" codeblocks
---
docs/features.md | 41 +++++++++++++++++++++++++++++++++++++++++
src/renderer.ts | 16 +++++++++++-----
2 files changed, 52 insertions(+), 5 deletions(-)
diff --git a/docs/features.md b/docs/features.md
index 28c60e2..2aa4b84 100644
--- a/docs/features.md
+++ b/docs/features.md
@@ -97,6 +97,47 @@ Here's a simple footnote,[^1] and here's a longer one.[^bignote]
---
+### LaTeX / Math
+
+```
+Inline math: $E = mc^2$ and $a^2 + b^2 = c^2$
+```
+
+Inline math: $E = mc^2$ and $a^2 + b^2 = c^2$
+
+Block math with `$$`:
+
+```
+$$
+\int_0^\infty e^{-x^2} dx = \frac{\sqrt{\pi}}{2}
+$$
+```
+
+$$
+\int_0^\infty e^{-x^2} dx = \frac{\sqrt{\pi}}{2}
+$$
+
+Math code block:
+
+````
+```math
+\sum_{n=1}^{\infty} \frac{1}{n^2} = \frac{\pi^2}{6}
+```
+````
+
+```math
+\sum_{n=1}^{\infty} \frac{1}{n^2} = \frac{\pi^2}{6}
+```
+
+> [!NOTE]
+> Consumers must include KaTeX CSS for proper rendering:
+>
+> ```html
+>
+> ```
+
+---
+
### Mermaid
Take a look at [how Pantsdown's demo is built](https://github.com/wallpants/pantsdown/blob/main/docs/build.ts)
diff --git a/src/renderer.ts b/src/renderer.ts
index ad39b86..8b418f1 100644
--- a/src/renderer.ts
+++ b/src/renderer.ts
@@ -26,18 +26,24 @@ export class Renderer {
code(code: string, infostring: string | undefined, sourceMap: SourceMap): string {
const lang = /^\S*/.exec(infostring ?? "")?.[0];
- code = code.replace(/\n$/, "") + "\n";
+ code = code.replace(/\n$/, "");
const attrs: HTMLAttrs = [];
if (lang === "mermaid") {
attrs.push(["class", "mermaid-container mermaid"]);
- } else {
- const language = lang && hljs.getLanguage(lang) ? lang : "plaintext";
- code = hljs.highlight(code, { language }).value;
- code = `${code}`;
+ const result = `
` + code + "\n" + ``; + return injectHtmlAttributes(result, attrs, sourceMap); } + if (lang === "math") { + return this.latexBlock(code, sourceMap); + } + + const language = lang && hljs.getLanguage(lang) ? lang : "plaintext"; + code = hljs.highlight(code + "\n", { language }).value; + code = `
${code}`;
+
const result = `` + code + ``; return injectHtmlAttributes(result, attrs, sourceMap); }