Skip to content

Commit

Permalink
refactor: clean up astroNode
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Feb 16, 2023
1 parent 2dfc226 commit e914da0
Showing 1 changed file with 7 additions and 21 deletions.
28 changes: 7 additions & 21 deletions packages/integrations/markdoc/components/astroNode.ts
Original file line number Diff line number Diff line change
@@ -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[];
}
Expand All @@ -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 '';
}

Expand Down

0 comments on commit e914da0

Please sign in to comment.