import hljs from "../node_modules/highlight.js/es/core.js"; import type { MimeType } from "@triliumnext/commons"; import definitions from "./syntax_highlighting.js"; import { type HighlightOptions } from "highlight.js"; const registeredMimeTypes = new Set(); const unsupportedMimeTypes = new Set(); 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;