|
| 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 | +}); |
0 commit comments