Skip to content

Commit f5e780a

Browse files
BobDotComLulalabyplun1331
authored
chore(docs): revert removal of custom search scoring (#2019)
Signed-off-by: Lala Sabathil <[email protected]> Co-authored-by: Lala Sabathil <[email protected]> Co-authored-by: plun1331 <[email protected]>
1 parent eb4fd12 commit f5e780a

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

docs/_static/js/scorer.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"use-strict";
2+
3+
let queryBeingDone = null;
4+
let pattern = null;
5+
6+
const escapedRegex = /[-\/\\^$*+?.()|[\]{}]/g;
7+
function escapeRegex(e) {
8+
return e.replace(escapedRegex, "\\$&");
9+
}
10+
11+
// for some reason Sphinx shows some entries twice
12+
// if something has been scored already I'd rather sort it to the bottom
13+
const beenScored = new Set();
14+
15+
function __score(haystack, regex) {
16+
let match = regex.exec(haystack);
17+
if (match == null) {
18+
return Number.MAX_VALUE;
19+
}
20+
let subLength = match[0].length;
21+
let start = match.index;
22+
return (subLength * 1000 + start) / 1000.0;
23+
}
24+
25+
// unused for now
26+
function __cleanNamespaces(query) {
27+
return query.replace(/(discord\.(ext\.)?)?(.+)/, "$3");
28+
}
29+
30+
Scorer = {
31+
// Implement the following function to further tweak the score for each result
32+
// The function takes a result array [filename, title, anchor, descr, score]
33+
// and returns the new score.
34+
score: (result) => {
35+
// only inflate the score of things that are actual API reference things
36+
const [, title, , , score] = result;
37+
38+
if (pattern !== null && title.startsWith("discord.")) {
39+
let _score = __score(title, pattern);
40+
if (_score === Number.MAX_VALUE) {
41+
return score;
42+
}
43+
if (beenScored.has(title)) {
44+
return 0;
45+
}
46+
beenScored.add(title);
47+
let newScore = 100 + queryBeingDone.length - _score;
48+
// console.log(`${title}: ${score} -> ${newScore} (${_score})`);
49+
return newScore;
50+
}
51+
return score;
52+
},
53+
54+
// query matches the full name of an object
55+
objNameMatch: 15,
56+
// or matches in the last dotted part of the object name
57+
objPartialMatch: 11,
58+
// Additive scores depending on the priority of the object
59+
objPrio: {
60+
0: 15, // used to be importantResults
61+
1: 7, // used to be objectResults
62+
2: -5, // used to be unimportantResults
63+
},
64+
// Used when the priority is not in the mapping.
65+
objPrioDefault: 0,
66+
67+
// query found in title
68+
title: 15,
69+
partialTitle: 7,
70+
// query found in terms
71+
term: 5,
72+
partialTerm: 2,
73+
};
74+
75+
document.addEventListener("DOMContentLoaded", () => {
76+
const params = new URLSearchParams(window.location.search);
77+
queryBeingDone = params.get("q");
78+
if (queryBeingDone) {
79+
let pattern = Array.from(queryBeingDone).map(escapeRegex).join(".*?");
80+
pattern = new RegExp(pattern, "i");
81+
}
82+
});

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ def write_new():
380380

381381
# The name of a javascript file (relative to the configuration directory) that
382382
# implements a search results scorer. If empty, the default will be used.
383-
# html_search_scorer = "_static/scorer.js"
383+
html_search_scorer = "_static/js/scorer.js"
384384

385385
# html_js_files = ["custom.js", "settings.js", "copy.js", "sidebar.js"]
386386

0 commit comments

Comments
 (0)