Skip to content

Build your personal knowledge base with Trilium Notes

fix(math): render invalid formulas as inline KaTeX errors instead of throwing

Elian Dorandf56d711c60d
diff --git a/apps/client/src/services/content_renderer_text.ts b/apps/client/src/services/content_renderer_text.ts
index 03b39ee6fa..c26ad6b04d 100644
--- a/apps/client/src/services/content_renderer_text.ts
+++ b/apps/client/src/services/content_renderer_text.ts
@@ -42,7 +42,10 @@ export async function postProcessRichContent(note: FNote | FAttachment, $rendere
if ($renderedContent.find("span.math-tex").length > 0) {
// KaTeX is heavy, so the math service is only loaded when there are formulas to render.
const { renderMathInElement } = await import("./math.js");
- renderMathInElement($renderedContent[0], { trust: true });
+ // throwOnError: false makes KaTeX render invalid formulas as an inline red error
+ // (with the parse message as a tooltip) instead of throwing and leaving raw `$…$`
+ // text plus a console error — matching the editor's behavior.
+ renderMathInElement($renderedContent[0], { trust: true, throwOnError: false });
}
const getNoteIdFromLink = (el: HTMLElement) => tree.getNoteIdFromUrl($(el).attr("href") || "");
diff --git a/apps/client/src/widgets/dialogs/revisions.tsx b/apps/client/src/widgets/dialogs/revisions.tsx
index f7d709c68d..c9ec149bd3 100644
--- a/apps/client/src/widgets/dialogs/revisions.tsx
+++ b/apps/client/src/widgets/dialogs/revisions.tsx
@@ -697,7 +697,9 @@ function RevisionContentText({ content }: { content: string | Uint8Array | undef
// KaTeX is heavy, so the math service is only loaded when there are formulas to render.
void import("../../services/math").then(({ renderMathInElement }) => {
if (contentRef.current) {
- renderMathInElement(contentRef.current, { trust: true });
+ // throwOnError: false renders invalid formulas as an inline red error
+ // instead of throwing and logging to the console.
+ renderMathInElement(contentRef.current, { trust: true, throwOnError: false });
}
});
}
diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsx
index ac1995e4f1..b05827ffe9 100644
--- a/apps/client/src/widgets/react/hooks.tsx
+++ b/apps/client/src/widgets/react/hooks.tsx
@@ -1804,7 +1804,9 @@ export function useMathRendering(containerRef: RefObject<HTMLElement>, deps: unk
equation = raw;
}
- math.render(equation, mathEl as HTMLElement, { displayMode });
+ // throwOnError: false renders invalid formulas as an inline red error
+ // instead of throwing (the catch below stays as a final safety net).
+ math.render(equation, mathEl as HTMLElement, { displayMode, throwOnError: false });
} catch (e) {
console.warn("Failed to render math:", e);
}
diff --git a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx
index 05a19e0f48..68d5d04761 100644
--- a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx
+++ b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx
@@ -141,6 +141,8 @@ function applyIncludedNotes(container: HTMLDivElement) {
function applyMath(container: HTMLDivElement) {
const equations = container.querySelectorAll("span.math-tex");
for (const equation of equations) {
- renderMathInElement(equation, { trust: true });
+ // throwOnError: false renders invalid formulas as an inline red error (with the
+ // parse message as a tooltip) instead of throwing and logging to the console.
+ renderMathInElement(equation, { trust: true, throwOnError: false });
}
}
diff --git a/packages/share-theme/src/scripts/modules/math.ts b/packages/share-theme/src/scripts/modules/math.ts
index 3893dbeeda..dadd92122d 100644
--- a/packages/share-theme/src/scripts/modules/math.ts
+++ b/packages/share-theme/src/scripts/modules/math.ts
@@ -11,6 +11,8 @@ export default async function setupMath() {
const contentEl = document.getElementById("content");
if (!contentEl) return;
- renderMathInElement(contentEl);
+ // throwOnError: false renders invalid formulas as an inline red error instead of
+ // throwing and leaving raw `$…$` text plus a console error (matches the editor).
+ renderMathInElement(contentEl, { throwOnError: false });
document.body.classList.add("math-loaded");
}