summaryrefslogtreecommitdiff
path: root/packages/markdown
diff options
context:
space:
mode:
Diffstat (limited to 'packages/markdown')
-rw-r--r--packages/markdown/remark/src/rehype-escape.ts6
-rw-r--r--packages/markdown/remark/test/entities.test.js12
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, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
+}
+
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, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
+ 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(`&lt;i&gt;This should NOT be italic&lt;/i&gt;`);
+
+ expect(code).to.equal(`<p>&#x3C;i>This should NOT be italic&#x3C;/i></p>`);
+ });
+});