From 68171ad2de93ee04011b2b88df57fad19f36f913 Mon Sep 17 00:00:00 2001 From: Adam Matthiesen Date: Sun, 22 Dec 2024 03:21:08 -0800 Subject: [PATCH] Refactor Astro Markdown rendering logic --- .../src/lib/contentRenderer.ts | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/studiocms_renderers/src/lib/contentRenderer.ts b/packages/studiocms_renderers/src/lib/contentRenderer.ts index fb68a5d93..6047e2734 100644 --- a/packages/studiocms_renderers/src/lib/contentRenderer.ts +++ b/packages/studiocms_renderers/src/lib/contentRenderer.ts @@ -1,4 +1,4 @@ -// import { logger } from '@it-astro:logger:studiocms-renderer'; +import { logger } from '@it-astro:logger:studiocms-renderer'; import rendererConfig from 'studiocms:renderer/config'; import renderAstroMD from './astro-remark'; import renderMarkDoc from './markdoc'; @@ -29,21 +29,27 @@ const { renderer } = rendererConfig; export async function contentRenderer(content: string): Promise { switch (renderer) { case 'astro': - // logger.debug('Using built-in renderer: astro remark'); + logger.debug('Using built-in renderer: astro remark'); return await renderAstroMD(content); case 'markdoc': - // logger.debug('Using built-in renderer: markdoc'); + logger.debug('Using built-in renderer: markdoc'); return await renderMarkDoc(content); case 'mdx': - // logger.debug('Using built-in renderer: mdx'); + logger.debug('Using built-in renderer: mdx'); return await renderAstroMDX(content); default: - if (renderer.renderer) { - // logger.debug(`Using custom renderer: ${renderer.name}`); + try { + logger.debug(`Using custom renderer: ${renderer.name}`); return await renderer.renderer(content); + } catch (e) { + if (e instanceof Error) { + logger.error( + `Error rendering with ${renderer.name}: ${e.message}, falling back to astro remark\n\n${e.stack}` + ); + } + logger.error(`Error rendering with ${renderer.name}: ${e}, falling back to astro remark`); + return await renderAstroMD(content); } - // logger.debug('Using built-in renderer: astro remark'); - return await renderAstroMD(content); } }