2023-03-21 07:17:20 -05:00
|
|
|
import type { RenderableTreeNode } from '@markdoc/markdoc';
|
|
|
|
import Markdoc from '@markdoc/markdoc';
|
2023-12-04 11:56:32 -05:00
|
|
|
import type { AstroInstance } from 'astro';
|
|
|
|
import type { HTMLString } from 'astro/runtime/server/index.js';
|
2023-05-24 15:52:22 -05:00
|
|
|
import {
|
|
|
|
createComponent,
|
2023-12-04 11:56:32 -05:00
|
|
|
createHeadAndContent,
|
|
|
|
isHTMLString,
|
2023-05-24 15:52:22 -05:00
|
|
|
render,
|
2023-12-04 11:56:32 -05:00
|
|
|
renderComponent,
|
2023-05-31 18:18:07 -05:00
|
|
|
renderScriptElement,
|
2023-12-04 11:56:32 -05:00
|
|
|
renderTemplate,
|
2023-05-31 18:18:07 -05:00
|
|
|
renderUniqueStylesheet,
|
|
|
|
unescapeHTML,
|
2023-05-24 15:52:22 -05:00
|
|
|
} from 'astro/runtime/server/index.js';
|
2023-03-21 07:17:20 -05:00
|
|
|
|
|
|
|
export type TreeNode =
|
|
|
|
| {
|
|
|
|
type: 'text';
|
2023-05-24 15:52:22 -05:00
|
|
|
content: string | HTMLString;
|
2023-03-21 07:17:20 -05:00
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: 'component';
|
|
|
|
component: AstroInstance['default'];
|
2023-05-31 18:18:07 -05:00
|
|
|
collectedLinks?: string[];
|
|
|
|
collectedStyles?: string[];
|
|
|
|
collectedScripts?: string[];
|
2023-03-21 07:17:20 -05:00
|
|
|
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 15:52:22 -05:00
|
|
|
|
2023-03-21 07:17:20 -05:00
|
|
|
const slots = {
|
2023-03-21 07:20:02 -05:00
|
|
|
default: () =>
|
|
|
|
render`${treeNode.children.map((child) =>
|
|
|
|
renderComponent(result, 'ComponentNode', ComponentNode, { treeNode: child })
|
|
|
|
)}`,
|
2023-03-21 07:17:20 -05:00
|
|
|
};
|
|
|
|
if (treeNode.type === 'component') {
|
2023-05-31 18:18:07 -05:00
|
|
|
let styles = '',
|
|
|
|
links = '',
|
|
|
|
scripts = '';
|
|
|
|
if (Array.isArray(treeNode.collectedStyles)) {
|
|
|
|
styles = treeNode.collectedStyles
|
|
|
|
.map((style: any) =>
|
|
|
|
renderUniqueStylesheet(result, {
|
|
|
|
type: 'inline',
|
|
|
|
content: style,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.join('');
|
|
|
|
}
|
|
|
|
if (Array.isArray(treeNode.collectedLinks)) {
|
|
|
|
links = treeNode.collectedLinks
|
|
|
|
.map((link: any) => {
|
|
|
|
return renderUniqueStylesheet(result, {
|
|
|
|
type: 'external',
|
|
|
|
src: link[0] === '/' ? link : '/' + link,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.join('');
|
|
|
|
}
|
|
|
|
if (Array.isArray(treeNode.collectedScripts)) {
|
|
|
|
scripts = treeNode.collectedScripts
|
|
|
|
.map((script: any) => renderScriptElement(script))
|
|
|
|
.join('');
|
|
|
|
}
|
|
|
|
|
|
|
|
const head = unescapeHTML(styles + links + scripts);
|
|
|
|
|
|
|
|
let headAndContent = createHeadAndContent(
|
|
|
|
head,
|
|
|
|
renderTemplate`${renderComponent(
|
|
|
|
result,
|
|
|
|
treeNode.component.name,
|
|
|
|
treeNode.component,
|
|
|
|
treeNode.props,
|
|
|
|
slots
|
|
|
|
)}`
|
2023-03-21 07:17:20 -05:00
|
|
|
);
|
2023-05-31 18:18:07 -05:00
|
|
|
|
|
|
|
// Let the runtime know that this component is being used.
|
2023-12-01 11:59:22 -05:00
|
|
|
result._metadata.propagators.add({
|
2023-10-02 07:54:01 -05:00
|
|
|
init() {
|
|
|
|
return headAndContent;
|
|
|
|
},
|
|
|
|
});
|
2023-05-31 18:18:07 -05:00
|
|
|
|
|
|
|
return headAndContent;
|
2023-03-21 07:17:20 -05:00
|
|
|
}
|
|
|
|
return renderComponent(result, treeNode.tag, treeNode.tag, treeNode.attributes, slots);
|
|
|
|
},
|
2023-05-31 18:18:07 -05:00
|
|
|
propagation: 'self',
|
2023-03-21 07:17:20 -05:00
|
|
|
});
|
|
|
|
|
2023-06-06 12:40:04 -05:00
|
|
|
export async function createTreeNode(node: RenderableTreeNode): Promise<TreeNode> {
|
2023-05-24 15:52:22 -05:00
|
|
|
if (isHTMLString(node)) {
|
|
|
|
return { type: 'text', content: node as HTMLString };
|
|
|
|
} else if (typeof node === 'string' || typeof node === 'number') {
|
2023-03-21 07:17:20 -05:00
|
|
|
return { type: 'text', content: String(node) };
|
|
|
|
} else if (node === null || typeof node !== 'object' || !Markdoc.Tag.isTag(node)) {
|
|
|
|
return { type: 'text', content: '' };
|
|
|
|
}
|
|
|
|
|
2023-05-31 18:18:07 -05:00
|
|
|
const children = await Promise.all(node.children.map((child) => createTreeNode(child)));
|
|
|
|
|
2023-03-27 17:04:37 -05:00
|
|
|
if (typeof node.name === 'function') {
|
|
|
|
const component = node.name;
|
2023-03-21 07:17:20 -05:00
|
|
|
const props = node.attributes;
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'component',
|
|
|
|
component,
|
|
|
|
props,
|
|
|
|
children,
|
|
|
|
};
|
2023-05-31 18:18:07 -05:00
|
|
|
} else if (isPropagatedAssetsModule(node.name)) {
|
|
|
|
const { collectedStyles, collectedLinks, collectedScripts } = node.name;
|
2023-06-06 12:40:04 -05:00
|
|
|
const component = (await node.name.getMod()).default;
|
2023-05-31 18:18:07 -05:00
|
|
|
const props = node.attributes;
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'component',
|
|
|
|
component,
|
|
|
|
collectedStyles,
|
|
|
|
collectedLinks,
|
|
|
|
collectedScripts,
|
|
|
|
props,
|
|
|
|
children,
|
|
|
|
};
|
2023-03-21 07:17:20 -05:00
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
type: 'element',
|
|
|
|
tag: node.name,
|
|
|
|
attributes: node.attributes,
|
2023-05-31 18:18:07 -05:00
|
|
|
children,
|
2023-03-21 07:17:20 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2023-05-31 18:18:07 -05:00
|
|
|
|
|
|
|
type PropagatedAssetsModule = {
|
|
|
|
__astroPropagation: true;
|
2023-06-06 12:40:04 -05:00
|
|
|
getMod: () => Promise<AstroInstance>;
|
2023-05-31 18:18:07 -05:00
|
|
|
collectedStyles: string[];
|
|
|
|
collectedLinks: string[];
|
|
|
|
collectedScripts: string[];
|
|
|
|
};
|
|
|
|
|
|
|
|
function isPropagatedAssetsModule(module: any): module is PropagatedAssetsModule {
|
|
|
|
return typeof module === 'object' && module != null && '__astroPropagation' in module;
|
|
|
|
}
|