From d0d5e2f984fbfcf8a32a2305d52857f6d7b40a3b Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Fri, 10 Feb 2023 09:01:04 -0500 Subject: [PATCH] fix: stop bundling markdoc for isTag --- .../markdoc/components/astroNode.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/integrations/markdoc/components/astroNode.ts b/packages/integrations/markdoc/components/astroNode.ts index 75c48e47e..3d359a9c1 100644 --- a/packages/integrations/markdoc/components/astroNode.ts +++ b/packages/integrations/markdoc/components/astroNode.ts @@ -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) => 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; getTreeNode(): Tag }): Record; + props?(params: { + attributes: Record; + getTreeNode(): import('@markdoc/markdoc').Tag; + }): Record; }; 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 ''; }