diff --git a/apps/client/src/services/library_loader.ts b/apps/client/src/services/library_loader.ts index 7197d6732..c8e10275c 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 | 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 = $(``); - $("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 b6b4dc263..135a5808d 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 { 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 1cd5af064..3815cf563 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(); const unsupportedMimeTypes = new Set(); +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; + } - const loadedTheme = await theme.load(); - console.log("Got", loadedTheme.default); + if (!highlightingThemeEl) { + highlightingThemeEl = document.createElement("style"); + document.querySelector("head")?.append(highlightingThemeEl); + } + + const themeCss = (await theme.load()).default as string; + highlightingThemeEl.textContent = themeCss; } export const { highlightAuto } = hljs;