From 429b65d60bb64973f1e5867cc58f1fe07bada952 Mon Sep 17 00:00:00 2001 From: hippotastic <6137925+hippotastic@users.noreply.github.com> Date: Mon, 30 May 2022 18:18:33 +0200 Subject: [PATCH] Fix `*/` breaking HTML comments in Markdown (#3477) --- .changeset/polite-eels-visit.md | 5 +++++ .../astro/src/vite-plugin-markdown/index.ts | 5 +++-- packages/astro/test/astro-markdown.test.js | 7 +++++++ .../astro-markdown/src/pages/comment-with-js.md | 17 +++++++++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 .changeset/polite-eels-visit.md create mode 100644 packages/astro/test/fixtures/astro-markdown/src/pages/comment-with-js.md diff --git a/.changeset/polite-eels-visit.md b/.changeset/polite-eels-visit.md new file mode 100644 index 000000000..00e573ac2 --- /dev/null +++ b/.changeset/polite-eels-visit.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Prevent `*/` from breaking HTML comments in Markdown diff --git a/packages/astro/src/vite-plugin-markdown/index.ts b/packages/astro/src/vite-plugin-markdown/index.ts index 997648a6b..38b36d27a 100644 --- a/packages/astro/src/vite-plugin-markdown/index.ts +++ b/packages/astro/src/vite-plugin-markdown/index.ts @@ -129,10 +129,11 @@ export default function markdown({ config }: AstroPluginOptions): Plugin { // Extract special frontmatter keys let { data: frontmatter, content: markdownContent } = matter(source); - // Turn HTML comments into JS comments + // Turn HTML comments into JS comments while preventing nested `*/` sequences + // from ending the JS comment by injecting a zero-width space markdownContent = markdownContent.replace( /<\s*!--([^-->]*)(.*?)-->/gs, - (whole) => `{/*${whole}*/}` + (whole) => `{/*${whole.replace(/\*\//g, '*\u200b/')}*/}` ); let renderResult = await renderMarkdown(markdownContent, { diff --git a/packages/astro/test/astro-markdown.test.js b/packages/astro/test/astro-markdown.test.js index ecd6074ec..181ca56e9 100644 --- a/packages/astro/test/astro-markdown.test.js +++ b/packages/astro/test/astro-markdown.test.js @@ -72,6 +72,13 @@ describe('Astro Markdown', () => { expect($('h1').text()).to.equal('It works!'); }); + it('Prevents `*/` sequences from breaking HTML comments (#3476)', async () => { + const html = await fixture.readFile('/comment-with-js/index.html'); + const $ = cheerio.load(html); + + expect($('h1').text()).to.equal('It still works!'); + }); + // 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/packages/astro/test/fixtures/astro-markdown/src/pages/comment-with-js.md b/packages/astro/test/fixtures/astro-markdown/src/pages/comment-with-js.md new file mode 100644 index 000000000..387b8380e --- /dev/null +++ b/packages/astro/test/fixtures/astro-markdown/src/pages/comment-with-js.md @@ -0,0 +1,17 @@ + + + + +# It still works!