summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.changeset/polite-eels-visit.md5
-rw-r--r--packages/astro/src/vite-plugin-markdown/index.ts5
-rw-r--r--packages/astro/test/astro-markdown.test.js7
-rw-r--r--packages/astro/test/fixtures/astro-markdown/src/pages/comment-with-js.md17
4 files changed, 32 insertions, 2 deletions
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 @@
+<!--
+HTML comments with */ inside!
+-->
+
+<!--
+```js
+/**
+ * It even works inside nested fenced code blocks!
+ */
+function test() {
+ /* Yay */
+ return 'Nice!';
+}
+```
+-->
+
+# It still works!