From 72ab43b411183b229c576f812b630edd8b285188 Mon Sep 17 00:00:00 2001 From: mayx Date: Mon, 28 Apr 2025 15:40:47 +0000 Subject: Update 2 files - /assets/js/main.js - /_posts/2025-04-04-search.md--- assets/js/main.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'assets/js/main.js') diff --git a/assets/js/main.js b/assets/js/main.js index 889c1f0..644e2b6 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -56,11 +56,20 @@ $(function () { const regex = new RegExp(`(${escapedKeyword})`, 'gi'); // 递归遍历并高亮文本节点 + const escapeHTML = str => str.replace(/[&<>"']/g, + tag => ({ + '&': '&', + '<': '<', + '>': '>', + '"': '"', + "'": ''' + }[tag] || tag)); function highlightTextNodes(element) { $(element).contents().each(function () { if (this.nodeType === Node.TEXT_NODE) { const $this = $(this); - const text = $this.text(); + const text = escapeHTML($this.text()); + // 使用正则替换并保留原始大小写 if (regex.test(text)) { const replaced = text.replace(regex, '$1'); -- cgit 1.4.1-2-gfad0