/************************************************* * academia * https://github.com/gcushen/hugo-academia * * In-built Fuse based search algorithm. **************************************************/ /* --------------------------------------------------------------------------- * Configuration. * --------------------------------------------------------------------------- */ // Configure Fuse. let fuseOptions = { shouldSort: true, includeMatches: true, tokenize: true, threshold: 0.0, location: 0, distance: 100, maxPatternLength: 32, minMatchCharLength: 2, keys: [ {name:'title', weight:0.99}, /* 1.0 doesn't work o_O */ {name:'summary', weight:0.6}, {name:'authors', weight:0.5}, {name:'content', weight:0.2}, {name:'tags', weight:0.5}, {name:'categories', weight:0.5} ] }; // Configure summary. let summaryLength = 60; /* --------------------------------------------------------------------------- * Functions. * --------------------------------------------------------------------------- */ // Get query from URI. function getSearchQuery(name) { return decodeURIComponent((location.search.split(name + '=')[1] || '').split('&')[0]).replace(/\+/g, ' '); } // Set query in URI without reloading the page. function updateURL(url) { if (history.pushState) { window.history.pushState({path:url}, '', url); } } // Pre-process new search query. function initSearch(force, fuse) { let query = $("#search-query").val(); // If query deleted, clear results. if ( query.length < 1) { $('#search-hits').empty(); } // Check for timer event (enter key not pressed) and query less than minimum length required. if (!force && query.length < fuseOptions.minMatchCharLength) return; // Do search. $('#search-hits').empty(); searchacademia(query, fuse); let newURL = window.location.protocol + "//" + window.location.host + window.location.pathname + '?q=' + encodeURIComponent(query) + window.location.hash; updateURL(newURL); } // Perform search. function searchacademia(query, fuse) { let results = fuse.search(query); // console.log({"results": results}); if (results.length > 0) { $('#search-hits').append('

' + results.length + ' ' + i18n.results + '

'); parseResults(query, results); } else { $('#search-hits').append('
' + i18n.no_results + '
'); } } // Parse search results. function parseResults(query, results) { $.each( results, function(key, value) { let content = value.item.content; let snippet = ""; let snippetHighlights = []; if ( fuseOptions.tokenize ) { snippetHighlights.push(query); } else { $.each( value.matches, function(matchKey, matchValue) { if (matchValue.key == "content") { let start = (matchValue.indices[0][0]-summaryLength>0) ? matchValue.indices[0][0]-summaryLength : 0; let end = (matchValue.indices[0][1]+summaryLength