import type { AstroInstance } from 'astro'; import type { RenderableTreeNode } from '@markdoc/markdoc'; import Markdoc from '@markdoc/markdoc'; import { createComponent, renderComponent, render } from 'astro/runtime/server/index.js'; export type TreeNode = | { type: 'text'; content: string; } | { type: 'component'; component: AstroInstance['default']; props: Record; children: TreeNode[]; } | { type: 'element'; tag: string; attributes: Record; children: TreeNode[]; }; export const ComponentNode = createComponent({ factory(result: any, { treeNode }: { treeNode: TreeNode }) { if (treeNode.type === 'text') return render`${treeNode.content}`; const slots = { default: () => render`${treeNode.children.map((child) => renderComponent(result, 'ComponentNode', ComponentNode, { treeNode: child }) )}`, }; if (treeNode.type === 'component') { return renderComponent( result, treeNode.component.name, treeNode.component, treeNode.props, slots ); } return renderComponent(result, treeNode.tag, treeNode.tag, treeNode.attributes, slots); }, propagation: 'none', }); export function createTreeNode(node: RenderableTreeNode): TreeNode { if (typeof node === 'string' || typeof node === 'number') { return { type: 'text', content: String(node) }; } else if (node === null || typeof node !== 'object' || !Markdoc.Tag.isTag(node)) { return { type: 'text', content: '' }; } if (typeof node.name === 'function') { const component = node.name; const props = node.attributes; const children = node.children.map((child) => createTreeNode(child)); return { type: 'component', component, props, children, }; } else { return { type: 'element', tag: node.name, attributes: node.attributes, children: node.children.map((child) => createTreeNode(child)), }; } }