Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
perf(client): load highlight.js lazily
Elian Doran9eed6eddd777
diff --git a/apps/client/src/services/syntax_highlight.spec.ts b/apps/client/src/services/syntax_highlight.spec.tsindex 0c0a5ec2d2..e980c29e12 100644--- a/apps/client/src/services/syntax_highlight.spec.ts+++ b/apps/client/src/services/syntax_highlight.spec.ts@@ -409,8 +409,9 @@ describe("syntax_highlight", () => {hl.loadTheme.mockClear();// OS scheme change with matchesApp=true + highlightingLoaded -> re-load theme+ // (the handler resolves highlight.js via a dynamic import, hence the waitFor)changeHandler!();- expect(hl.loadTheme).toHaveBeenCalledTimes(1);+ await vi.waitFor(() => expect(hl.loadTheme).toHaveBeenCalledTimes(1));// a second ensure must NOT register the listener againawait mod.ensureMimeTypesForHighlighting("text-x-csrc");diff --git a/apps/client/src/services/syntax_highlight.ts b/apps/client/src/services/syntax_highlight.tsindex e161eb21f6..d87c7b704c 100644--- a/apps/client/src/services/syntax_highlight.ts+++ b/apps/client/src/services/syntax_highlight.ts@@ -1,5 +1,7 @@import { MimeType } from "@triliumnext/commons";-import { type AutoHighlightResult, ensureMimeTypes, highlight, highlightAuto, type HighlightResult, loadTheme, type Theme,Themes } from "@triliumnext/highlightjs";+// highlight.js is heavy and this module is reachable from the startup graph (note tooltips →+// content renderer), so the library itself is only imported dynamically when highlighting runs.+import type { AutoHighlightResult, HighlightResult, Theme } from "@triliumnext/highlightjs";import { copyText, copyTextWithToast } from "./clipboard_ext.js";import { t } from "./i18n.js";@@ -75,7 +77,7 @@ export async function formatCodeBlocks($container: JQuery<HTMLElement>) {}if (syntaxHighlightingEnabled) {- applySingleBlockSyntaxHighlight($(codeBlock), normalizedMimeType);+ await applySingleBlockSyntaxHighlight($(codeBlock), normalizedMimeType);}}@@ -137,9 +139,11 @@ export async function applySingleBlockSyntaxHighlight($codeBlock: JQuery<HTMLElelet highlightedText: HighlightResult | AutoHighlightResult | null = null;if (normalizedMimeType === mime_types.MIME_TYPE_AUTO && !isShare) {await ensureMimeTypesForHighlighting();+ const { highlightAuto } = await import("@triliumnext/highlightjs");highlightedText = highlightAuto(text);} else if (normalizedMimeType) {await ensureMimeTypesForHighlighting(normalizedMimeType);+ const { highlight } = await import("@triliumnext/highlightjs");try {highlightedText = highlight(text, { language: normalizedMimeType });} catch (e) {@@ -179,12 +183,14 @@ export async function ensureMimeTypesForHighlighting(mimeTypeHint?: string) {mimeTypes = mime_types.getMimeTypes();}+ const { ensureMimeTypes } = await import("@triliumnext/highlightjs");await ensureMimeTypes(mimeTypes);highlightingLoaded = true;}export async function loadHighlightingTheme(themeName: string) {+ const { loadTheme, Themes } = await import("@triliumnext/highlightjs");const themePrefix = "default:";let theme: Theme | null = null;if (glob.device === "print") {diff --git a/apps/client/src/widgets/sidebar/HighlightsList.tsx b/apps/client/src/widgets/sidebar/HighlightsList.tsxindex dc9a78c500..1ba0d9344c 100644--- a/apps/client/src/widgets/sidebar/HighlightsList.tsx+++ b/apps/client/src/widgets/sidebar/HighlightsList.tsx@@ -7,7 +7,7 @@ import { randomString } from "../../services/utils";import { useActiveNoteContext, useContentElement, useIsNoteReadOnly, useMathRendering, useNoteProperty, useTextEditor, useTriliumOptionJson } from "../react/hooks";import Modal from "../react/Modal";import RawHtml from "../react/RawHtml";-import { HighlightsListOptions } from "../type_widgets/options/text_notes";+import { HighlightsListOptions } from "../type_widgets/options/highlights_list_options";import RightPanelWidget from "./RightPanelWidget";interface RawHighlight {diff --git a/apps/client/src/widgets/type_widgets/options/highlights_list_options.tsx b/apps/client/src/widgets/type_widgets/options/highlights_list_options.tsxnew file mode 100644index 0000000000..5258d8eb59--- /dev/null+++ b/apps/client/src/widgets/type_widgets/options/highlights_list_options.tsx@@ -0,0 +1,30 @@+import { t } from "../../../services/i18n";+import FormText from "../../react/FormText";+import { useTriliumOptionJson } from "../../react/hooks";+import CheckboxList from "./components/CheckboxList";++/**+ * Lives in its own module (rather than in `text_notes.tsx`) because it is also rendered by the+ * always-loaded highlights list sidebar, while the rest of the text note options pull in heavy+ * dependencies such as highlight.js.+ */+export function HighlightsListOptions() {+ const [ highlightsList, setHighlightsList ] = useTriliumOptionJson<string[]>("highlightsList");++ return (+ <>+ <FormText>{t("highlights_list.description")}</FormText>+ <CheckboxList+ values={[+ { val: "bold", title: t("highlights_list.bold") },+ { val: "italic", title: t("highlights_list.italic") },+ { val: "underline", title: t("highlights_list.underline") },+ { val: "color", title: t("highlights_list.color") },+ { val: "bgColor", title: t("highlights_list.bg_color") }+ ]}+ keyProperty="val" titleProperty="title"+ currentValue={highlightsList} onChange={setHighlightsList}+ />+ </>+ );+}diff --git a/apps/client/src/widgets/type_widgets/options/text_notes.tsx b/apps/client/src/widgets/type_widgets/options/text_notes.tsxindex 8074e34bde..ef7a482955 100644--- a/apps/client/src/widgets/type_widgets/options/text_notes.tsx+++ b/apps/client/src/widgets/type_widgets/options/text_notes.tsx@@ -15,14 +15,14 @@ import { FormListItem } from "../../react/FormList";import FormSelect, { FormSelectGroup, FormSelectWithGroups } from "../../react/FormSelect";import FormText from "../../react/FormText";import FormTextBox, { FormTextBoxWithUnit } from "../../react/FormTextBox";-import { useColorScheme, useTriliumOption, useTriliumOptionBool, useTriliumOptionJson } from "../../react/hooks";+import { useColorScheme, useTriliumOption, useTriliumOptionBool } from "../../react/hooks";import { getHtml } from "../../react/RawHtml";-import CheckboxList from "./components/CheckboxList";import OptionsRow, { OptionsRowWithToggle } from "./components/OptionsRow";import OptionsSection from "./components/OptionsSection";import RadioWithIllustration from "./components/RadioWithIllustration";import RelatedSettings from "./components/RelatedSettings";import ThemeModeSelector from "./components/ThemeModeSelector";+import { HighlightsListOptions } from "./highlights_list_options";const isNewLayout = isExperimentalFeatureEnabled("new-layout");@@ -442,24 +442,3 @@ function HighlightsList() {</OptionsSection>);}--export function HighlightsListOptions() {- const [ highlightsList, setHighlightsList ] = useTriliumOptionJson<string[]>("highlightsList");-- return (- <>- <FormText>{t("highlights_list.description")}</FormText>- <CheckboxList- values={[- { val: "bold", title: t("highlights_list.bold") },- { val: "italic", title: t("highlights_list.italic") },- { val: "underline", title: t("highlights_list.underline") },- { val: "color", title: t("highlights_list.color") },- { val: "bgColor", title: t("highlights_list.bg_color") }- ]}- keyProperty="val" titleProperty="title"- currentValue={highlightsList} onChange={setHighlightsList}- />- </>- );-}