astro/packages/markdown/remark/src/remark-scoped-styles.ts
2021-12-22 16:11:05 -05:00

18 lines
549 B
TypeScript

import { visit } from 'unist-util-visit';
const noVisit = new Set(['root', 'html', 'text']);
/** */
export default function scopedStyles(className: string) {
const visitor = (node: any) => {
if (noVisit.has(node.type)) return;
const { data } = node;
let currentClassName = data?.hProperties?.class ?? '';
node.data = node.data || {};
node.data.hProperties = node.data.hProperties || {};
node.data.hProperties.class = `${className} ${currentClassName}`.trim();
return node;
};
return () => (tree: any) => visit(tree, visitor);
}