forked from drizzle-team/drizzle-orm-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeComponentNode.ts
More file actions
55 lines (50 loc) · 1.39 KB
/
Copy pathmakeComponentNode.ts
File metadata and controls
55 lines (50 loc) · 1.39 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import type { BlockContent } from "mdast";
interface NodeProps {
attributes?: Record<string, string | boolean | number | undefined | null>;
}
function makeAFMDComponentNode(
hName: string,
{ attributes }: NodeProps,
...children: BlockContent[]
) {
return {
type: "afmdJsxFlowElement",
data: { hName, hProperties: attributes },
children,
};
}
export function makeMDXComponentNode(
name: string,
{ attributes = {} }: NodeProps = {},
...children: BlockContent[]
) {
return {
type: "mdxJsxFlowElement",
name,
attributes: Object.entries(attributes)
// Filter out non-truthy attributes to avoid empty attrs being parsed as `true`.
.filter(([_k, v]) => v !== false && Boolean(v))
.map(([name, value]) => ({ type: "mdxJsxAttribute", name, value })),
children,
};
}
interface ComponentNodeProps extends NodeProps {
mdx: boolean;
}
/**
* Create AST node for a custom component injection. The data type differs
* depending on if you need to inject into a MDX or Astro-flavored Markdown
* context.
*
* @example
* makeComponentNode('MyComponent', { mdx: true }, h('p', 'Paragraph inside component'))
*
*/
export function makeComponentNode(
tagName: string,
{ mdx, ...opts }: ComponentNodeProps,
...children: BlockContent[]
) {
const factory = mdx ? makeMDXComponentNode : makeAFMDComponentNode;
return factory(tagName, opts, ...children);
}