Merge pull request #1558 from CyC2018/search-camel

Update search.js
This commit is contained in:
CyC2018
2026-11-07 05:35:35 +08:00
committed by GitHub
2 changed files with 17 additions and 0 deletions
+6
View File
@@ -116,6 +116,12 @@ const skiplist = { documents: [
{ title: 'MySQL', path: '/notes/MySQL', sections: [{ title: '参数', id: 'param', text: '设置缓冲池大小' }] },
] };
assert.equal(search(skiplist, '跳表的层高怎么设置')[0].id, 'skip', '整句里的“跳表”也能匹配标题“跳跃表”');
const camel = { documents: [
{ title: '微服务', path: '/notes/微服务', sections: [{ title: '二、Spring Cloud', id: 'sc', text: 'Spring Cloud Alibaba 提供 Nacos、Sentinel 等组件' }] },
{ title: '缓存', path: '/notes/缓存', sections: [{ title: '七、LRU', id: 'lru', text: '下面是基于 LinkedHashMap 实现的 LRU Cache' }] },
] };
assert.equal(search(camel, 'SpringCloudAlibaba')[0].id, 'sc', '驼峰连写按拆开的写法也能搜到');
assert.equal(search(camel, 'LRUCache')[0].id, 'lru', '连续大写后接单词时在单词前拆开');
const versions = { documents: [{ title: 'HTTP', path: '/notes/HTTP', sections: [
{ title: '八、HTTP/2.0', id: 'h2', text: '二进制分帧、多路复用、首部压缩' },
{ title: '七、HTTP/1.1 新特性', id: 'h11', text: '默认长连接' },
+11
View File
@@ -79,8 +79,18 @@
return query;
}
// 词中含有另一种说法时也替换,例如“散列冲突”同时按“哈希冲突”搜索
// 驼峰连写的英文(SpringCloudAlibaba、LRUCache)在笔记中常分开写,每次搜索时从原文算出拆开的写法
var camelForms = {};
function camelSplit(query) {
camelForms = {};
(String(query).normalize('NFKC').match(/[A-Za-z][A-Za-z0-9]*/g) || []).forEach(function (word) {
var split = word.replace(/([a-z0-9])([A-Z])/g, '$1 $2').replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2');
if (split !== word) camelForms[word.toLowerCase()] = split.toLowerCase();
});
}
function alternatives(term) {
var result = [term];
if (camelForms[term]) result.push(camelForms[term]);
// “单例模式”“观察者模式”:正文常只写“单例”“观察者”
if (/^[㐀-鿿]{2,}模式$/.test(term) && term !== '设计模式') result.push(term.slice(0, -2));
SYNONYMS.forEach(function (group) {
@@ -209,6 +219,7 @@
function search(index, query, limit) {
var normalizedQuery = normalize(query).slice(0, 120);
if (!normalizedQuery) return [];
camelSplit(query);
var entries = prepareIndex(index), results = [], fuzzy = false, used = [];
var seen = {};
function collect(scoreOf) {