fumadocs-core
Version:
The React.js library for building a documentation website
80 lines (78 loc) • 2.52 kB
JavaScript
import { Fragment } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { toJsxRuntime } from "hast-util-to-jsx-runtime";
//#region src/highlight/shiki.ts
const defaultThemes = {
light: "github-light",
dark: "github-dark"
};
const highlighters = /* @__PURE__ */ new Map();
async function highlightHast(code, options) {
const { lang: initialLang, fallbackLanguage, components: _, engine = "js", ...rest } = options;
let lang = initialLang;
let themes;
let themesToLoad;
if ("theme" in options && options.theme) {
themes = { theme: options.theme };
themesToLoad = [themes.theme];
} else {
themes = { themes: "themes" in options && options.themes ? options.themes : defaultThemes };
themesToLoad = Object.values(themes.themes).filter((v) => v !== void 0);
}
const highlighter = await getHighlighter(engine, {
langs: [],
themes: themesToLoad
});
try {
await highlighter.loadLanguage(lang);
} catch {
lang = fallbackLanguage ?? "text";
await highlighter.loadLanguage(lang);
}
return highlighter.codeToHast(code, {
lang,
...rest,
...themes,
defaultColor: "themes" in themes ? false : void 0
});
}
function hastToJsx(hast, options) {
return toJsxRuntime(hast, {
jsx,
jsxs,
development: false,
Fragment,
...options
});
}
/**
* Get Shiki highlighter instance of Fumadocs (mostly for internal use, you should use Shiki directly over this).
*
* @param engineType - Shiki Regex engine to use.
* @param options - Shiki options.
*/
async function getHighlighter(engineType, options) {
const { createHighlighter } = await import("shiki");
let highlighter = highlighters.get(engineType);
if (!highlighter) {
let engine;
if (engineType === "js") engine = import("shiki/engine/javascript").then((res) => res.createJavaScriptRegexEngine());
else engine = import("shiki/engine/oniguruma").then((res) => res.createOnigurumaEngine(import("shiki/wasm")));
highlighter = createHighlighter({
...options,
engine
});
highlighters.set(engineType, highlighter);
return highlighter;
}
return highlighter.then(async (instance) => {
await Promise.all([instance.loadLanguage(...options.langs), instance.loadTheme(...options.themes)]);
return instance;
});
}
async function highlight(code, options) {
return hastToJsx(await highlightHast(code, options), { components: options.components });
}
//#endregion
export { highlightHast as a, highlight as i, getHighlighter as n, hastToJsx as r, defaultThemes as t };
//# sourceMappingURL=shiki-4oMYwHED.js.map