Skip to content

Commit f5c018d

Browse files
committed
add doc search
1 parent 43a7d2b commit f5c018d

File tree

1 file changed

+7
-65
lines changed

1 file changed

+7
-65
lines changed

docsearch.html

Lines changed: 7 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -55,52 +55,19 @@ <h1>Search Documentation</h1>
5555
const browserLang = navigator.language || navigator.userLanguage;
5656
console.log('Browser language:', browserLang);
5757

58-
// 检查 URL 路径
59-
const currentPath = window.location.pathname;
60-
if (currentPath.includes('/ja/')) {
61-
return 'ja';
62-
}
63-
if (currentPath.includes('/zh/')) {
64-
return 'zh';
65-
}
66-
6758
// 根据浏览器语言判断
6859
if (browserLang.startsWith('ja')) {
69-
return 'ja';
60+
return ['lang:en', 'lang:ja'];
7061
}
7162
if (browserLang.startsWith('zh')) {
72-
return 'zh';
63+
return ['lang:zh-Hans', 'lang:en'];
7364
}
7465

75-
return 'en';
76-
}
77-
78-
// 过滤搜索结果
79-
function filterSearchResults(items) {
80-
const userLang = getUserLanguage();
81-
console.log('User language detected:', userLang);
82-
83-
return items.filter(item => {
84-
const url = item.url || '';
85-
86-
// 总是包含英文结果(不包含 /ja/ 或 /zh/ 路径的结果)
87-
const isEnglish = !url.includes('/ja/') && !url.includes('/zh/');
88-
89-
// 根据用户语言包含相应的本地化结果
90-
const isJapanese = url.includes('/ja/');
91-
const isChinese = url.includes('/zh/');
92-
93-
switch (userLang) {
94-
case 'ja':
95-
return isEnglish || isJapanese;
96-
case 'zh':
97-
return isEnglish || isChinese;
98-
default:
99-
return isEnglish; // 其他语言只显示英文结果
100-
}
101-
});
66+
return 'lang:en';
10267
}
103-
68+
69+
const langFilter = getUserLanguage();
70+
10471
// 初始化 DocSearch
10572
document.addEventListener('DOMContentLoaded', function() {
10673
const loadingStatus = document.getElementById('loading-status');
@@ -118,37 +85,12 @@ <h1>Search Documentation</h1>
11885
placeholder: 'Search documentation...',
11986
debug: true, // 开启调试模式,方便查看问题
12087
searchParameters: {
121-
facetFilters: [],
88+
facetFilters: [langFilter],
12289
hitsPerPage: 20, // 每页显示20个结果
12390
attributesToHighlight: ['hierarchy.lvl0', 'hierarchy.lvl1', 'hierarchy.lvl2', 'hierarchy.lvl3'],
124-
attributesToSnippet: ['content:40'] // 进一步增加内容片段长度
12591
},
12692
maxResultsPerGroup: 20, // 每组最多显示20个结果
12793
resultsFooterComponent: null, // 移除底部组件避免遮挡
128-
transformItems(items) {
129-
console.log('Raw DocSearch results:', items.length, 'items');
130-
131-
// 先进行语言过滤
132-
const filteredItems = filterSearchResults(items);
133-
console.log('Filtered DocSearch results:', filteredItems.length, 'items');
134-
135-
// 然后处理 URL
136-
const processedItems = filteredItems.map((item) => {
137-
return {
138-
...item,
139-
url: item.url.replace(/^https?:\/\/[^\/]+/, ''),
140-
};
141-
});
142-
143-
// 调试输出
144-
console.log('Processed results:', processedItems.map(item => ({
145-
title: item.hierarchy?.lvl0 || item.hierarchy?.lvl1 || 'No title',
146-
url: item.url,
147-
content: item.content?.substring(0, 100) + '...'
148-
})));
149-
150-
return processedItems;
151-
}
15294
});
15395

15496
// 隐藏加载状态

0 commit comments

Comments
 (0)