Skip to content

Build your personal knowledge base with Trilium Notes

fix(markdown): ToC sometimes not rendering

Elian Doran6bc6a9aa8769
diff --git a/apps/client/src/components/note_context.ts b/apps/client/src/components/note_context.ts
index f2504a73f8..4a5a65c265 100644
--- a/apps/client/src/components/note_context.ts
+++ b/apps/client/src/components/note_context.ts
@@ -149,17 +149,12 @@ class NoteContext extends Component implements EventListener<"entitiesReloaded">
if (this.noteId !== previousNoteId) {
const oldKeys = Array.from(this.contextData.keys());
this.contextData.clear();
- if (oldKeys.length > 0) {
- // Notify subscribers asynchronously to avoid blocking navigation
- window.setTimeout(() => {
- for (const key of oldKeys) {
- this.triggerEvent("contextDataChanged", {
- noteContext: this,
- key,
- value: undefined
- });
- }
- }, 0);
+ for (const key of oldKeys) {
+ this.triggerEvent("contextDataChanged", {
+ noteContext: this,
+ key,
+ value: undefined
+ });
}
}
diff --git a/apps/client/src/widgets/type_widgets/code/Markdown.tsx b/apps/client/src/widgets/type_widgets/code/Markdown.tsx
index b98c4ceef9..536e046774 100644
--- a/apps/client/src/widgets/type_widgets/code/Markdown.tsx
+++ b/apps/client/src/widgets/type_widgets/code/Markdown.tsx
@@ -5,7 +5,7 @@ import { CustomMarkdownRenderer, renderToHtml } from "@triliumnext/commons";
import DOMPurify from "dompurify";
import { Marked, type Tokens } from "marked";
import { createContext } from "preact";
-import { useContext, useEffect, useMemo, useState } from "preact/hooks";
+import { useCallback, useContext, useEffect, useMemo, useState } from "preact/hooks";
import appContext from "../../../components/app_context";
import NoteContext from "../../../components/note_context";
@@ -18,7 +18,7 @@ import server from "../../../services/server";
import { removeIndividualBinding } from "../../../services/shortcuts";
import tree from "../../../services/tree";
import utils, { isDesktop } from "../../../services/utils";
-import { useLegacyImperativeHandlers } from "../../react/hooks";
+import { useLegacyImperativeHandlers, useTriliumEvent } from "../../react/hooks";
import SplitEditor from "../helpers/SplitEditor";
import { ReadOnlyTextContent } from "../text/ReadOnlyText";
import { TypeWidgetProps } from "../type_widget";
@@ -87,7 +87,7 @@ export default function Markdown(props: TypeWidgetProps) {
useSyncedScrolling(editorView, previewEl);
useSyncedHighlight(editorView, previewEl, html);
- usePublishToc(props.noteContext, editorView, headings);
+ usePublishToc(props.noteContext, editorView, headings, props.note);
useImageDrop(props.note, editorView);
useTextCommands(props.parentComponent, editorView);
useSlashCommands(props.parentComponent, editorView, props.note);
@@ -133,10 +133,11 @@ function MarkdownPreview({ ntxId }: { ntxId: TypeWidgetProps["ntxId"] }) {
function usePublishToc(
noteContext: NoteContext | undefined,
editorView: VanillaCodeMirror | null,
- headings: MarkdownHeading[]
+ headings: MarkdownHeading[],
+ note: FNote
) {
- useEffect(() => {
- if (!noteContext) return;
+ const publish = useCallback(() => {
+ if (!noteContext || noteContext.noteId !== note.noteId) return;
noteContext.setContextData("toc", {
headings,
scrollToHeading(heading) {
@@ -150,7 +151,19 @@ function usePublishToc(
editorView.scrollDOM.scrollTo({ top: targetTop, behavior: "smooth" });
}
});
- }, [ noteContext, headings, editorView ]);
+ }, [ noteContext, headings, editorView, note.noteId ]);
+
+ // Publish when headings or editor change.
+ useEffect(() => { publish(); }, [ publish ]);
+
+ // Re-publish after note switches: context data is cleared when the noteId
+ // changes, so when our note becomes active again we need to restore it.
+ // The noteId guard in publish() prevents overwriting another widget's ToC.
+ useTriliumEvent("noteSwitched", ({ noteContext: ctx }) => {
+ if (ctx === noteContext) {
+ publish();
+ }
+ });
}
//#endregion