Fix */ breaking HTML comments in Markdown (#3477)

This commit is contained in:
hippotastic 2022-05-30 18:18:33 +02:00 committed by GitHub
parent 95c506bf6b
commit 429b65d60b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Prevent `*/` from breaking HTML comments in Markdown

View file

@ -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, {

View file

@ -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');

View file

@ -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!