2023-03-21 12:17:20 +00:00
|
|
|
import type { AstroInstance } from 'astro';
|
|
|
|
import type { RenderableTreeNode } from '@markdoc/markdoc';
|
|
|
|
import Markdoc from '@markdoc/markdoc';
|
2023-05-24 20:52:22 +00:00
|
|
|
import {
|
|
|
|
createComponent,
|
|
|
|
renderComponent,
|
|
|
|
render,
|
2023-05-31 23:18:07 +00:00
|
|
|
renderScriptElement,
|
|
|
|
renderUniqueStylesheet,
|
|
|
|
createHeadAndContent,
|
|
|
|
unescapeHTML,
|
|
|
|
renderTemplate,
|
2023-05-24 20:52:22 +00:00
|
|
|
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'];
|
2023-05-31 23:18:07 +00:00
|
|
|
collectedLinks?: string[];
|
|
|
|
collectedStyles?: string[];
|
|
|
|
collectedScripts?: string[];
|
2023-03-21 12:17:20 +00: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 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-31 23:18:07 +00: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 12:17:20 +00:00
|
|
|
);
|
2023-05-31 23:18:07 +00:00
|
|
|
|
|
|
|
// Let the runtime know that this component is being used.
|
2023-07-05 16:25:24 +00:00
|
|
|
// `result.propagators` has been moved to `result._metadata.propagators`
|
|
|
|
// TODO: remove this fallback in the next markdoc integration major
|
|
|
|
const propagators = result._metadata.propagators || result.propagators;
|
2023-10-02 12:54:01 +00:00
|
|
|
propagators.add({
|
|
|
|
init() {
|
|
|
|
return headAndContent;
|
|
|
|
},
|
|
|
|
});
|
2023-05-31 23:18:07 +00:00
|
|
|
|
|
|
|
return headAndContent;
|
2023-03-21 12:17:20 +00:00
|
|
|
}
|
|
|
|
return renderComponent(result, treeNode.tag, treeNode.tag, treeNode.attributes, slots);
|
|
|
|
},
|
2023-05-31 23:18:07 +00:00
|
|
|
propagation: 'self',
|
2023-03-21 12:17:20 +00:00
|
|
|
});
|
|
|
|
|
2023-06-06 17:40:04 +00:00
|
|
|
export async function createTreeNode(node: RenderableTreeNode): Promise<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) };
|
|
|
|
} else if (node === null || typeof node !== 'object' || !Markdoc.Tag.isTag(node)) {
|
|
|
|
return { type: 'text', content: '' };
|
|
|
|
}
|
|
|
|
|
2023-05-31 23:18:07 +00:00
|
|
|
const children = await Promise.all(node.children.map((child) => createTreeNode(child)));
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'component',
|
|
|
|
component,
|
|
|
|
props,
|
|
|
|
children,
|
|
|
|
};
|
2023-05-31 23:18:07 +00:00
|
|
|
} else if (isPropagatedAssetsModule(node.name)) {
|
|
|
|
const { collectedStyles, collectedLinks, collectedScripts } = node.name;
|
2023-06-06 17:40:04 +00:00
|
|
|
const component = (await node.name.getMod()).default;
|
2023-05-31 23:18:07 +00:00
|
|
|
const props = node.attributes;
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'component',
|
|
|
|
component,
|
|
|
|
collectedStyles,
|
|
|
|
collectedLinks,
|
|
|
|
collectedScripts,
|
|
|
|
props,
|
|
|
|
children,
|
|
|
|
};
|
2023-03-21 12:17:20 +00:00
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
type: 'element',
|
|
|
|
tag: node.name,
|
|
|
|
attributes: node.attributes,
|
2023-05-31 23:18:07 +00:00
|
|
|
children,
|
2023-03-21 12:17:20 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2023-05-31 23:18:07 +00:00
|
|
|
|
|
|
|
type PropagatedAssetsModule = {
|
|
|
|
__astroPropagation: true;
|
2023-06-06 17:40:04 +00:00
|
|
|
getMod: () => Promise<AstroInstance>;
|
2023-05-31 23:18:07 +00:00
|
|
|
collectedStyles: string[];
|
|
|
|
collectedLinks: string[];
|
|
|
|
collectedScripts: string[];
|
|
|
|
};
|
|
|
|
|
|
|
|
function isPropagatedAssetsModule(module: any): module is PropagatedAssetsModule {
|
|
|
|
return typeof module === 'object' && module != null && '__astroPropagation' in module;
|
|
|
|
}
|