Skip to content

Commit

Permalink
Fix */ breaking HTML comments in Markdown (#3477)
Browse files Browse the repository at this point in the history
  • Loading branch information
hippotastic committed May 30, 2022
1 parent 95c506b commit 429b65d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-eels-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Prevent `*/` from breaking HTML comments in Markdown
5 changes: 3 additions & 2 deletions packages/astro/src/vite-plugin-markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
7 changes: 7 additions & 0 deletions packages/astro/test/astro-markdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
@@ -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!

0 comments on commit 429b65d

Please sign in to comment.