UNPKG

prosemirror-highlight

Version:

A ProseMirror plugin to highlight code blocks

44 lines (42 loc) 1.43 kB
import { Decoration } from "prosemirror-view"; //#region src/shiki.ts function createParser(highlighter, options) { return function parser({ content, language, pos, size }) { const decorations = []; const { tokens, fg, bg, rootStyle } = highlighter.codeToTokens(content, { lang: language, ...options ?? { theme: highlighter.getLoadedThemes()[0] } }); const style = rootStyle || (fg && bg ? `--prosemirror-highlight:${fg};--prosemirror-highlight-bg:${bg}` : ""); if (style) { const decoration = Decoration.node(pos, pos + size, { style }); decorations.push(decoration); } let from = pos + 1; for (const line of tokens) { for (const token of line) { const to = from + token.content.length; const decoration = Decoration.inline(from, to, { style: stringifyTokenStyle(token.htmlStyle ?? `color: ${token.color}`), class: "shiki" }); decorations.push(decoration); from = to; } from += 1; } return decorations; }; } /** * Copied from https://github.com/shikijs/shiki/blob/v3.22.0/packages/core/src/utils/tokens.ts#L151 * * Copy instead of import it from `shiki` to avoid importing the `shiki` package in this file. */ function stringifyTokenStyle(token) { if (typeof token === "string") return token; return Object.entries(token).map(([key, value]) => `${key}:${value}`).join(";"); } //#endregion export { createParser }; //# sourceMappingURL=shiki.js.map