From df1debf775b98ed16132b011b5defb4c807ddbea Mon Sep 17 00:00:00 2001 From: BobDotCom <71356958+BobDotCom@users.noreply.github.com> Date: Thu, 10 Nov 2022 10:44:10 -0600 Subject: [PATCH 1/2] Add back search scorer --- docs/_static/js/scorer.js | 82 +++++++++++++++++++++++++++++++++++++++ docs/conf.py | 2 +- 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 docs/_static/js/scorer.js diff --git a/docs/_static/js/scorer.js b/docs/_static/js/scorer.js new file mode 100644 index 0000000000..a0f50cb083 --- /dev/null +++ b/docs/_static/js/scorer.js @@ -0,0 +1,82 @@ +"use-strict"; + +let queryBeingDone = null; +let pattern = null; + +const escapedRegex = /[-\/\\^$*+?.()|[\]{}]/g; +function escapeRegex(e) { + return e.replace(escapedRegex, "\\$&"); +} + +// for some reason Sphinx shows some entries twice +// if something has been scored already I'd rather sort it to the bottom +const beenScored = new Set(); + +function __score(haystack, regex) { + let match = regex.exec(haystack); + if (match == null) { + return Number.MAX_VALUE; + } + let subLength = match[0].length; + let start = match.index; + return (subLength * 1000 + start) / 1000.0; +} + +// unused for now +function __cleanNamespaces(query) { + return query.replace(/(discord\.(ext\.)?)?(.+)/, "$3"); +} + +Scorer = { + // Implement the following function to further tweak the score for each result + // The function takes a result array [filename, title, anchor, descr, score] + // and returns the new score. + score: (result) => { + // only inflate the score of things that are actual API reference things + const [, title, , , score] = result; + + if (pattern !== null && title.startsWith("discord.")) { + let _score = __score(title, pattern); + if (_score === Number.MAX_VALUE) { + return score; + } + if (beenScored.has(title)) { + return 0; + } + beenScored.add(title); + let newScore = 100 + queryBeingDone.length - _score; + // console.log(`${title}: ${score} -> ${newScore} (${_score})`); + return newScore; + } + return score; + }, + + // query matches the full name of an object + objNameMatch: 15, + // or matches in the last dotted part of the object name + objPartialMatch: 11, + // Additive scores depending on the priority of the object + objPrio: { + 0: 15, // used to be importantResults + 1: 7, // used to be objectResults + 2: -5, // used to be unimportantResults + }, + // Used when the priority is not in the mapping. + objPrioDefault: 0, + + // query found in title + title: 15, + partialTitle: 7, + // query found in terms + term: 5, + partialTerm: 2, +}; + +document.addEventListener("DOMContentLoaded", () => { + const params = new URLSearchParams(window.location.search); + queryBeingDone = params.get("q"); + if (queryBeingDone) { + let pattern = Array.from(queryBeingDone).map(escapeRegex).join(".*?"); + pattern = new RegExp(pattern, "i"); + } +}); diff --git a/docs/conf.py b/docs/conf.py index a3a7536b75..e12f9cb1b3 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -379,7 +379,7 @@ def write_new(): # The name of a javascript file (relative to the configuration directory) that # implements a search results scorer. If empty, the default will be used. -# html_search_scorer = "_static/scorer.js" +html_search_scorer = "_static/scorer.js" # html_js_files = ["custom.js", "settings.js", "copy.js", "sidebar.js"] From eb5c5309648f25fb3f486f9fdb6fa3b6bdee2e8e Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Mon, 17 Apr 2023 17:34:56 +0200 Subject: [PATCH 2/2] Update docs/conf.py Co-authored-by: plun1331 <49261529+plun1331@users.noreply.github.com> Signed-off-by: Lala Sabathil --- docs/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index ede29e7ada..38958e6066 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -380,7 +380,7 @@ def write_new(): # The name of a javascript file (relative to the configuration directory) that # implements a search results scorer. If empty, the default will be used. -html_search_scorer = "_static/scorer.js" +html_search_scorer = "_static/js/scorer.js" # html_js_files = ["custom.js", "settings.js", "copy.js", "sidebar.js"]