2025-05-18 00:31:20 +03:00
|
|
|
import hljs from "../node_modules/highlight.js/es/core.js";
|
2025-05-18 15:16:53 +03:00
|
|
|
import type { MimeType } from "@triliumnext/commons";
|
|
|
|
import definitions from "./syntax_highlighting.js";
|
|
|
|
import { type HighlightOptions } from "highlight.js";
|
2025-05-18 00:31:20 +03:00
|
|
|
|
2025-05-18 15:16:53 +03:00
|
|
|
const registeredMimeTypes = new Set<string>();
|
|
|
|
const unsupportedMimeTypes = new Set<string>();
|
|
|
|
|
|
|
|
export async function ensureMimeTypes(mimeTypes: MimeType[]) {
|
|
|
|
for (const mimeType of mimeTypes) {
|
|
|
|
if (!mimeType.enabled) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const mime = mimeType.mime;
|
|
|
|
if (registeredMimeTypes.has(mime)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
registeredMimeTypes.add(mime);
|
|
|
|
const loader = definitions[mime];
|
|
|
|
if (!loader) {
|
|
|
|
unsupportedMimeTypes.add(mime);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const language = (await loader).default;
|
|
|
|
console.info(`Registered highlighting for ${mime}.`);
|
|
|
|
hljs.registerLanguage(mime, language);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function highlight(code: string, options: HighlightOptions) {
|
|
|
|
if (unsupportedMimeTypes.has(options.language)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!registeredMimeTypes.has(options.language)) {
|
|
|
|
console.warn(`Unable to find highlighting for ${code}.`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hljs.highlight(code, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const { highlightAuto } = hljs;
|