Skip to content

Commit

Permalink
fix: stop bundling markdoc for isTag
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Feb 16, 2023
1 parent cb19048 commit a0dcbff
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions packages/integrations/markdoc/components/astroNode.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
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
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 =
Expand All @@ -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 '';
}

Expand Down

0 comments on commit a0dcbff

Please sign in to comment.