0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-20 22:12:38 -05:00
astro/packages/markdown/remark/src/remark-shiki.ts
2023-11-14 15:01:58 +00:00

21 lines
699 B
TypeScript

import { visit } from 'unist-util-visit';
import { createShikiHighlighter, type ShikiHighlighter } from './shiki.js';
import type { RemarkPlugin, ShikiConfig } from './types.js';
export function remarkShiki(config?: ShikiConfig): ReturnType<RemarkPlugin> {
let highlighterAsync: Promise<ShikiHighlighter> | undefined;
return async (tree: any) => {
highlighterAsync ??= createShikiHighlighter(config);
const highlighter = await highlighterAsync;
visit(tree, 'code', (node) => {
const lang = typeof node.lang === 'string' ? node.lang : 'plaintext';
const html = highlighter.highlight(node.value, lang);
node.type = 'html';
node.value = html;
node.children = [];
});
};
}