2023-03-21 12:17:20 +00:00
|
|
|
import type { AstroInstance } from 'astro';
|
2023-04-03 15:27:51 +00:00
|
|
|
import { Fragment } from 'astro/jsx-runtime';
|
2023-03-21 12:17:20 +00:00
|
|
|
import type { RenderableTreeNode } from '@markdoc/markdoc';
|
|
|
|
import Markdoc from '@markdoc/markdoc';
|
2023-05-24 20:52:22 +00:00
|
|
|
import {
|
|
|
|
createComponent,
|
|
|
|
renderComponent,
|
|
|
|
render,
|
|
|
|
HTMLString,
|
|
|
|
isHTMLString,
|
|
|
|
} from 'astro/runtime/server/index.js';
|
2023-03-21 12:17:20 +00:00
|
|
|
|
|
|
|
export type TreeNode =
|
|
|
|
| {
|
|
|
|
type: 'text';
|
2023-05-24 20:52:22 +00:00
|
|
|
content: string | HTMLString;
|
2023-03-21 12:17:20 +00:00
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: 'component';
|
|
|
|
component: AstroInstance['default'];
|
|
|
|
props: Record<string, any>;
|
|
|
|
children: TreeNode[];
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: 'element';
|
|
|
|
tag: string;
|
|
|
|
attributes: Record<string, any>;
|
|
|
|
children: TreeNode[];
|
|
|
|
};
|
|
|
|
|
|
|
|
export const ComponentNode = createComponent({
|
|
|
|
factory(result: any, { treeNode }: { treeNode: TreeNode }) {
|
|
|
|
if (treeNode.type === 'text') return render`${treeNode.content}`;
|
2023-05-24 20:52:22 +00:00
|
|
|
|
2023-03-21 12:17:20 +00:00
|
|
|
const slots = {
|
2023-03-21 12:20:02 +00:00
|
|
|
default: () =>
|
|
|
|
render`${treeNode.children.map((child) =>
|
|
|
|
renderComponent(result, 'ComponentNode', ComponentNode, { treeNode: child })
|
|
|
|
)}`,
|
2023-03-21 12:17:20 +00:00
|
|
|
};
|
|
|
|
if (treeNode.type === 'component') {
|
2023-05-23 16:16:50 +00:00
|
|
|
return renderComponent(
|
|
|
|
result,
|
|
|
|
treeNode.component.name,
|
|
|
|
treeNode.component,
|
|
|
|
treeNode.props,
|
|
|
|
slots
|
2023-03-21 12:17:20 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return renderComponent(result, treeNode.tag, treeNode.tag, treeNode.attributes, slots);
|
|
|
|
},
|
2023-05-23 16:16:50 +00:00
|
|
|
propagation: 'none',
|
2023-03-21 12:17:20 +00:00
|
|
|
});
|
|
|
|
|
2023-05-23 16:16:50 +00:00
|
|
|
export function createTreeNode(node: RenderableTreeNode | RenderableTreeNode[]): TreeNode {
|
2023-05-24 20:52:22 +00:00
|
|
|
if (isHTMLString(node)) {
|
|
|
|
return { type: 'text', content: node as HTMLString };
|
|
|
|
} else if (typeof node === 'string' || typeof node === 'number') {
|
2023-03-21 12:17:20 +00:00
|
|
|
return { type: 'text', content: String(node) };
|
2023-04-03 15:27:51 +00:00
|
|
|
} else if (Array.isArray(node)) {
|
|
|
|
return {
|
|
|
|
type: 'component',
|
|
|
|
component: Fragment,
|
|
|
|
props: {},
|
2023-05-23 16:16:50 +00:00
|
|
|
children: node.map((child) => createTreeNode(child)),
|
2023-04-03 15:27:51 +00:00
|
|
|
};
|
2023-03-21 12:17:20 +00:00
|
|
|
} else if (node === null || typeof node !== 'object' || !Markdoc.Tag.isTag(node)) {
|
|
|
|
return { type: 'text', content: '' };
|
|
|
|
}
|
|
|
|
|
2023-03-27 22:04:37 +00:00
|
|
|
if (typeof node.name === 'function') {
|
|
|
|
const component = node.name;
|
2023-03-21 12:17:20 +00:00
|
|
|
const props = node.attributes;
|
2023-05-23 16:16:50 +00:00
|
|
|
const children = node.children.map((child) => createTreeNode(child));
|
2023-03-21 12:17:20 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'component',
|
|
|
|
component,
|
|
|
|
props,
|
|
|
|
children,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
type: 'element',
|
|
|
|
tag: node.name,
|
|
|
|
attributes: node.attributes,
|
2023-05-23 16:16:50 +00:00
|
|
|
children: node.children.map((child) => createTreeNode(child)),
|
2023-03-21 12:17:20 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|