mirror of
https://github.com/Mabbs/mabbs.github.io
synced 2025-07-21 01:22:03 +00:00
Update 2 files
- /assets/js/main.js - /_posts/2025-04-04-search.md
This commit is contained in:
parent
0f00469871
commit
72ab43b411
@ -32,11 +32,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, '<mark>$1</mark>');
|
||||
@ -56,6 +65,7 @@ $(function() {
|
||||
});
|
||||
});
|
||||
```
|
||||
(2025.04.28更新:解决了一个潜在的解析问题)
|
||||
我测试了一下,非常符合我的需求,各种情况都能按照我的预期工作,虽然说功能非常简单,但是能正常运行,AI写的还是挺不错的。
|
||||
|
||||
# 近期的其他修改
|
||||
|
@ -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, '<mark>$1</mark>');
|
||||
|
Loading…
x
Reference in New Issue
Block a user