添加搜索关键词解析函数

This commit is contained in:
2022-03-18 11:13:01 +08:00
parent 5b9fc602b9
commit 463aa3ae4b
2 changed files with 188 additions and 0 deletions

View File

@@ -394,6 +394,37 @@ export default {
}
},
/**
* @typedef MDNSearchIndexDatas
* @type {{ title: string, url: string }[]}
*/
/**
* @description 解析 MDN 搜索关键词
* @param {MDNSearchIndexDatas} datas 关键词数据
* @param {string} keyword 输入的关键词
* @param {number} [max] 最多返回的结果数量,默认为 10
* @returns {MDNSearchIndexDatas} 匹配到的关键词
*/
parseMDNSearchWords(datas = [], keyword = '', max = 10) {
/** @type {MDNSearchIndexDatas} */
const result = [];
const word = String(keyword).toLowerCase();
let count = 0;
for (const item of datas) {
if (item.title.toLowerCase().indexOf(word) > -1) {
result.push(item);
if ((count += 1) >= max) {
break;
}
}
}
return result;
},
/**
* 搜索引擎(获取关键词建议)
*
@@ -769,6 +800,7 @@ export default {
.name {
flex-shrink: 0;
margin-left: 0.5rem;
}
.desc {