From ec66970188ce5653fdb95c828e894a3ece31b417 Mon Sep 17 00:00:00 2001 From: Tomasz Kajtoch Date: Mon, 15 Jul 2024 16:43:11 +0200 Subject: [PATCH] fix(docusaurus-theme): fix `get_source_from_children` not handling optimized production builds' MDX content --- .../demo/source/get_source_from_children.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/docusaurus-theme/src/components/demo/source/get_source_from_children.ts b/packages/docusaurus-theme/src/components/demo/source/get_source_from_children.ts index 51f8dfd3528..6dff6b4da38 100644 --- a/packages/docusaurus-theme/src/components/demo/source/get_source_from_children.ts +++ b/packages/docusaurus-theme/src/components/demo/source/get_source_from_children.ts @@ -18,7 +18,13 @@ export const getSourceFromChildren = (children: ReactNode): string | null => { } const element = children as ReactElement; - if (typeof element.type !== 'function' || (element.type as Function).name !== 'MDXPre') { + const functionName = (element.type as Function).name; + // The code block content could render in either MDXPre (development builds) + // or pre (optimized production builds) + if ( + typeof element.type !== 'function' || + (functionName !== 'MDXPre' && functionName !== 'pre') + ) { return null; } @@ -32,9 +38,9 @@ export const getSourceFromChildren = (children: ReactNode): string | null => { } const code = codeElement.props.children; - if (typeof code !== 'string'){ + if (typeof code !== 'string') { return null; } return code; -} +};