fix: stop bundling markdoc for isTag

This commit is contained in:
bholmesdev 2023-02-10 09:01:04 -05:00
parent 10a9df65ed
commit 39676acc50

View file

@ -1,4 +1,4 @@
import { RenderableTreeNode, Tag, renderers, NodeType } from '@markdoc/markdoc';
import type { RenderableTreeNode, Tag } from '@markdoc/markdoc';
import { escape } from 'html-escaper';
// TODO: expose `AstroComponentFactory` type from core
@ -6,11 +6,22 @@ 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
| {
component: AstroComponentFactory;
props?(params: { attributes: Record<string, any>; getTreeNode(): Tag }): Record<string, any>;
props?(params: {
attributes: Record<string, any>;
getTreeNode(): import('@markdoc/markdoc').Tag;
}): Record<string, any>;
};
export type AstroNode =
@ -32,7 +43,7 @@ export function createAstroNode(
): AstroNode {
if (typeof node === 'string' || typeof node === 'number') {
return escape(String(node));
} else if (node === null || typeof node !== 'object' || !Tag.isTag(node)) {
} else if (node === null || typeof node !== 'object' || !isTag(node)) {
return '';
}