aboutsummaryrefslogtreecommitdiff
path: root/packages/integrations/mdx/src/rehype-meta-string.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/integrations/mdx/src/rehype-meta-string.ts')
-rw-r--r--packages/integrations/mdx/src/rehype-meta-string.ts17
1 files changed, 17 insertions, 0 deletions
diff --git a/packages/integrations/mdx/src/rehype-meta-string.ts b/packages/integrations/mdx/src/rehype-meta-string.ts
new file mode 100644
index 000000000..c3f2dbd2f
--- /dev/null
+++ b/packages/integrations/mdx/src/rehype-meta-string.ts
@@ -0,0 +1,17 @@
+import { visit } from 'unist-util-visit';
+
+/**
+ * Moves `data.meta` to `properties.metastring` for the `code` element node
+ * as `rehype-raw` strips `data` from all nodes, which may contain useful information.
+ * e.g. ```js {1:3} => metastring: "{1:3}"
+ */
+export default function rehypeMetaString() {
+ return function (tree: any) {
+ visit(tree, (node) => {
+ if (node.type === 'element' && node.tagName === 'code' && node.data?.meta) {
+ node.properties ??= {};
+ node.properties.metastring = node.data.meta;
+ }
+ });
+ };
+}