Fix double-escaping of non-highlighted code blocks in Astro-flavored markdown (#4169)
This commit is contained in:
parent
9315ce65dd
commit
16034f0dd5
3 changed files with 26 additions and 6 deletions
5
.changeset/strong-hotels-cross.md
Normal file
5
.changeset/strong-hotels-cross.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@astrojs/markdown-remark': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix double-escaping of non-highlighted code blocks in Astro-flavored markdown
|
|
@ -1,4 +1,4 @@
|
||||||
import { visit } from 'unist-util-visit';
|
import { visit, SKIP } from 'unist-util-visit';
|
||||||
|
|
||||||
export function escapeEntities(value: string): string {
|
export function escapeEntities(value: string): string {
|
||||||
return value.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
return value.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
||||||
|
@ -14,8 +14,9 @@ export default function rehypeEscape(): any {
|
||||||
visit(el, 'raw', (raw) => {
|
visit(el, 'raw', (raw) => {
|
||||||
raw.value = escapeEntities(raw.value);
|
raw.value = escapeEntities(raw.value);
|
||||||
});
|
});
|
||||||
|
// Do not visit children to prevent double escaping
|
||||||
|
return SKIP;
|
||||||
}
|
}
|
||||||
return el;
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,11 +2,25 @@ import { renderMarkdown } from '../dist/index.js';
|
||||||
import { expect } from 'chai';
|
import { expect } from 'chai';
|
||||||
|
|
||||||
describe('entities', () => {
|
describe('entities', () => {
|
||||||
const renderAstroMd = (text) => renderMarkdown(text, { isAstroFlavoredMd: false });
|
it('should not unescape entities in regular Markdown', async () => {
|
||||||
|
const { code } = await renderMarkdown(`<i>This should NOT be italic</i>`, {
|
||||||
it('should not unescape entities', async () => {
|
isAstroFlavoredMd: false,
|
||||||
const { code } = await renderAstroMd(`<i>This should NOT be italic</i>`);
|
});
|
||||||
|
|
||||||
expect(code).to.equal(`<p><i>This should NOT be italic</i></p>`);
|
expect(code).to.equal(`<p><i>This should NOT be italic</i></p>`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should not escape entities in code blocks twice in Astro-flavored markdown', async () => {
|
||||||
|
const { code } = await renderMarkdown(
|
||||||
|
`\`\`\`astro\n<h1>{x && x.name || ''}!</h1>\n\`\`\``,
|
||||||
|
{
|
||||||
|
isAstroFlavoredMd: true,
|
||||||
|
syntaxHighlight: false,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(code).to.equal(
|
||||||
|
`<pre is:raw><code class="language-astro"><h1>{x && x.name || ''}!</h1>\n</code></pre>`
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue