summaryrefslogtreecommitdiff
path: root/packages/markdown/remark/src/remark-scoped-styles.ts
blob: ba8780bb7026d5bd0ea16d0336b5e697ff0d1c38 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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);
}