refactor: clean up astroNode
This commit is contained in:
parent
7e9b2204b8
commit
d6137d0622
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 type { RenderableTreeNode, Tag } from '@markdoc/markdoc';
|
||||||
import { escape } from 'html-escaper';
|
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 =
|
export type ComponentRenderer =
|
||||||
| AstroComponentFactory
|
| ComponentInstance['default']
|
||||||
| {
|
| {
|
||||||
component: AstroComponentFactory;
|
component: ComponentInstance['default'];
|
||||||
props?(params: {
|
props?(params: { attributes: Record<string, any>; getTreeNode(): Tag }): Record<string, any>;
|
||||||
attributes: Record<string, any>;
|
|
||||||
getTreeNode(): import('@markdoc/markdoc').Tag;
|
|
||||||
}): Record<string, any>;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export type AstroNode =
|
export type AstroNode =
|
||||||
| string
|
| string
|
||||||
| {
|
| {
|
||||||
component: AstroComponentFactory;
|
component: ComponentInstance['default'];
|
||||||
props: Record<string, any>;
|
props: Record<string, any>;
|
||||||
children: AstroNode[];
|
children: AstroNode[];
|
||||||
}
|
}
|
||||||
|
@ -43,7 +29,7 @@ export function createAstroNode(
|
||||||
): AstroNode {
|
): AstroNode {
|
||||||
if (typeof node === 'string' || typeof node === 'number') {
|
if (typeof node === 'string' || typeof node === 'number') {
|
||||||
return escape(String(node));
|
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 '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue