Skip to content

Commit

Permalink
chore: search sort by location
Browse files Browse the repository at this point in the history
  • Loading branch information
ATQQ committed Jul 21, 2024
1 parent 09c82c6 commit e11d0f7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/theme/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- chore: support rewrite version text
- chore: sass warning
- Updated dependencies
- vitepress-plugin-pagefind@0.4.8
- vitepress-plugin-pagefind@0.4.9
- vitepress@1.3.1

## 0.4.7
Expand Down
3 changes: 3 additions & 0 deletions packages/theme/docs/.vitepress/blog-theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const RSS: Theme.RSSOptions = {
}

export const blogTheme = getThemeConfig({
search: {
pageResultCount: 5
},
// 图表支持
mermaid: true,
imageStyle: {
Expand Down
6 changes: 6 additions & 0 deletions packages/vitepress-plugin-pagefind/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# vitepress-plugin-pagefind

## 0.4.9

### Patch Changes

- chore: search sort by location

## 0.4.8

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/vitepress-plugin-pagefind/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vitepress-plugin-pagefind",
"version": "0.4.8",
"version": "0.4.9",
"description": "vitepress plugin, Offline full-text search based on pagefind implementation.",
"author": "sugar",
"license": "MIT",
Expand Down
15 changes: 12 additions & 3 deletions packages/vitepress-plugin-pagefind/src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@ export function formatPagefindResult(result: PagefindResult, count = 1) {
const { sub_results: subResults, anchors, weighted_locations: weightedLocations } = result

// TODO:pick策略优化

// 按照权重排序,从大到小
weightedLocations.sort((a, b) => {
// 权重相等按照 location 排序
// 权重相等按照 location 顺序排序
if (b.weight === a.weight) {
return b.location - a.location
return a.location - b.location
}
return b.weight - a.weight
})
Expand Down Expand Up @@ -106,6 +105,16 @@ export function formatPagefindResult(result: PagefindResult, count = 1) {
}
}

// 按文章中顺序,排序
subs.sort((a, b) => {
const [minA] = a.locations || []
const [minB] = b.locations || []
if (!minA || !minB) {
return 0
}
return minA - minB
})

const filterMap = new Map<string, any>()
return subs.map(sub => parseSubResult(sub, anchors, result))
.filter((v) => {
Expand Down

0 comments on commit e11d0f7

Please sign in to comment.