Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): treat unloaded code block theme options as disabled

Elian Dorandd16987640d2
diff --git a/apps/client/src/services/syntax_highlight.spec.ts b/apps/client/src/services/syntax_highlight.spec.ts
index 3ea3e3a266..c79eb7ca99 100644
--- a/apps/client/src/services/syntax_highlight.spec.ts
+++ b/apps/client/src/services/syntax_highlight.spec.ts
@@ -142,6 +142,15 @@ describe("syntax_highlight", () => {
expect(mod.isSyntaxHighlightEnabled()).toBe(false);
});
+ it("is disabled while the options have not loaded yet, under either theme source", async () => {
+ const mod = await freshModule();
+ setOptions({});
+ expect(mod.isSyntaxHighlightEnabled()).toBe(false);
+
+ setOptions({ codeBlockThemeMatchesApp: "true" });
+ expect(mod.isSyntaxHighlightEnabled()).toBe(false);
+ });
+
it("uses the dark/light theme option when matching the app appearance", async () => {
const mod = await freshModule();
setOptions({
diff --git a/apps/client/src/services/syntax_highlight.ts b/apps/client/src/services/syntax_highlight.ts
index 6218603287..e6b4ba228a 100644
--- a/apps/client/src/services/syntax_highlight.ts
+++ b/apps/client/src/services/syntax_highlight.ts
@@ -13,11 +13,12 @@ import { isShare } from "./utils.js";
let highlightingLoaded = false;
function getEffectiveCodeBlockTheme(): string {
- if (options.get("codeBlockThemeMatchesApp") === "true") {
- const style = getEffectiveThemeStyle();
- return String(options.get(style === "dark" ? "codeBlockThemeDark" : "codeBlockThemeLight"));
- }
- return String(options.get("codeBlockTheme"));
+ const theme = options.get("codeBlockThemeMatchesApp") === "true"
+ ? options.get(getEffectiveThemeStyle() === "dark" ? "codeBlockThemeDark" : "codeBlockThemeLight")
+ : options.get("codeBlockTheme");
+ // `options.get()` answers undefined until the options have loaded. String() would turn that
+ // into the truthy "undefined", which `isSyntaxHighlightEnabled()` reads as a theme.
+ return String(theme ?? "");
}
// Re-apply the highlight.js theme when the effective color scheme changes, so that