refactor: clean up astroNode

This commit is contained in:
bholmesdev 2023-02-14 09:25:38 -05:00
parent 6f6df47597
commit 36025cdb66

View file

@ -1,33 +1,19 @@
import type { ComponentInstance } from 'astro';
import Markdoc from '@markdoc/markdoc';
import type { RenderableTreeNode, Tag } from '@markdoc/markdoc';
import { escape } from 'html-escaper';
// TODO: expose `AstroComponentFactory` type from core
type AstroComponentFactory = (props: Record<string, any>) => any & {
isAstroComponentFactory: true;
};
/**
* Copied from Markdoc Tag.isTag implementation
* to avoid dragging the whole 40kb Markdoc bundle into your build!
*/
function isTag(tag: any): tag is Tag {
return !!(tag?.$$mdtype === 'Tag');
}
export type ComponentRenderer =
| AstroComponentFactory
| ComponentInstance['default']
| {
component: AstroComponentFactory;
props?(params: {
attributes: Record<string, any>;
getTreeNode(): import('@markdoc/markdoc').Tag;
}): Record<string, any>;
component: ComponentInstance['default'];
props?(params: { attributes: Record<string, any>; getTreeNode(): Tag }): Record<string, any>;
};
export type AstroNode =
| string
| {
component: AstroComponentFactory;
component: ComponentInstance['default'];
props: Record<string, any>;
children: AstroNode[];
}
@ -43,7 +29,7 @@ export function createAstroNode(
): AstroNode {
if (typeof node === 'string' || typeof node === 'number') {
return escape(String(node));
} else if (node === null || typeof node !== 'object' || !isTag(node)) {
} else if (node === null || typeof node !== 'object' || !Markdoc.Tag.isTag(node)) {
return '';
}