astro/packages/markdown/remark/src/remark-shiki.ts
Juan Martín Seery 6fe1b0279f
Add Shiki as an alternative to Prism (#2497)
* [ci] yarn format

* Added shiki to markdown-remark

* Upgraded astro shiki

* Added minimal example

* Changed defaults to match <Code />

* Replace `shiki` with `astro` classes

* Added documentation

* Updated Astro code to use new `codeToHtml`

* Added changesets

* Added basic test

* Updated tests a bit

Co-authored-by: JuanM04 <JuanM04@users.noreply.github.com>
2022-01-31 16:14:07 -06:00

23 lines
731 B
TypeScript

import shiki from 'shiki';
import { visit } from 'unist-util-visit';
const remarkShiki = async (theme: shiki.Theme) => {
const highlighter = await shiki.getHighlighter({ theme });
return () => (tree: any) => {
visit(tree, 'code', (node) => {
let html = highlighter.codeToHtml(node.value, { lang: node.lang ?? 'plaintext' });
// Replace "shiki" class naming with "astro".
html = html.replace('<pre class="shiki"', '<pre class="astro-code"');
// Replace "shiki" css variable naming with "astro".
html = html.replace(/style="(background-)?color: var\(--shiki-/g, 'style="$1color: var(--astro-code-');
node.type = 'html';
node.value = html;
node.children = [];
});
};
};
export default remarkShiki;