From 82c336309edd9e57ed0ec4ae53c34f461acf53ed Mon Sep 17 00:00:00 2001 From: Adam Matthiesen Date: Sun, 22 Dec 2024 03:37:03 -0800 Subject: [PATCH] Refactor Astro Markdown rendering logic to use cached processor instead of creating a new one each time --- packages/studiocms_renderers/src/lib/astro-remark.ts | 10 +++------- .../studiocms_renderers/src/lib/contentRenderer.ts | 10 ++++++---- .../src/stubs/renderer-markdownConfig.ts | 2 +- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/packages/studiocms_renderers/src/lib/astro-remark.ts b/packages/studiocms_renderers/src/lib/astro-remark.ts index c29fe7d65..5cf5a735e 100644 --- a/packages/studiocms_renderers/src/lib/astro-remark.ts +++ b/packages/studiocms_renderers/src/lib/astro-remark.ts @@ -1,11 +1,7 @@ import astroMarkdownConfig from 'studiocms:renderer/astroMarkdownConfig'; -import { type AstroMarkdownOptions, createMarkdownProcessor } from '@astrojs/markdown-remark'; +import { createMarkdownProcessor } from '@astrojs/markdown-remark'; -const mergedConfig: AstroMarkdownOptions = { - ...(astroMarkdownConfig as AstroMarkdownOptions), -}; - -const processor = await createMarkdownProcessor(mergedConfig); +const cachedProcessor = await createMarkdownProcessor(astroMarkdownConfig); /** * Render Astro Markdown @@ -19,7 +15,7 @@ const processor = await createMarkdownProcessor(mergedConfig); * @returns The rendered content */ export async function renderAstroMD(content: string) { - return (await processor.render(content)).code; + return (await cachedProcessor.render(content)).code; } export default renderAstroMD; diff --git a/packages/studiocms_renderers/src/lib/contentRenderer.ts b/packages/studiocms_renderers/src/lib/contentRenderer.ts index 56f69b6b1..bad0c2961 100644 --- a/packages/studiocms_renderers/src/lib/contentRenderer.ts +++ b/packages/studiocms_renderers/src/lib/contentRenderer.ts @@ -7,12 +7,14 @@ import renderAstroMDX from './mdx'; const { renderer } = rendererConfig; /** - * Content Renderer + * Renders the given content using a specified renderer. * - * Renders content using the specified renderer from the StudioCMS configuration + * The renderer can be a custom object with a `renderer` function and a `name` property, + * or a string indicating one of the built-in renderers ('astro', 'markdoc', 'mdx'). * - * @param content - The content to render - * @returns The rendered content as a HTML string + * @param content - The content to be rendered. + * @returns A promise that resolves to the rendered content as a string. + * @throws Will throw an error if the custom renderer object is invalid. */ export async function contentRenderer(content: string): Promise { if (typeof renderer === 'object') { diff --git a/packages/studiocms_renderers/src/stubs/renderer-markdownConfig.ts b/packages/studiocms_renderers/src/stubs/renderer-markdownConfig.ts index 461013f16..5f77ed395 100644 --- a/packages/studiocms_renderers/src/stubs/renderer-markdownConfig.ts +++ b/packages/studiocms_renderers/src/stubs/renderer-markdownConfig.ts @@ -8,7 +8,7 @@ rendererMarkdownConfig.addSingleLineNote( rendererMarkdownConfig.addModule('studiocms:renderer/astroMarkdownConfig', { defaultExport: { - typeDef: `import('astro').AstroConfig['markdown']`, + typeDef: `import('astro').AstroUserConfig['markdown']`, }, });