Skip to content

Commit

Permalink
fix: ignore "setup" and "components" in plain MD mode
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Jul 21, 2022
1 parent 4df193c commit 6244c67
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions packages/astro/src/vite-plugin-markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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/')}*/}`
Expand All @@ -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 = `<Fragment set:html={${JSON.stringify(astroResult)}} />`
}

Expand Down

0 comments on commit 6244c67

Please sign in to comment.