[matrix] Refactor docs JS
* Refactor custom.js * Refactor scorer.js * tables variable shoudn't be in global scope
This commit is contained in:
		
							
								
								
									
										71
									
								
								docs/_static/scorer.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										71
									
								
								docs/_static/scorer.js
									
									
									
									
										vendored
									
									
								
							@@ -1,17 +1,20 @@
 | 
			
		||||
var _queryBeingDone = null;
 | 
			
		||||
var _pattern = null;
 | 
			
		||||
var _escapedRegex = /[-\/\\^$*+?.()|[\]{}]/g;
 | 
			
		||||
'use-strict';
 | 
			
		||||
 | 
			
		||||
let queryBeingDone = null;
 | 
			
		||||
let pattern = null;
 | 
			
		||||
 | 
			
		||||
const escapedRegex = /[-\/\\^$*+?.()|[\]{}]/g;
 | 
			
		||||
function escapeRegex(e) {
 | 
			
		||||
    return e.replace(_escapedRegex, '\\$&');
 | 
			
		||||
    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
 | 
			
		||||
var _beenScored = new Set();
 | 
			
		||||
const beenScored = new Set();
 | 
			
		||||
 | 
			
		||||
function __score(haystack, regex) {
 | 
			
		||||
    let match = regex.exec(haystack);
 | 
			
		||||
    if(match == null) {
 | 
			
		||||
    if (match == null) {
 | 
			
		||||
        return Number.MAX_VALUE;
 | 
			
		||||
    }
 | 
			
		||||
    let subLength = match[0].length;
 | 
			
		||||
@@ -24,38 +27,41 @@ function __cleanNamespaces(query) {
 | 
			
		||||
    return query.replace(/(discord\.(ext\.)?)?(.+)/, '$3');
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var Scorer = {
 | 
			
		||||
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;
 | 
			
		||||
 | 
			
		||||
    score: function(result) {
 | 
			
		||||
      // only inflate the score of things that are actual API reference things
 | 
			
		||||
      if(_pattern !== null && result[1].startsWith('discord.')) {
 | 
			
		||||
        let _score = __score(result[1], _pattern);
 | 
			
		||||
        if(_score === Number.MAX_VALUE) {
 | 
			
		||||
            return result[4];
 | 
			
		||||
        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;
 | 
			
		||||
        }
 | 
			
		||||
        if(_beenScored.has(result[1])) {
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
        _beenScored.add(result[1]);
 | 
			
		||||
        let newScore = 100 + _queryBeingDone.length - _score;
 | 
			
		||||
        // console.log(`${result[1]}: ${result[4]} -> ${newScore} (${_score})`);
 | 
			
		||||
        return newScore;
 | 
			
		||||
      }
 | 
			
		||||
      return result[4];
 | 
			
		||||
        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
 | 
			
		||||
    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,
 | 
			
		||||
 | 
			
		||||
@@ -67,12 +73,11 @@ var Scorer = {
 | 
			
		||||
    partialTerm: 2
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
$(document).ready(function() {
 | 
			
		||||
    let params = $.getQueryParameters();
 | 
			
		||||
    if(params.q) {
 | 
			
		||||
        _queryBeingDone = params.q[0];
 | 
			
		||||
        let pattern = Array.from(_queryBeingDone).map(escapeRegex).join('.*?');
 | 
			
		||||
        _pattern = new RegExp(pattern, 'i');
 | 
			
		||||
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');
 | 
			
		||||
    }
 | 
			
		||||
});
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user