Skip to content

Commit

Permalink
搜索逻辑新增了拼音匹配
Browse files Browse the repository at this point in the history
  • Loading branch information
nowscott committed Nov 28, 2024
1 parent 3fee70a commit 493cb73
Show file tree
Hide file tree
Showing 3 changed files with 1,161 additions and 2,321 deletions.
18 changes: 13 additions & 5 deletions lib/dataLoader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import _ from 'lodash';
import TinyPinyin from 'tiny-pinyin';

// 随机排序
export const randomSort = arr => _.shuffle(arr);
Expand Down Expand Up @@ -26,11 +27,18 @@ export const filterPostsBySearch = (posts, query) => {
return posts;
}
const lowerQuery = query.toLowerCase();
return posts.filter(post =>
post.name.toLowerCase().includes(lowerQuery) ||
post.brief.toLowerCase().includes(lowerQuery) ||
post.tags.some(tag => tag.toLowerCase().includes(lowerQuery))
);
return posts.filter(post => {
const namePinyin = TinyPinyin.convertToPinyin(post.name || '');
const tagsPinyin = post.tags.map(tag => TinyPinyin.convertToPinyin(tag || ''));

return (
post.name.toLowerCase().includes(lowerQuery) ||
post.brief.toLowerCase().includes(lowerQuery) ||
post.tags.some(tag => tag.toLowerCase().includes(lowerQuery)) ||
namePinyin.toLowerCase().includes(lowerQuery) ||
tagsPinyin.some(tagPinyin => tagPinyin.toLowerCase().includes(lowerQuery))
);
});
};

// 渲染标签
Expand Down
Loading

0 comments on commit 493cb73

Please sign in to comment.