Skip to content

Build your personal knowledge base with Trilium Notes

fix(toc): not rendering math the first time

Elian Doran4c02d70dae8b
diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsx
index 251463fa9a..6c70703385 100644
--- a/apps/client/src/widgets/react/hooks.tsx
+++ b/apps/client/src/widgets/react/hooks.tsx
@@ -1447,24 +1447,29 @@ export function useColorScheme() {
export function useMathRendering(containerRef: RefObject<HTMLElement>, deps: unknown[]) {
useEffect(() => {
if (!containerRef.current) return;
- // Support both read-only (.math-tex) and CKEditor editing view (.ck-math-tex) classes
- const mathElements = containerRef.current.querySelectorAll(".math-tex, .ck-math-tex");
+ const mathElements = containerRef.current.querySelectorAll(".math-tex");
for (const mathEl of mathElements) {
// Skip if already rendered by KaTeX
if (mathEl.querySelector(".katex")) continue;
try {
- let equation = mathEl.textContent || "";
-
- // CKEditor widgets store equation without delimiters, add them for KaTeX
- if (mathEl.classList.contains("ck-math-tex")) {
- // Check if it's display mode or inline
- const isDisplay = mathEl.classList.contains("ck-math-tex-display");
- equation = isDisplay ? `\\[${equation}\\]` : `\\(${equation}\\)`;
+ // CKEditor's data format wraps the equation with \(...\) or \[...\]
+ // delimiters. katex.render() expects raw LaTeX without them.
+ const raw = mathEl.textContent?.trim() ?? "";
+ let equation: string;
+ let displayMode = false;
+
+ if (raw.startsWith("\\(") && raw.endsWith("\\)")) {
+ equation = raw.slice(2, -2);
+ } else if (raw.startsWith("\\[") && raw.endsWith("\\]")) {
+ equation = raw.slice(2, -2);
+ displayMode = true;
+ } else {
+ equation = raw;
}
- math.render(equation, mathEl as HTMLElement);
+ math.render(equation, mathEl as HTMLElement, { displayMode });
} catch (e) {
console.warn("Failed to render math:", e);
}
diff --git a/apps/client/src/widgets/sidebar/TableOfContents.tsx b/apps/client/src/widgets/sidebar/TableOfContents.tsx
index aca1c11c5b..4442af884c 100644
--- a/apps/client/src/widgets/sidebar/TableOfContents.tsx
+++ b/apps/client/src/widgets/sidebar/TableOfContents.tsx
@@ -200,17 +200,34 @@ function extractTocFromTextEditor(editor: CKTextEditor) {
const level = Number(item.name.replace( 'heading', '' ));
- // Convert model element to view, then to DOM to get HTML
+ // Convert model element to view, then to DOM to get HTML.
+ // Math UIElements render their KaTeX content asynchronously, so
+ // ck-math-tex spans may be empty at read time. Replace them with
+ // math-tex spans (the data format) using the equation from the model,
+ // so useMathRendering can render them synchronously in the sidebar.
const viewEl = editor.editing.mapper.toViewElement(item);
let text = '';
if (viewEl) {
const domEl = editor.editing.view.domConverter.mapViewToDom(viewEl);
if (domEl instanceof HTMLElement) {
- text = domEl.innerHTML;
+ const clone = domEl.cloneNode(true) as HTMLElement;
+ const ckMathSpans = clone.querySelectorAll('.ck-math-tex');
+ let mathIdx = 0;
+ for (const child of item.getChildren()) {
+ if (!child.is('element', 'mathtex-inline')) continue;
+ if (mathIdx >= ckMathSpans.length) break;
+ const equation = String(child.getAttribute('equation') ?? '');
+ const span = document.createElement('span');
+ span.className = 'math-tex';
+ span.textContent = `\\(${equation}\\)`;
+ ckMathSpans[mathIdx].replaceWith(span);
+ mathIdx++;
+ }
+ text = clone.innerHTML;
}
}
- // Fallback to plain text if conversion fails
+ // Fallback to plain text if DOM conversion fails
if (!text) {
text = Array.from( item.getChildren() )
.map( c => c.is( '$text' ) ? c.data : '' )