diff options
Diffstat (limited to 'packages/markdown')
-rw-r--r-- | packages/markdown/remark/src/rehype-escape.ts | 6 | ||||
-rw-r--r-- | packages/markdown/remark/test/entities.test.js | 12 |
2 files changed, 17 insertions, 1 deletions
diff --git a/packages/markdown/remark/src/rehype-escape.ts b/packages/markdown/remark/src/rehype-escape.ts index e776c1bb1..e99e37e41 100644 --- a/packages/markdown/remark/src/rehype-escape.ts +++ b/packages/markdown/remark/src/rehype-escape.ts @@ -1,5 +1,9 @@ import { visit } from 'unist-util-visit'; +export function escapeEntities(value: string): string { + return value.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'); +} + export default function rehypeEscape(): any { return function (node: any): any { return visit(node, 'element', (el) => { @@ -8,7 +12,7 @@ export default function rehypeEscape(): any { // Visit all raw children and escape HTML tags to prevent Markdown code // like "This is a `<script>` tag" from actually opening a script tag visit(el, 'raw', (raw) => { - raw.value = raw.value.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'); + raw.value = escapeEntities(raw.value); }); } return el; diff --git a/packages/markdown/remark/test/entities.test.js b/packages/markdown/remark/test/entities.test.js new file mode 100644 index 000000000..a6b5918a5 --- /dev/null +++ b/packages/markdown/remark/test/entities.test.js @@ -0,0 +1,12 @@ +import { renderMarkdown } from '../dist/index.js'; +import { expect } from 'chai'; + +describe('entities', () => { + const renderAstroMd = (text) => renderMarkdown(text, { isAstroFlavoredMd: false }); + + it('should not unescape entities', async () => { + const { code } = await renderAstroMd(`<i>This should NOT be italic</i>`); + + expect(code).to.equal(`<p><i>This should NOT be italic</i></p>`); + }); +}); |