fix(highlightjs): integrate with the rest of the application

This commit is contained in:
Elian Doran 2025-05-18 10:05:02 +03:00
parent 5205320d44
commit 51f2b9fc81
No known key found for this signature in database
4 changed files with 5 additions and 17 deletions

View File

@ -121,6 +121,5 @@ export default {
requireCss,
requireLibrary,
loadHighlightingTheme,
KATEX,
HIGHLIGHT_JS
KATEX
};

View File

@ -1,4 +1,4 @@
import library_loader from "./library_loader.js";
import { highlight, highlightAuto } from "@triliumnext/highlightjs";
import mime_types from "./mime_types.js";
import options from "./options.js";
@ -43,17 +43,13 @@ export async function applySingleBlockSyntaxHighlight($codeBlock: JQuery<HTMLEle
$codeBlock.parent().toggleClass("hljs");
const text = $codeBlock.text();
if (!window.hljs) {
await library_loader.requireLibrary(library_loader.HIGHLIGHT_JS);
}
let highlightedText = null;
if (normalizedMimeType === mime_types.MIME_TYPE_AUTO) {
highlightedText = hljs.highlightAuto(text);
highlightedText = highlightAuto(text);
} else if (normalizedMimeType) {
const language = mime_types.getHighlightJsNameForMime(normalizedMimeType);
if (language) {
highlightedText = hljs.highlight(text, { language });
highlightedText = highlight(text, { language });
} else {
console.warn(`Unknown mime type: ${normalizedMimeType}.`);
}

View File

@ -124,13 +124,6 @@ declare global {
var __non_webpack_require__: RequireMethod | undefined;
// Libraries
// TODO: Replace once library loader is replaced with webpack.
var hljs: {
highlightAuto(text: string);
highlight(text: string, {
language: string
});
};
var renderMathInElement: (element: HTMLElement, options: {
trust: boolean;
}) => void;

View File

@ -91,7 +91,7 @@ export default class CodeBlockOptions extends OptionsWidget {
#setupPreview(shouldEnableSyntaxHighlight: boolean) {
const text = SAMPLE_CODE;
if (shouldEnableSyntaxHighlight) {
library_loader.requireLibrary(library_loader.HIGHLIGHT_JS).then(() => {
import("@triliumnext/highlightjs").then((hljs) => {
const highlightedText = hljs.highlight(text, {
language: SAMPLE_LANGUAGE
});