Skip to content

Commit

Permalink
Refactor Astro Markdown rendering logic to use cached processor inste…
Browse files Browse the repository at this point in the history
…ad of creating a new one each time
  • Loading branch information
Adammatthiesen committed Dec 22, 2024
1 parent 053174b commit 82c3363
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
10 changes: 3 additions & 7 deletions packages/studiocms_renderers/src/lib/astro-remark.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
10 changes: 6 additions & 4 deletions packages/studiocms_renderers/src/lib/contentRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
if (typeof renderer === 'object') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ rendererMarkdownConfig.addSingleLineNote(

rendererMarkdownConfig.addModule('studiocms:renderer/astroMarkdownConfig', {
defaultExport: {
typeDef: `import('astro').AstroConfig['markdown']`,
typeDef: `import('astro').AstroUserConfig['markdown']`,
},
});

Expand Down

0 comments on commit 82c3363

Please sign in to comment.