Skip to content

Commit

Permalink
Fixes rendering of HTML comments inside markdown code blocks (withast…
Browse files Browse the repository at this point in the history
…ro#3638)

* JS comment wrappers should be removed from HTML comments in code blocks

* chore: add changeset
  • Loading branch information
Tony Sullivan authored Jun 20, 2022
1 parent 9e86c93 commit 73e755a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/vite-plugin-markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export default function markdown({ config }: AstroPluginOptions): Plugin {

// Turn HTML comments into JS comments while preventing nested `*/` sequences
// from ending the JS comment by injecting a zero-width space
// Inside code blocks, this is removed during renderMarkdown by the remark-escape plugin.
markdownContent = markdownContent.replace(
/<\s*!--([^-->]*)(.*?)-->/gs,
(whole) => `{/*${whole.replace(/\*\//g, '*\u200b/')}*/}`
Expand Down
14 changes: 14 additions & 0 deletions test/astro-markdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ describe('Astro Markdown', () => {
expect($('h1').text()).to.equal('It still works!');
});

it.only('Can handle HTML comments in inline code', async () => {
const html = await fixture.readFile('/comment-with-js/index.html');
const $ = cheerio.load(html);

expect($('p code').text()).to.equal('<!-- HTML comments in code -->');
});

it('Can handle HTML comments in code fences', async () => {
const html = await fixture.readFile('/comment-with-js/index.html');
const $ = cheerio.load(html);

expect($('body > code').text()).to.equal('<!-- HTML comments in code fence -->')
});

// 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
6 changes: 6 additions & 0 deletions test/fixtures/astro-markdown/src/pages/comment-with-js.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ function test() {
```
-->

```
<!-- HTML comments in code fence -->
```

`<!-- HTML comments in code -->`

# It still works!

0 comments on commit 73e755a

Please sign in to comment.