summaryrefslogtreecommitdiff
path: root/packages/markdown-support/src/remark-scoped-styles.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/markdown-support/src/remark-scoped-styles.ts')
-rw-r--r--packages/markdown-support/src/remark-scoped-styles.ts18
1 files changed, 18 insertions, 0 deletions
diff --git a/packages/markdown-support/src/remark-scoped-styles.ts b/packages/markdown-support/src/remark-scoped-styles.ts
new file mode 100644
index 000000000..9ca70c029
--- /dev/null
+++ b/packages/markdown-support/src/remark-scoped-styles.ts
@@ -0,0 +1,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);
+}