Skip to content

Build your personal knowledge base with Trilium Notes

Merge branch 'feature/release_bugfixes' of https://github.com/TriliumNext/Trilium into feature/release_bugfixes

Elian Doran14eaba444ca2
diff --cc apps/client/src/widgets/type_widgets/code/Markdown.tsx
index 41fb888755,cf4e744c9f..c2cd4a7815
--- a/apps/client/src/widgets/type_widgets/code/Markdown.tsx
+++ b/apps/client/src/widgets/type_widgets/code/Markdown.tsx
@@@ -1,37 -1,28 +1,45 @@@
import "./Markdown.css";
import VanillaCodeMirror from "@triliumnext/codemirror";
-import { renderToHtml } from "@triliumnext/commons";
+import { CustomMarkdownRenderer, renderToHtml } from "@triliumnext/commons";
import DOMPurify from "dompurify";
-import { Marked } from "marked";
+import { Marked, type Tokens } from "marked";
import { createContext } from "preact";
import { useContext, useEffect, useMemo, useState } from "preact/hooks";
-
+ import NoteContext from "../../../components/note_context";
import SplitEditor from "../helpers/SplitEditor";
import { ReadOnlyTextContent } from "../text/ReadOnlyText";
import { TypeWidgetProps } from "../type_widget";
const marked = new Marked({ breaks: true, gfm: true });
+/**
+ * The default {@link CustomMarkdownRenderer} falls back to
+ * `language-text-x-trilium-auto` on unlabeled fences, which drives
+ * `syntax_highlight.ts` into `highlightAuto` — that dynamic-imports every hljs
+ * language bundle on first render and runs detection on each block, which is
+ * both slow and often wrong on short snippets. For the live preview we instead
+ * emit unlabeled fences without a `language-` class so the highlighter skips
+ * them entirely.
+ */
+class MarkdownPreviewRenderer extends CustomMarkdownRenderer {
+ override code(token: Tokens.Code): string {
+ const html = super.code(token);
+ if (token.lang) return html;
+ return html.replace('<code class="language-text-x-trilium-auto">', `<code class="language-text-plain">`);
+ }
+}
+
+ export interface MarkdownHeading {
+ id: string;
+ level: number;
+ text: string;
+ line: number;
+ }
+
interface MarkdownContextValue {
html: string;
+ headings: MarkdownHeading[];
setEditorView: (view: VanillaCodeMirror | null) => void;
setPreviewEl: (el: HTMLDivElement | null) => void;
}
@@@ -192,10 -224,9 +241,10 @@@ export function renderWithSourceLines(s
const html = renderToHtml(src, "", {
sanitize: (h) => DOMPurify.sanitize(h),
wikiLink: { formatHref: (id) => `#root/${id}` },
- demoteH1: false
+ demoteH1: false,
+ renderer: new MarkdownPreviewRenderer({ async: false })
});
- if (!html) return "";
+ if (!html) return { html: "", headings };
const container = document.createElement("div");
container.innerHTML = html;