Skip to content

Commit 19910b1

Browse files
committed
Fix search highlighting for XML/HTML snippets
- Modified encodeHtmlEntities function to preserve <mark> tags - Search matches now display correctly in XML/HTML content - Prevents content loss when highlighting XML/HTML snippets - Maintains HTML entity encoding for security
1 parent bd2d81f commit 19910b1

1 file changed

Lines changed: 24 additions & 6 deletions

File tree

public/js/app.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -695,13 +695,31 @@ function needsHtmlEncoding(language) {
695695
return languagesNeedingHtmlEncoding.includes(getPrismLanguage(language));
696696
}
697697

698-
// Function to encode HTML entities
698+
// Function to encode HTML entities while preserving <mark> tags
699699
function encodeHtmlEntities(text) {
700-
return text.replace(/&/g, '&amp;')
701-
.replace(/</g, '&lt;')
702-
.replace(/>/g, '&gt;')
703-
.replace(/"/g, '&quot;')
704-
.replace(/'/g, '&#39;');
700+
// First, temporarily replace <mark> tags with placeholders
701+
const markTagRegex = /<mark[^>]*>.*?<\/mark>/gi;
702+
const placeholders = [];
703+
let placeholderIndex = 0;
704+
705+
// Replace <mark> tags with placeholders
706+
const textWithoutMarks = text.replace(markTagRegex, (match) => {
707+
placeholders.push(match);
708+
return `__MARK_PLACEHOLDER_${placeholderIndex++}__`;
709+
});
710+
711+
// Encode HTML entities in the text without <mark> tags
712+
const encodedText = textWithoutMarks
713+
.replace(/&/g, '&amp;')
714+
.replace(/</g, '&lt;')
715+
.replace(/>/g, '&gt;')
716+
.replace(/"/g, '&quot;')
717+
.replace(/'/g, '&#39;');
718+
719+
// Restore <mark> tags
720+
return encodedText.replace(/__MARK_PLACEHOLDER_(\d+)__/g, (match, index) => {
721+
return placeholders[parseInt(index)];
722+
});
705723
}
706724
const aboutBtn = document.getElementById('about-btn');
707725
const aboutModal = document.getElementById('about-modal');

0 commit comments

Comments
 (0)