summaryrefslogtreecommitdiff
path: root/packages/integrations/mdx/src/rehype-meta-string.ts
blob: c3f2dbd2fe312616420389f089fab8413062d798 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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;
			}
		});
	};
}