From 6244c6721eba3a5be895a290c13c68d3c3f2b6c6 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Thu, 21 Jul 2022 12:23:46 -0400 Subject: [PATCH] fix: ignore "setup" and "components" in plain MD mode --- .../astro/src/vite-plugin-markdown/index.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/astro/src/vite-plugin-markdown/index.ts b/packages/astro/src/vite-plugin-markdown/index.ts index b67a144e494f3..3d70aba72dfe8 100644 --- a/packages/astro/src/vite-plugin-markdown/index.ts +++ b/packages/astro/src/vite-plugin-markdown/index.ts @@ -137,6 +137,7 @@ export default function markdown({ config }: AstroPluginOptions): Plugin { const filename = normalizeFilename(id); const source = await fs.promises.readFile(filename, 'utf8'); const renderOpts = config.markdown; + const isMDX = renderOpts.mode === 'mdx'; const fileUrl = new URL(`file://${filename}`); const isPage = fileUrl.pathname.startsWith(resolvePages(config).pathname); @@ -148,7 +149,7 @@ export default function markdown({ config }: AstroPluginOptions): Plugin { // Turn HTML comments into JS comments while preventing nested `*/` sequences // from ending the JS comment by injecting a zero-width space // Inside code blocks, this is removed during renderMarkdown by the remark-escape plugin. - if (renderOpts.mode === 'mdx') { + if (isMDX) { markdownContent = markdownContent.replace( /<\s*!--([^-->]*)(.*?)-->/gs, (whole) => `{/*${whole.replace(/\*\//g, '*\u200b/')}*/}` @@ -167,23 +168,27 @@ export default function markdown({ config }: AstroPluginOptions): Plugin { const prelude = `--- import Slugger from 'github-slugger'; ${layout ? `import Layout from '${layout}';` : ''} -${components ? `import * from '${components}';` : ''} +${isMDX && components ? `import * from '${components}';` : ''} ${hasInjectedScript ? `import '${PAGE_SSR_SCRIPT_ID}';` : ''} -${setup} +${isMDX ? setup : ''} const slugger = new Slugger(); function $$slug(value) { return slugger.slug(value); } -const $$content = ${JSON.stringify(content)} +const $$content = ${JSON.stringify(isMDX + ? content + // Avoid stripping "setup" and "components" + // in plain MD mode + : { ...content, setup, components })} ---`; const imports = `${layout ? `import Layout from '${layout}';` : ''} -${setup}`.trim(); +${isMDX ? setup : ''}`.trim(); // Wrap with set:html fragment to skip // JSX expressions and components in "plain" md mode - if (renderOpts.mode === 'md') { + if (!isMDX) { astroResult = `` }