64432bcb87
* Upgrade @astrojs/prism to a real package, fix component import not working * Remove `@astrojs/prism` as a dependency of `astro` * Update lock file * Refactor to multiple files * Oops, can't have astro imports run inside node * Follow Nate's suggestion on being minors instead of patchs * Update lockfile
32 lines
850 B
TypeScript
32 lines
850 B
TypeScript
import { runHighlighterWithAstro } from '@astrojs/prism/dist/highlighter';
|
|
import { visit } from 'unist-util-visit';
|
|
const noVisit = new Set(['root', 'html', 'text']);
|
|
|
|
type MaybeString = string | null | undefined;
|
|
|
|
/** */
|
|
function transformer(className: MaybeString) {
|
|
return function (tree: any) {
|
|
const visitor = (node: any) => {
|
|
let { lang, value } = node;
|
|
node.type = 'html';
|
|
|
|
let { html, classLanguage } = runHighlighterWithAstro(lang, value);
|
|
let classes = [classLanguage];
|
|
if (className) {
|
|
classes.push(className);
|
|
}
|
|
node.value = `<pre class="${classes.join(
|
|
' '
|
|
)}"><code is:raw class="${classLanguage}">${html}</code></pre>`;
|
|
return node;
|
|
};
|
|
return visit(tree, 'code', visitor);
|
|
};
|
|
}
|
|
|
|
function plugin(className: MaybeString) {
|
|
return transformer.bind(null, className);
|
|
}
|
|
|
|
export default plugin;
|