소스 검색

Update 2 files

- /assets/js/main.js
- /_posts/2025-04-04-search.md
mayx 10 달 전
부모
커밋
72ab43b411
2개의 변경된 파일24개의 추가작업 그리고 5개의 파일을 삭제
  1. 14 4
      _posts/2025-04-04-search.md
  2. 10 1
      assets/js/main.js

파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 14 - 4
_posts/2025-04-04-search.md


+ 10 - 1
assets/js/main.js

@@ -56,11 +56,20 @@ $(function () {
     const regex = new RegExp(`(${escapedKeyword})`, 'gi');
 
     // 递归遍历并高亮文本节点
+    const escapeHTML = str => str.replace(/[&<>"']/g, 
+        tag => ({
+            '&': '&amp;',
+            '<': '&lt;',
+            '>': '&gt;',
+            '"': '&quot;',
+            "'": '&#39;'
+        }[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, '<mark>$1</mark>');