Skip to content

Commit ec08457

Browse files
committed
prioritise current section when searching
1 parent 952edbf commit ec08457

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

packages/site-kit/src/lib/search/SearchBox.svelte

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ It appears when the user clicks on the `Search` component or presses the corresp
1010
import Icon from '../components/Icon.svelte';
1111
import SearchResults from './SearchResults.svelte';
1212
import SearchWorker from './search-worker.js?worker';
13+
import { page } from '$app/stores';
1314
1415
interface Props {
1516
placeholder?: string;
@@ -94,7 +95,14 @@ It appears when the user clicks on the `Search` component or presses the corresp
9495
const id = uid++;
9596
pending.add(id);
9697
97-
worker.postMessage({ type: 'query', id, payload: $search_query });
98+
worker.postMessage({
99+
type: 'query',
100+
id,
101+
payload: {
102+
query: $search_query,
103+
path: $page.url.pathname
104+
}
105+
});
98106
}
99107
});
100108

packages/site-kit/src/lib/search/search-worker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ addEventListener('message', async (event) => {
1212
}
1313

1414
if (type === 'query') {
15-
const query = payload;
16-
const results = search(query);
15+
const { query, path } = payload;
16+
const results = search(query, path);
1717

1818
postMessage({ type: 'results', payload: { results, query } });
1919
}

packages/site-kit/src/lib/search/search.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,36 @@ export function init(blocks: Block[]) {
4747
/**
4848
* Search for a given query in the existing index
4949
*/
50-
export function search(query: string): BlockGroup[] {
50+
export function search(query: string, path: string): BlockGroup[] {
5151
const escaped = query.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
5252
const regex = new RegExp(`(^|\\b)${escaped}`, 'i');
5353

54+
const parts = path.split('/');
55+
5456
const blocks = indexes
5557
.flatMap((index) => index.search(query))
5658
// @ts-expect-error flexsearch types are wrong i think?
5759
.map(lookup)
5860
.map((block, rank) => ({ block: block as Block, rank }))
5961
.sort((a, b) => {
60-
// If rank is way lower, give that priority
61-
if (Math.abs(a.rank - b.rank) > 3) {
62-
return a.rank - b.rank;
62+
// prioritise current section
63+
const a_parts = a.block.href.split('/');
64+
const b_parts = b.block.href.split('/');
65+
66+
for (let i = 0; i < parts.length; i += 1) {
67+
const a_part_matches = a_parts[i] === parts[i];
68+
const b_part_matches = b_parts[i] === parts[i];
69+
70+
if (!a_part_matches || !b_part_matches) {
71+
if (a_part_matches !== b_part_matches) {
72+
if (i > 1) {
73+
console.log('here', a, b);
74+
}
75+
return a_part_matches ? -1 : 1;
76+
}
77+
78+
break;
79+
}
6380
}
6481

6582
const a_title_matches = regex.test(a.block.breadcrumbs.at(-1)!);

0 commit comments

Comments
 (0)