Encode ampersands in markdown code blocks (#3630)
* encode ampersands in markdown code blocks * chore: add changeset * nit: fixing test case description
This commit is contained in:
parent
509b4f122f
commit
48e67fe053
3 changed files with 34 additions and 2 deletions
5
.changeset/pink-pugs-beg.md
Normal file
5
.changeset/pink-pugs-beg.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@astrojs/markdown-remark': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Encodes ampersand characters in code blocks
|
|
@ -8,7 +8,7 @@ export default function rehypeEscape(): any {
|
||||||
// Visit all raw children and escape HTML tags to prevent Markdown code
|
// Visit all raw children and escape HTML tags to prevent Markdown code
|
||||||
// like "This is a `<script>` tag" from actually opening a script tag
|
// like "This is a `<script>` tag" from actually opening a script tag
|
||||||
visit(el, 'raw', (raw) => {
|
visit(el, 'raw', (raw) => {
|
||||||
raw.value = raw.value.replace(/</g, '<').replace(/>/g, '>');
|
raw.value = raw.value.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return el;
|
return el;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { renderMarkdown } from '../dist/index.js';
|
import { renderMarkdown } from '../dist/index.js';
|
||||||
import chai from 'chai';
|
import chai, { expect } from 'chai';
|
||||||
|
|
||||||
describe('expressions', () => {
|
describe('expressions', () => {
|
||||||
it('should be able to serialize bare expression', async () => {
|
it('should be able to serialize bare expression', async () => {
|
||||||
|
@ -71,6 +71,33 @@ describe('expressions', () => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should be able to encode ampersand characters in code blocks', async () => {
|
||||||
|
const { code } = await renderMarkdown(
|
||||||
|
'The ampersand in ` ` must be encoded in code blocks.',
|
||||||
|
{}
|
||||||
|
);
|
||||||
|
|
||||||
|
chai
|
||||||
|
.expect(code)
|
||||||
|
.to.equal(
|
||||||
|
'<p>The ampersand in <code is:raw>&nbsp;</code> must be encoded in code blocks.</p>'
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be able to encode ampersand characters in fenced code blocks', async () => {
|
||||||
|
const { code } = await renderMarkdown(`
|
||||||
|
\`\`\`md
|
||||||
|
The ampersand in \` \` must be encoded in code blocks.
|
||||||
|
\`\`\`
|
||||||
|
`);
|
||||||
|
|
||||||
|
chai
|
||||||
|
.expect(code)
|
||||||
|
.to.match(
|
||||||
|
/^<pre is:raw.*<code>.*The ampersand in `&nbsp;`/
|
||||||
|
);
|
||||||
|
})
|
||||||
|
|
||||||
it('should be able to serialize function expression', async () => {
|
it('should be able to serialize function expression', async () => {
|
||||||
const { code } = await renderMarkdown(
|
const { code } = await renderMarkdown(
|
||||||
`{frontmatter.list.map(item => <p id={item}>{item}</p>)}`,
|
`{frontmatter.list.map(item => <p id={item}>{item}</p>)}`,
|
||||||
|
|
Loading…
Add table
Reference in a new issue