From 73e755aba040e03630324afa8241284e2c33eb4d Mon Sep 17 00:00:00 2001 From: Tony Sullivan Date: Mon, 20 Jun 2022 17:12:42 +0000 Subject: [PATCH] Fixes rendering of HTML comments inside markdown code blocks (#3638) * JS comment wrappers should be removed from HTML comments in code blocks * chore: add changeset --- src/vite-plugin-markdown/index.ts | 1 + test/astro-markdown.test.js | 14 ++++++++++++++ .../astro-markdown/src/pages/comment-with-js.md | 6 ++++++ 3 files changed, 21 insertions(+) diff --git a/src/vite-plugin-markdown/index.ts b/src/vite-plugin-markdown/index.ts index ba749e24987e..2bcc0b241a58 100644 --- a/src/vite-plugin-markdown/index.ts +++ b/src/vite-plugin-markdown/index.ts @@ -141,6 +141,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. markdownContent = markdownContent.replace( /<\s*!--([^-->]*)(.*?)-->/gs, (whole) => `{/*${whole.replace(/\*\//g, '*\u200b/')}*/}` diff --git a/test/astro-markdown.test.js b/test/astro-markdown.test.js index dd40bc0717e1..0afbfdf36c65 100644 --- a/test/astro-markdown.test.js +++ b/test/astro-markdown.test.js @@ -79,6 +79,20 @@ describe('Astro Markdown', () => { expect($('h1').text()).to.equal('It still works!'); }); + it.only('Can handle HTML comments in inline code', async () => { + const html = await fixture.readFile('/comment-with-js/index.html'); + const $ = cheerio.load(html); + + expect($('p code').text()).to.equal(''); + }); + + it('Can handle HTML comments in code fences', async () => { + const html = await fixture.readFile('/comment-with-js/index.html'); + const $ = cheerio.load(html); + + expect($('body > code').text()).to.equal('') + }); + // https://github.com/withastro/astro/issues/3254 it('Can handle scripts in markdown pages', async () => { const html = await fixture.readFile('/script/index.html'); diff --git a/test/fixtures/astro-markdown/src/pages/comment-with-js.md b/test/fixtures/astro-markdown/src/pages/comment-with-js.md index 387b8380e20e..374463d2db11 100644 --- a/test/fixtures/astro-markdown/src/pages/comment-with-js.md +++ b/test/fixtures/astro-markdown/src/pages/comment-with-js.md @@ -14,4 +14,10 @@ function test() { ``` --> +``` + +``` + +`` + # It still works!