Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): race condition in syntax highlight (closes #8464)

Elian Doran8b86e17ac87e
diff --git a/apps/client/src/services/syntax_highlight.ts b/apps/client/src/services/syntax_highlight.ts
index dd2bd48b47..3b2c35f682 100644
--- a/apps/client/src/services/syntax_highlight.ts
+++ b/apps/client/src/services/syntax_highlight.ts
@@ -1,10 +1,11 @@
-import { ensureMimeTypes, highlight, highlightAuto, loadTheme, Themes, type AutoHighlightResult, type HighlightResult, type Theme } from "@triliumnext/highlightjs";
+import { MimeType } from "@triliumnext/commons";
+import { type AutoHighlightResult, ensureMimeTypes, highlight, highlightAuto, type HighlightResult, loadTheme, type Theme,Themes } from "@triliumnext/highlightjs";
+
+import { copyText, copyTextWithToast } from "./clipboard_ext.js";
+import { t } from "./i18n.js";
import mime_types from "./mime_types.js";
import options from "./options.js";
-import { t } from "./i18n.js";
-import { copyText, copyTextWithToast } from "./clipboard_ext.js";
import { isShare } from "./utils.js";
-import { MimeType } from "@triliumnext/commons";
let highlightingLoaded = false;
@@ -76,13 +77,15 @@ export async function applySingleBlockSyntaxHighlight($codeBlock: JQuery<HTMLEle
}
export async function ensureMimeTypesForHighlighting(mimeTypeHint?: string) {
- if (highlightingLoaded) {
+ if (!mimeTypeHint && highlightingLoaded) {
return;
}
// Load theme.
- const currentThemeName = String(options.get("codeBlockTheme"));
- await loadHighlightingTheme(currentThemeName);
+ if (!highlightingLoaded) {
+ const currentThemeName = String(options.get("codeBlockTheme"));
+ await loadHighlightingTheme(currentThemeName);
+ }
// Load mime types.
let mimeTypes: MimeType[];
@@ -94,7 +97,7 @@ export async function ensureMimeTypesForHighlighting(mimeTypeHint?: string) {
enabled: true,
mime: mimeTypeHint.replace("-", "/")
}
- ]
+ ];
} else {
mimeTypes = mime_types.getMimeTypes();
}
@@ -124,9 +127,9 @@ export function isSyntaxHighlightEnabled() {
if (!isShare) {
const theme = options.get("codeBlockTheme");
return !!theme && theme !== "none";
- } else {
- return true;
}
+ return true;
+
}
/**
diff --git a/packages/highlightjs/src/index.ts b/packages/highlightjs/src/index.ts
index 6638a4b04e..3016869b73 100644
--- a/packages/highlightjs/src/index.ts
+++ b/packages/highlightjs/src/index.ts
@@ -22,7 +22,6 @@ export async function ensureMimeTypes(mimeTypes: MimeType[]) {
continue;
}
- registeredMimeTypes.add(mime);
const loader = syntaxDefinitions[mime];
if (!loader) {
unsupportedMimeTypes.add(mime);
@@ -31,6 +30,7 @@ export async function ensureMimeTypes(mimeTypes: MimeType[]) {
const language = (await loader()).default;
hljs.registerLanguage(mime, language);
+ registeredMimeTypes.add(mime);
}
}