Skip to content

Commit

Permalink
fix(markdown): support HTML comments in markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Nate Moore committed May 24, 2022
1 parent f42cc34 commit ee6e7f9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/astro/src/vite-plugin-markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ export default function markdown({ config }: AstroPluginOptions): Plugin {
const hasInjectedScript = isPage && config._ctx.scripts.some((s) => s.stage === 'page-ssr');

// Extract special frontmatter keys
const { data: frontmatter, content: markdownContent } = matter(source);
let { data: frontmatter, content: markdownContent } = matter(source);

// Turn HTML comments into JS comments
markdownContent = markdownContent.replace(/<\s*!--([^-->]*)(.*?)-->/gs, (whole) => `{/*${whole}*/}`)

let renderResult = await renderMarkdown(markdownContent, renderOpts);
let { code: astroResult, metadata } = renderResult;
const { layout = '', components = '', setup = '', ...content } = frontmatter;
Expand Down
8 changes: 7 additions & 1 deletion packages/astro/test/astro-markdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,16 @@ describe('Astro Markdown', () => {
it('Correctly handles component children in markdown pages (#3319)', async () => {
const html = await fixture.readFile('/children/index.html');

console.log(html);
expect(html).not.to.contain('<p></p>');
});

it('Can handle HTML comments in markdown pages', async () => {
const html = await fixture.readFile('/comment/index.html');
const $ = cheerio.load(html);

expect($('h1').text()).to.equal('It works!');
});

it('Can load more complex jsxy stuff', async () => {
const html = await fixture.readFile('/complex/index.html');
const $ = cheerio.load(html);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!-- HTML comments! -->
# It works!

0 comments on commit ee6e7f9

Please sign in to comment.