feat(search): 引入拼音搜索

* add pinyin search

* style(highlight): keep theme-aware background

- restore theme-aware .highlight with --highlight-bg

- add highlight vars for light/dark

chore: load pinyin lib before script.js

- ensure pinyin-match.js loads before script.js to avoid runtime dependency issues

docs: add third-party notice for pinyin-match

- add placeholder notice; replace with upstream license after verification

---------

Co-authored-by: neo <neo@example.com>
Co-authored-by: Maintainer (PR Edit) <maintainer@local>
This commit is contained in:
nsnans
2025-10-14 03:49:17 +08:00
committed by GitHub
parent e5fc71aee8
commit 338d0304d1
6 changed files with 29 additions and 1 deletions

1
assets/pinyin-match.js Normal file

File diff suppressed because one or more lines are too long

View File

@@ -157,6 +157,7 @@ html {
color: var(--text-color); color: var(--text-color);
} }
body { body {
font-family: var(--font-body); font-family: var(--font-body);
font-weight: var(--font-weight-body); font-weight: var(--font-weight-body);

View File

@@ -0,0 +1,7 @@
Third-party notice: pinyin-match
- File: assets/pinyin-match.js (minified vendor script)
- Source: To be confirmed; replace with upstream repo URL and exact license.
- License: To be added (likely MIT). Include the full license text once verified.
Vendored to enable offline builds. This notice will be replaced with the exact license details once confirmed.

View File

@@ -877,6 +877,12 @@ function copyStaticFiles(config) {
console.error('Error copying style.css:', e); console.error('Error copying style.css:', e);
} }
try {
fs.copyFileSync('assets/pinyin-match.js', 'dist/pinyin-match.js');
} catch (e) {
console.error('Error copying pinyin-match.js:', e);
}
// 复制JavaScript文件 // 复制JavaScript文件
try { try {
fs.copyFileSync('src/script.js', 'dist/script.js'); fs.copyFileSync('src/script.js', 'dist/script.js');

View File

@@ -665,7 +665,7 @@ document.addEventListener('DOMContentLoaded', () => {
// 使用更高效的搜索算法 // 使用更高效的搜索算法
const matchedItems = searchIndex.items.filter(item => { const matchedItems = searchIndex.items.filter(item => {
return item.searchText.includes(searchTerm); return item.searchText.includes(searchTerm) || PinyinMatch.match(item.searchText, searchTerm);;
}); });
// 按页面分组结果 // 按页面分组结果
@@ -800,6 +800,12 @@ document.addEventListener('DOMContentLoaded', () => {
title.removeChild(title.firstChild); title.removeChild(title.firstChild);
} }
title.appendChild(titleFragment); title.appendChild(titleFragment);
} else if (PinyinMatch.match(title.textContent, searchTerm)) {
const arr = PinyinMatch.match(title.textContent, searchTerm);
const [start, end] = arr;
title.innerHTML = title.textContent.slice(0, start) +
`<span class="highlight">${title.textContent.slice(start, end + 1)}</span>` +
title.textContent.slice(end + 1);
} }
// 安全地高亮描述中的匹配文本 // 安全地高亮描述中的匹配文本
@@ -846,6 +852,12 @@ document.addEventListener('DOMContentLoaded', () => {
description.removeChild(description.firstChild); description.removeChild(description.firstChild);
} }
description.appendChild(descFragment); description.appendChild(descFragment);
} else if (PinyinMatch.match(description.textContent, searchTerm)) {
const arr = PinyinMatch.match(description.textContent, searchTerm);
const [start, end] = arr;
description.innerHTML = description.textContent.slice(0, start) +
`<span class="highlight">${description.textContent.slice(start, end + 1)}</span>` +
description.textContent.slice(end + 1);
} }
} catch (error) { } catch (error) {
console.error('Error highlighting search term'); console.error('Error highlighting search term');

View File

@@ -128,6 +128,7 @@
<script id="menav-config-data" type="application/json" style="display: none;"> <script id="menav-config-data" type="application/json" style="display: none;">
{{{configJSON}}} {{{configJSON}}}
</script> </script>
<script src="pinyin-match.js"></script>
<script src="script.js"></script> <script src="script.js"></script>
</body> </body>
</html> </html>