Update 2 files

- /assets/js/main.js
- /_posts/2025-04-04-search.md
This commit is contained in:
mayx 2025-04-28 15:40:47 +00:00
parent 0f00469871
commit 72ab43b411
2 changed files with 24 additions and 5 deletions

View File

@ -32,11 +32,20 @@ $(function() {
const regex = new RegExp(`(${escapedKeyword})`, 'gi'); const regex = new RegExp(`(${escapedKeyword})`, 'gi');
// 递归遍历并高亮文本节点 // 递归遍历并高亮文本节点
const escapeHTML = str => str.replace(/[&<>"']/g,
tag => ({
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;'
}[tag] || tag));
function highlightTextNodes(element) { function highlightTextNodes(element) {
$(element).contents().each(function () { $(element).contents().each(function () {
if (this.nodeType === Node.TEXT_NODE) { if (this.nodeType === Node.TEXT_NODE) {
const $this = $(this); const $this = $(this);
const text = $this.text(); const text = escapeHTML($this.text());
// 使用正则替换并保留原始大小写 // 使用正则替换并保留原始大小写
if (regex.test(text)) { if (regex.test(text)) {
const replaced = text.replace(regex, '<mark>$1</mark>'); const replaced = text.replace(regex, '<mark>$1</mark>');
@ -56,6 +65,7 @@ $(function() {
}); });
}); });
``` ```
2025.04.28更新:解决了一个潜在的解析问题)
我测试了一下非常符合我的需求各种情况都能按照我的预期工作虽然说功能非常简单但是能正常运行AI写的还是挺不错的。 我测试了一下非常符合我的需求各种情况都能按照我的预期工作虽然说功能非常简单但是能正常运行AI写的还是挺不错的。
# 近期的其他修改 # 近期的其他修改

View File

@ -56,11 +56,20 @@ $(function () {
const regex = new RegExp(`(${escapedKeyword})`, 'gi'); const regex = new RegExp(`(${escapedKeyword})`, 'gi');
// 递归遍历并高亮文本节点 // 递归遍历并高亮文本节点
const escapeHTML = str => str.replace(/[&<>"']/g,
tag => ({
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;'
}[tag] || tag));
function highlightTextNodes(element) { function highlightTextNodes(element) {
$(element).contents().each(function () { $(element).contents().each(function () {
if (this.nodeType === Node.TEXT_NODE) { if (this.nodeType === Node.TEXT_NODE) {
const $this = $(this); const $this = $(this);
const text = $this.text(); const text = escapeHTML($this.text());
// 使用正则替换并保留原始大小写 // 使用正则替换并保留原始大小写
if (regex.test(text)) { if (regex.test(text)) {
const replaced = text.replace(regex, '<mark>$1</mark>'); const replaced = text.replace(regex, '<mark>$1</mark>');