fix: move validateComponents to render comp
This commit is contained in:
parent
aff11bbdfd
commit
7eddf996e3
3 changed files with 40 additions and 37 deletions
|
@ -1,6 +1,7 @@
|
||||||
---
|
---
|
||||||
import type { RenderableTreeNode } from '@markdoc/markdoc';
|
import type { RenderableTreeNode } from '@markdoc/markdoc';
|
||||||
import type { AstroInstance } from 'astro';
|
import type { AstroInstance } from 'astro';
|
||||||
|
import { validateComponentsProp } from '../dist/utils.js';
|
||||||
import { createAstroNode } from './astroNode';
|
import { createAstroNode } from './astroNode';
|
||||||
import RenderNode from './RenderNode.astro';
|
import RenderNode from './RenderNode.astro';
|
||||||
|
|
||||||
|
@ -10,6 +11,9 @@ type Props = {
|
||||||
};
|
};
|
||||||
|
|
||||||
const { content, components } = Astro.props as Props;
|
const { content, components } = Astro.props as Props;
|
||||||
|
|
||||||
|
// Will throw if components is invalid
|
||||||
|
validateComponentsProp(components);
|
||||||
---
|
---
|
||||||
|
|
||||||
<RenderNode node={createAstroNode(content, components)} />
|
<RenderNode node={createAstroNode(content, components)} />
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
import type { AstroInstance } from 'astro';
|
import type { AstroInstance } from 'astro';
|
||||||
import type { RenderableTreeNode } from '@markdoc/markdoc';
|
import type { RenderableTreeNode } from '@markdoc/markdoc';
|
||||||
import Markdoc from '@markdoc/markdoc';
|
import Markdoc from '@markdoc/markdoc';
|
||||||
import { MarkdocError } from '../dist/utils.js';
|
import { MarkdocError, isCapitalized } from '../dist/utils.js';
|
||||||
import z from 'astro/zod';
|
|
||||||
|
|
||||||
export type AstroNode =
|
export type AstroNode =
|
||||||
| string
|
| string
|
||||||
|
@ -21,15 +20,13 @@ export function createAstroNode(
|
||||||
node: RenderableTreeNode,
|
node: RenderableTreeNode,
|
||||||
components: Record<string, AstroInstance['default']> = {}
|
components: Record<string, AstroInstance['default']> = {}
|
||||||
): AstroNode {
|
): AstroNode {
|
||||||
components = validateComponentsProp(components);
|
|
||||||
|
|
||||||
if (typeof node === 'string' || typeof node === 'number') {
|
if (typeof node === 'string' || typeof node === 'number') {
|
||||||
return String(node);
|
return String(node);
|
||||||
} else if (node === null || typeof node !== 'object' || !Markdoc.Tag.isTag(node)) {
|
} else if (node === null || typeof node !== 'object' || !Markdoc.Tag.isTag(node)) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isCapitalized(node.name) && node.name in components) {
|
if (node.name in components) {
|
||||||
const component = components[node.name];
|
const component = components[node.name];
|
||||||
const props = node.attributes;
|
const props = node.attributes;
|
||||||
const children = node.children.map((child) => createAstroNode(child, components));
|
const children = node.children.map((child) => createAstroNode(child, components));
|
||||||
|
@ -52,35 +49,3 @@ export function createAstroNode(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const componentsPropValidator = z.record(
|
|
||||||
z
|
|
||||||
.string()
|
|
||||||
.min(1, 'Invalid `components` prop. Component names cannot be empty!')
|
|
||||||
.refine(
|
|
||||||
(value) => isCapitalized(value),
|
|
||||||
(value) => ({
|
|
||||||
message: `Invalid \`components\` prop: ${JSON.stringify(
|
|
||||||
value
|
|
||||||
)}. Component name must be capitalized. If you want to render HTML elements as components, try using a Markdoc node [TODO: DOCS LINK]`,
|
|
||||||
})
|
|
||||||
),
|
|
||||||
z.any()
|
|
||||||
);
|
|
||||||
|
|
||||||
function validateComponentsProp(components: Record<string, AstroInstance['default']>) {
|
|
||||||
try {
|
|
||||||
return componentsPropValidator.parse(components);
|
|
||||||
} catch (e) {
|
|
||||||
throw new MarkdocError({
|
|
||||||
message:
|
|
||||||
e instanceof z.ZodError
|
|
||||||
? e.issues[0].message
|
|
||||||
: 'Invalid `components` prop. Ensure you are passing an object of components to <Content />',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function isCapitalized(str: string) {
|
|
||||||
return str.length > 0 && str[0] === str[0].toUpperCase();
|
|
||||||
}
|
|
||||||
|
|
|
@ -2,6 +2,8 @@ import matter from 'gray-matter';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import type fsMod from 'node:fs';
|
import type fsMod from 'node:fs';
|
||||||
import type { ErrorPayload as ViteErrorPayload } from 'vite';
|
import type { ErrorPayload as ViteErrorPayload } from 'vite';
|
||||||
|
import type { AstroInstance } from 'astro';
|
||||||
|
import z from 'astro/zod';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Match YAML exception handling from Astro core errors
|
* Match YAML exception handling from Astro core errors
|
||||||
|
@ -109,3 +111,35 @@ export function getAstroConfigPath(fs: typeof fsMod, root: string): string | und
|
||||||
export function prependForwardSlash(str: string) {
|
export function prependForwardSlash(str: string) {
|
||||||
return str[0] === '/' ? str : '/' + str;
|
return str[0] === '/' ? str : '/' + str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function validateComponentsProp(components: Record<string, AstroInstance['default']>) {
|
||||||
|
try {
|
||||||
|
return componentsPropValidator.parse(components);
|
||||||
|
} catch (e) {
|
||||||
|
throw new MarkdocError({
|
||||||
|
message:
|
||||||
|
e instanceof z.ZodError
|
||||||
|
? e.issues[0].message
|
||||||
|
: 'Invalid `components` prop. Ensure you are passing an object of components to <Content />',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const componentsPropValidator = z.record(
|
||||||
|
z
|
||||||
|
.string()
|
||||||
|
.min(1, 'Invalid `components` prop. Component names cannot be empty!')
|
||||||
|
.refine(
|
||||||
|
(value) => isCapitalized(value),
|
||||||
|
(value) => ({
|
||||||
|
message: `Invalid \`components\` prop: ${JSON.stringify(
|
||||||
|
value
|
||||||
|
)}. Component name must be capitalized. If you want to render HTML elements as components, try using a Markdoc node [TODO: DOCS LINK]`,
|
||||||
|
})
|
||||||
|
),
|
||||||
|
z.any()
|
||||||
|
);
|
||||||
|
|
||||||
|
export function isCapitalized(str: string) {
|
||||||
|
return str.length > 0 && str[0] === str[0].toUpperCase();
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue