Skip to content

Commit 38fdc2f

Browse files
authored
Merge pull request #1558 from CyC2018/search-camel
Update search.js
2 parents 880f499 + 8697481 commit 38fdc2f

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

‎website/builder/test_search.cjs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ const skiplist = { documents: [
116116
{ title: 'MySQL', path: '/notes/MySQL', sections: [{ title: '参数', id: 'param', text: '设置缓冲池大小' }] },
117117
] };
118118
assert.equal(search(skiplist, '跳表的层高怎么设置')[0].id, 'skip', '整句里的“跳表”也能匹配标题“跳跃表”');
119+
const camel = { documents: [
120+
{ title: '微服务', path: '/notes/微服务', sections: [{ title: '二、Spring Cloud', id: 'sc', text: 'Spring Cloud Alibaba 提供 Nacos、Sentinel 等组件' }] },
121+
{ title: '缓存', path: '/notes/缓存', sections: [{ title: '七、LRU', id: 'lru', text: '下面是基于 LinkedHashMap 实现的 LRU Cache' }] },
122+
] };
123+
assert.equal(search(camel, 'SpringCloudAlibaba')[0].id, 'sc', '驼峰连写按拆开的写法也能搜到');
124+
assert.equal(search(camel, 'LRUCache')[0].id, 'lru', '连续大写后接单词时在单词前拆开');
119125
const versions = { documents: [{ title: 'HTTP', path: '/notes/HTTP', sections: [
120126
{ title: '八、HTTP/2.0', id: 'h2', text: '二进制分帧、多路复用、首部压缩' },
121127
{ title: '七、HTTP/1.1 新特性', id: 'h11', text: '默认长连接' },

‎website/templates/search.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,18 @@
7979
return query;
8080
}
8181
// 词中含有另一种说法时也替换,例如“散列冲突”同时按“哈希冲突”搜索
82+
// 驼峰连写的英文(SpringCloudAlibaba、LRUCache)在笔记中常分开写,每次搜索时从原文算出拆开的写法
83+
var camelForms = {};
84+
function camelSplit(query) {
85+
camelForms = {};
86+
(String(query).normalize('NFKC').match(/[A-Za-z][A-Za-z0-9]*/g) || []).forEach(function (word) {
87+
var split = word.replace(/([a-z0-9])([A-Z])/g, '$1 $2').replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2');
88+
if (split !== word) camelForms[word.toLowerCase()] = split.toLowerCase();
89+
});
90+
}
8291
function alternatives(term) {
8392
var result = [term];
93+
if (camelForms[term]) result.push(camelForms[term]);
8494
// “单例模式”“观察者模式”:正文常只写“单例”“观察者”
8595
if (/^[㐀-鿿]{2,}模式$/.test(term) && term !== '设计模式') result.push(term.slice(0, -2));
8696
SYNONYMS.forEach(function (group) {
@@ -209,6 +219,7 @@
209219
function search(index, query, limit) {
210220
var normalizedQuery = normalize(query).slice(0, 120);
211221
if (!normalizedQuery) return [];
222+
camelSplit(query);
212223
var entries = prepareIndex(index), results = [], fuzzy = false, used = [];
213224
var seen = {};
214225
function collect(scoreOf) {

0 commit comments

Comments
 (0)