-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomCodeComponent.js
More file actions
32 lines (30 loc) · 1.01 KB
/
customCodeComponent.js
File metadata and controls
32 lines (30 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { visit } from 'unist-util-visit';
/*
* Transforms Markdown code fences to the <Code /> component. Our <Code />
* component is a slim wrapper around Astro's default <Code /> component that
* adds support for the `title` property as well as some custom styling. For
* example, the following Markdown/MDX will render the `hello_world.py` as a
* title for the code block:
*
* ```py title="hello_world.py"
* print("Hello World!")
* ```
*/
export default function customCodeComponent() {
return () => {
return (tree) => {
visit(tree, 'code', (node, index, parent) => {
const componentNode = {
type: 'mdxJsxFlowElement',
name: 'Code',
attributes: [
{ type: 'mdxJsxAttribute', name: 'lang', value: node.lang },
{ type: 'mdxJsxAttribute', name: 'code', value: node.value },
{ type: 'mdxJsxAttribute', name: 'meta', value: node.meta },
],
};
parent.children.splice(index, 1, componentNode);
});
};
};
}