From 7c439868a3bc7d466418da9af669966014f3d9fe Mon Sep 17 00:00:00 2001 From: Ben Holmes Date: Mon, 27 Mar 2023 18:04:37 -0400 Subject: [Markdoc] New config format with runtime variable support! (#6653) * deps: esbuild * feat: support direct component imports for render! * deps: add devalue back * refactor: remove unused components prop * refactor: load experimental assets config separately * fix: upate Content type def to support props * refactor: replace astro stub with inline data * feat: pass through viteId to getRenderMod * fix: add back $entry var with defaults convention * chore: remove unneeded validateRenderProps * chore: remove uneeded validateComponents * fix: remove userMarkdocConfig prop * chore: add helpful error for legacy config * deps: kleur * fix: add back `isCapitalized` * fix: log instead of throw to avoid scary stacktrace * chore: delete more old logic (nice) * chore: delete MORE unused utils * chore: comment on separate assets config * chore: remove console.log * chore: general code cleanup * test: new render config * docs: new README * fix: add expect-error on astro:assets * feat: add defineMarkdocConfig helper * docs: update example README * test: add runtime variable * chore: lint * chore: changeset * chore: add component import deletion * docs: add notes on Vite fork * fix: astro check * chore: add `.mts` to markdoc config formats --- .../integrations/markdoc/components/TreeNode.ts | 29 +++++----------------- 1 file changed, 6 insertions(+), 23 deletions(-) (limited to 'packages/integrations/markdoc/components/TreeNode.ts') diff --git a/packages/integrations/markdoc/components/TreeNode.ts b/packages/integrations/markdoc/components/TreeNode.ts index f46355d5c..8a3158589 100644 --- a/packages/integrations/markdoc/components/TreeNode.ts +++ b/packages/integrations/markdoc/components/TreeNode.ts @@ -1,10 +1,7 @@ import type { AstroInstance } from 'astro'; import type { RenderableTreeNode } from '@markdoc/markdoc'; -import { createComponent, renderComponent, render } from 'astro/runtime/server/index.js'; -// @ts-expect-error Cannot find module 'astro:markdoc-assets' or its corresponding type declarations -import { Image } from 'astro:markdoc-assets'; import Markdoc from '@markdoc/markdoc'; -import { MarkdocError, isCapitalized } from '../dist/utils.js'; +import { createComponent, renderComponent, render } from 'astro/runtime/server/index.js'; export type TreeNode = | { @@ -47,26 +44,17 @@ export const ComponentNode = createComponent({ propagation: 'none', }); -const builtInComponents: Record = { - Image, -}; - -export function createTreeNode( - node: RenderableTreeNode, - userComponents: Record = {} -): TreeNode { - const components = { ...userComponents, ...builtInComponents }; - +export function createTreeNode(node: RenderableTreeNode): TreeNode { if (typeof node === 'string' || typeof node === 'number') { return { type: 'text', content: String(node) }; } else if (node === null || typeof node !== 'object' || !Markdoc.Tag.isTag(node)) { return { type: 'text', content: '' }; } - if (node.name in components) { - const component = components[node.name]; + if (typeof node.name === 'function') { + const component = node.name; const props = node.attributes; - const children = node.children.map((child) => createTreeNode(child, components)); + const children = node.children.map((child) => createTreeNode(child)); return { type: 'component', @@ -74,17 +62,12 @@ export function createTreeNode( props, children, }; - } else if (isCapitalized(node.name)) { - throw new MarkdocError({ - message: `Unable to render ${JSON.stringify(node.name)}.`, - hint: 'Did you add this to the "components" prop on your component?', - }); } else { return { type: 'element', tag: node.name, attributes: node.attributes, - children: node.children.map((child) => createTreeNode(child, components)), + children: node.children.map((child) => createTreeNode(child)), }; } } -- cgit v1.2.3