34 lines
1.2 KiB
JavaScript
34 lines
1.2 KiB
JavaScript
import { rehypeHighlight as rehypeHighlightUniversal } from "./rehype.js";
|
|
const defaults = {
|
|
theme: {},
|
|
async highlighter(code, lang, theme, options) {
|
|
try {
|
|
if (import.meta.browser && window.sessionStorage.getItem("mdc-shiki-highlighter") === "browser") {
|
|
return import("#mdc-highlighter").then((h) => h.default(code, lang, theme, options)).catch(() => ({}));
|
|
}
|
|
return await $fetch("/api/_mdc/highlight", {
|
|
params: {
|
|
code,
|
|
lang,
|
|
theme: JSON.stringify(theme),
|
|
options: JSON.stringify(options)
|
|
}
|
|
});
|
|
} catch (e) {
|
|
if (import.meta.browser && e?.response?.status === 404) {
|
|
window.sessionStorage.setItem("mdc-shiki-highlighter", "browser");
|
|
return this.highlighter?.(code, lang, theme, options);
|
|
}
|
|
}
|
|
return Promise.resolve({ tree: [{ type: "text", value: code }], className: "", style: "" });
|
|
}
|
|
};
|
|
export default rehypeHighlight;
|
|
export function rehypeHighlight(opts = {}) {
|
|
const options = { ...defaults, ...opts };
|
|
if (typeof options.highlighter !== "function") {
|
|
options.highlighter = defaults.highlighter;
|
|
}
|
|
return rehypeHighlightUniversal(options);
|
|
}
|