Skip to content

Commit 19f3c11

Browse files
escape text content before interpreting as HTML
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent a774719 commit 19f3c11

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/components/mermaid.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ import {useEffect} from 'react';
44
export default function Mermaid() {
55
useEffect(() => {
66
(async function () {
7+
const escapeHTML = (str) => {
8+
return str.replace(/[&<>"']/g, function (match) {
9+
const escapeMap = {
10+
'&': '&amp;',
11+
'<': '&lt;',
12+
'>': '&gt;',
13+
'"': '&quot;',
14+
"'": '&#39;'
15+
};
16+
return escapeMap[match];
17+
});
18+
};
719
const mermaidBlocks =
820
document.querySelectorAll<HTMLDivElement>('.language-mermaid');
921
if (mermaidBlocks.length === 0) {
@@ -14,7 +26,7 @@ export default function Mermaid() {
1426
mermaidBlocks.forEach(block => {
1527
// get rid of code highlighting
1628
const code = block.textContent ?? '';
17-
block.innerHTML = code;
29+
block.innerHTML = escapeHTML(code);
1830
// force transparent background
1931
block.style.backgroundColor = 'transparent';
2032
const parentCodeTabs = block.closest('.code-tabs-wrapper');

0 commit comments

Comments
 (0)