mirror of
https://github.com/zadam/trilium.git
synced 2025-11-03 03:46:37 +01:00
chore(highlightjs): basic integration
This commit is contained in:
@@ -1,3 +1,46 @@
|
||||
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";
|
||||
|
||||
export const { highlight, highlightAuto } = hljs;
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user