0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-27 22:19:04 -05:00
astro/packages/markdown/remark/src/remark-shiki.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

22 lines
699 B
TypeScript
Raw Normal View History

import { visit } from 'unist-util-visit';
import { createShikiHighlighter, type ShikiHighlighter } from './shiki.js';
2023-11-14 15:01:58 +00:00
import type { RemarkPlugin, ShikiConfig } from './types.js';
export function remarkShiki(config?: ShikiConfig): ReturnType<RemarkPlugin> {
let highlighterAsync: Promise<ShikiHighlighter> | undefined;
2022-03-24 09:49:54 -07:00
2023-09-07 22:28:02 +08:00
return async (tree: any) => {
highlighterAsync ??= createShikiHighlighter(config);
const highlighter = await highlighterAsync;
2022-03-24 09:49:54 -07:00
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 = [];
});
};
2023-09-07 22:28:02 +08:00
}