Skip to content

Commit 545fc88

Browse files
committed
boost on exact by default
1 parent 009a244 commit 545fc88

File tree

3 files changed

+33
-38
lines changed

3 files changed

+33
-38
lines changed

example/typedoc.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"search": {
77
"numResults": 12,
88
"boosts": {
9-
"exactMatch": 2,
109
"byKind": {
1110
"class": 1.2
1211
},

src/lib/output/themes/default/assets/typedoc/components/Search.ts

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -168,50 +168,47 @@ function updateResults(
168168
// when the `searchText` is empty.
169169
let res = searchText ? state.index.search(`*${searchText}*`) : [];
170170

171-
if (searchConfig.boosts != undefined) {
172-
for (let i = 0; i < res.length; i++) {
173-
const item = res[i];
174-
const row = state.data.rows[Number(item.ref)];
175-
let boost = 1;
176-
177-
// boost by exact match on name
178-
if (
179-
searchConfig.boosts.exactMatch &&
180-
row.name.toLowerCase() === searchText.toLowerCase()
181-
) {
182-
boost *= searchConfig.boosts.exactMatch;
183-
}
171+
for (let i = 0; i < res.length; i++) {
172+
const item = res[i];
173+
const row = state.data.rows[Number(item.ref)];
174+
let boost = 1;
175+
176+
// boost by exact match on name
177+
if(row.name
178+
.toLowerCase()
179+
.startsWith(searchText.toLowerCase())) {
180+
boost *= (1 / Math.abs(row.name.length - searchText.length) * 10)
181+
}
184182

185-
// boost by kind
186-
for (let kindName in searchConfig.boosts.byKind ?? {}) {
187-
const kind: ReflectionKind = parseInt(
188-
Object.keys(ReflectionKind).find(
189-
(key: string) =>
190-
ReflectionKind[key as keyof typeof ReflectionKind]
191-
.toString()
192-
.toLowerCase() === kindName.toLowerCase()
193-
) ?? "",
194-
10
195-
);
196-
if (row.kind == kind) {
197-
boost *= searchConfig?.boosts?.byKind?.[kindName] ?? 1;
198-
}
183+
// boost by kind
184+
for (let kindName in searchConfig.boosts?.byKind ?? {}) {
185+
const kind: ReflectionKind = parseInt(
186+
Object.keys(ReflectionKind).find(
187+
(key: string) =>
188+
ReflectionKind[key as keyof typeof ReflectionKind]
189+
.toString()
190+
.toLowerCase() === kindName.toLowerCase()
191+
) ?? "",
192+
10
193+
);
194+
if (row.kind == kind) {
195+
boost *= searchConfig?.boosts?.byKind?.[kindName] ?? 1;
199196
}
197+
}
200198

201-
// boost by category
202-
for (let categoryTitle in searchConfig.boosts?.byCategory ?? []) {
203-
if (row.categories.indexOf(categoryTitle) > -1) {
204-
boost *=
205-
searchConfig.boosts.byCategory?.[categoryTitle] ?? 1;
206-
}
199+
// boost by category
200+
for (let categoryTitle in searchConfig.boosts?.byCategory ?? []) {
201+
if (row.categories.indexOf(categoryTitle) > -1) {
202+
boost *=
203+
searchConfig.boosts?.byCategory?.[categoryTitle] ?? 1;
207204
}
208-
209-
item.score *= boost;
210205
}
211206

212-
res.sort((a, b) => b.score - a.score);
207+
item.score *= boost;
213208
}
214209

210+
res.sort((a, b) => b.score - a.score);
211+
215212
for (
216213
let i = 0, c = Math.min(searchConfig.numResults ?? 10, res.length);
217214
i < c;

src/lib/utils/options/declaration.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ const Kinds = Object.values(ReflectionKind);
5454
export interface SearchConfig {
5555
numResults?: number;
5656
boosts?: {
57-
exactMatch?: number;
5857
byKind?: { [key: typeof Kinds[number]]: number };
5958
byCategory?: { [key: string]: number };
6059
};

0 commit comments

Comments
 (0)