refactor: clean up astroNode
This commit is contained in:
parent
6f6df47597
commit
36025cdb66
1 changed files with 7 additions and 21 deletions
|
@ -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 '';
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue