Skip to content

Build your personal knowledge base with Trilium Notes

chore(highlightjs): reintegrate loading of themes

Elian Doranb998dee47601
diff --git a/apps/client/src/services/library_loader.ts b/apps/client/src/services/library_loader.ts
index 7197d67326..c8e10275c3 100644
--- a/apps/client/src/services/library_loader.ts
+++ b/apps/client/src/services/library_loader.ts
@@ -31,8 +31,6 @@ const HIGHLIGHT_JS: Library = {
}
}
- loadHighlightingTheme(currentTheme);
-
return Array.from(scriptsToLoad);
}
};
@@ -90,35 +88,8 @@ async function requireCss(url: string, prependAssetPath = true) {
}
}
-let highlightingThemeEl: JQuery<HTMLElement> | null = null;
-function loadHighlightingTheme(theme: string) {
- if (!theme) {
- return;
- }
-
- if (theme === "none") {
- // Deactivate the theme.
- if (highlightingThemeEl) {
- highlightingThemeEl.remove();
- highlightingThemeEl = null;
- }
- return;
- }
-
- if (!highlightingThemeEl) {
- highlightingThemeEl = $(`<link rel="stylesheet" type="text/css" />`);
- $("head").append(highlightingThemeEl);
- }
-
- const url = getStylesheetUrl(theme);
- if (url) {
- highlightingThemeEl.attr("href", url);
- }
-}
-
export default {
requireCss,
requireLibrary,
- loadHighlightingTheme,
KATEX
};
diff --git a/apps/client/src/services/syntax_highlight.ts b/apps/client/src/services/syntax_highlight.ts
index b6b4dc2636..135a5808d2 100644
--- a/apps/client/src/services/syntax_highlight.ts
+++ b/apps/client/src/services/syntax_highlight.ts
@@ -59,20 +59,24 @@ export async function applySingleBlockSyntaxHighlight($codeBlock: JQuery<HTMLEle
export async function ensureMimeTypesForHighlighting() {
// Load theme.
const currentThemeName = String(options.get("codeBlockTheme"));
+ loadHighlightingTheme(currentThemeName);
+
+ // Load mime types.
+ const mimeTypes = mime_types.getMimeTypes();
+ await ensureMimeTypes(mimeTypes);
+}
+
+export function loadHighlightingTheme(themeName: string) {
const themePrefix = "default:";
let theme = null;
- if (currentThemeName.includes(themePrefix)) {
- theme = Themes[currentThemeName.substring(themePrefix.length)];
+ if (themeName.includes(themePrefix)) {
+ theme = Themes[themeName.substring(themePrefix.length)];
}
if (!theme) {
theme = Themes.default;
}
loadTheme(theme);
-
- // Load mime types.
- const mimeTypes = mime_types.getMimeTypes();
- await ensureMimeTypes(mimeTypes);
}
/**
diff --git a/apps/client/src/widgets/type_widgets/options/text_notes/code_block.ts b/apps/client/src/widgets/type_widgets/options/text_notes/code_block.ts
index 4dffa0a19a..078405dccd 100644
--- a/apps/client/src/widgets/type_widgets/options/text_notes/code_block.ts
+++ b/apps/client/src/widgets/type_widgets/options/text_notes/code_block.ts
@@ -1,9 +1,8 @@
import type { OptionMap } from "@triliumnext/commons";
import { t } from "../../../../services/i18n.js";
-import library_loader from "../../../../services/library_loader.js";
import server from "../../../../services/server.js";
import OptionsWidget from "../options_widget.js";
-import { ensureMimeTypesForHighlighting } from "../../../../services/syntax_highlight.js";
+import { ensureMimeTypesForHighlighting, loadHighlightingTheme } from "../../../../services/syntax_highlight.js";
import { Themes } from "@triliumnext/highlightjs";
const SAMPLE_LANGUAGE = "javascript";
@@ -78,7 +77,7 @@ export default class CodeBlockOptions extends OptionsWidget {
}
this.$themeSelect.on("change", async () => {
const newTheme = String(this.$themeSelect.val());
- library_loader.loadHighlightingTheme(newTheme);
+ loadHighlightingTheme(newTheme);
await server.put(`options/codeBlockTheme/${newTheme}`);
});
diff --git a/packages/highlightjs/src/index.ts b/packages/highlightjs/src/index.ts
index 1cd5af0646..3815cf563b 100644
--- a/packages/highlightjs/src/index.ts
+++ b/packages/highlightjs/src/index.ts
@@ -8,6 +8,7 @@ export { default as Themes } from "./themes.js";
const registeredMimeTypes = new Set<string>();
const unsupportedMimeTypes = new Set<string>();
+let highlightingThemeEl: HTMLStyleElement | null = null;
export async function ensureMimeTypes(mimeTypes: MimeType[]) {
for (const mimeType of mimeTypes) {
@@ -45,11 +46,22 @@ export function highlight(code: string, options: HighlightOptions) {
return hljs.highlight(code, options);
}
-export async function loadTheme(theme: Theme) {
- console.log("Got theme", theme);
+export async function loadTheme(theme: "none" | Theme) {
+ if (theme === "none") {
+ if (highlightingThemeEl) {
+ highlightingThemeEl.remove();
+ highlightingThemeEl = null;
+ }
+ return;
+ }
+
+ if (!highlightingThemeEl) {
+ highlightingThemeEl = document.createElement("style");
+ document.querySelector("head")?.append(highlightingThemeEl);
+ }
- const loadedTheme = await theme.load();
- console.log("Got", loadedTheme.default);
+ const themeCss = (await theme.load()).default as string;
+ highlightingThemeEl.textContent = themeCss;
}
export const { highlightAuto } = hljs;