summaryrefslogtreecommitdiff
path: root/packages/markdown-support/src/remark-scoped-styles.ts
blob: 9ca70c029651bed5c1d3461d86e9b19e3cd2de21 (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);
}