astro/packages/integrations/markdoc/components/TreeNode.ts
Ben Holmes 1efaef6be0
Markdoc - Shiki (#7187)
* chore: remove unused util

* chore: changeset

* deps: shiki

* wip: first stab at shiki markdoc config

* feat: get shiki working!

* refactor: return HTML string directly from transform

* chore: move shiki to markdoc dev dep

* refactor: use async cache with clear docs on why

* test: transform units with Shiki config options

* refactor: switch to `extends` model

* refactor: nodes/ -> extensions/

* feat: raise friendly error for Promise extensions

* docs: README

* chore: lint

* chore: dead file

* chore: lowercase for fuzzy find please

* fix: bad ctx spread

* chore: clean up cache, add shiki imp error

* chore: add shiki to optional peer deps

* chore: hoist those consts

* docs: more explicit "install shiki now please"

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>

* oops bad find and replace

* chore: update changeset

* nit: period haunts me

---------

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
2023-05-24 16:52:22 -04:00

90 lines
2.2 KiB
TypeScript

import type { AstroInstance } from 'astro';
import { Fragment } from 'astro/jsx-runtime';
import type { RenderableTreeNode } from '@markdoc/markdoc';
import Markdoc from '@markdoc/markdoc';
import {
createComponent,
renderComponent,
render,
HTMLString,
isHTMLString,
} from 'astro/runtime/server/index.js';
export type TreeNode =
| {
type: 'text';
content: string | HTMLString;
}
| {
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}`;
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 | RenderableTreeNode[]): TreeNode {
if (isHTMLString(node)) {
return { type: 'text', content: node as HTMLString };
} else if (typeof node === 'string' || typeof node === 'number') {
return { type: 'text', content: String(node) };
} else if (Array.isArray(node)) {
return {
type: 'component',
component: Fragment,
props: {},
children: node.map((child) => createTreeNode(child)),
};
} 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)),
};
}
}