UNPKG

@mantine/code-highlight

Version:

Code highlight with Mantine theme

48 lines (45 loc) 1.4 kB
import { light, dark } from './shiki-themes.mjs'; function stripElement(openTag, data) { const openIndex = data.indexOf(`<${openTag}`); let closeIndex = openIndex + openTag.length; for (let i = openIndex; i < data.length; i++) { if (data[i] === ">") { closeIndex = i; break; } } const striped = data.slice(0, openIndex) + data.slice(closeIndex + 1); return striped.replace(`</${openTag}>`, ""); } function stripShikiCodeBlocks(data) { return stripElement("code", stripElement("pre", data)); } const createShikiAdapter = (loadShiki, { forceColorScheme } = {}) => { return { loadContext: loadShiki, getHighlighter: (ctx) => { if (!ctx) { return ({ code }) => ({ highlightedCode: code, isHighlighted: false }); } return ({ code, language, colorScheme }) => { let _colorScheme = colorScheme; if (colorScheme === "light") { _colorScheme = light; } else if (colorScheme === "dark") { _colorScheme = dark; } return { isHighlighted: true, highlightedCode: stripShikiCodeBlocks( ctx.codeToHtml(code, { lang: language, theme: forceColorScheme || _colorScheme }) ) }; }; } }; }; export { createShikiAdapter, stripShikiCodeBlocks }; //# sourceMappingURL=shiki-adapter.mjs.map