chore(highlightjs): basic integration

This commit is contained in:
Elian Doran
2025-05-18 15:16:53 +03:00
parent 2c4b28c6cb
commit 4fad4de319
14 changed files with 105 additions and 34 deletions

View File

@@ -1,4 +1,4 @@
import { highlight, highlightAuto } from "@triliumnext/highlightjs";
import { ensureMimeTypes, highlight, highlightAuto } from "@triliumnext/highlightjs";
import mime_types from "./mime_types.js";
import options from "./options.js";
@@ -47,12 +47,8 @@ export async function applySingleBlockSyntaxHighlight($codeBlock: JQuery<HTMLEle
if (normalizedMimeType === mime_types.MIME_TYPE_AUTO) {
highlightedText = highlightAuto(text);
} else if (normalizedMimeType) {
const language = mime_types.getHighlightJsNameForMime(normalizedMimeType);
if (language) {
highlightedText = highlight(text, { language });
} else {
console.warn(`Unknown mime type: ${normalizedMimeType}.`);
}
await ensureMimeTypesForHighlighting();
highlightedText = highlight(text, { language: normalizedMimeType });
}
if (highlightedText) {
@@ -60,6 +56,11 @@ export async function applySingleBlockSyntaxHighlight($codeBlock: JQuery<HTMLEle
}
}
export async function ensureMimeTypesForHighlighting() {
const mimeTypes = mime_types.getMimeTypes();
await ensureMimeTypes(mimeTypes);
}
/**
* Indicates whether syntax highlighting should be enabled for code blocks, by querying the value of the `codeblockTheme` option.
* @returns whether syntax highlighting should be enabled for code blocks.