Fix */
breaking HTML comments in Markdown (#3477)
This commit is contained in:
parent
95c506bf6b
commit
429b65d60b
4 changed files with 32 additions and 2 deletions
5
.changeset/polite-eels-visit.md
Normal file
5
.changeset/polite-eels-visit.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Prevent `*/` from breaking HTML comments in Markdown
|
|
@ -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, {
|
||||
|
|
|
@ -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');
|
||||
|
|
17
packages/astro/test/fixtures/astro-markdown/src/pages/comment-with-js.md
vendored
Normal file
17
packages/astro/test/fixtures/astro-markdown/src/pages/comment-with-js.md
vendored
Normal 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!
|
Loading…
Reference in a new issue