Skip to content

Commit

Permalink
docs: update to sphinx 5.1 (#565)
Browse files Browse the repository at this point in the history
Co-authored-by: shiftinv <8530778+shiftinv@users.noreply.github.com>
  • Loading branch information
onerandomusername and shiftinv committed Jul 26, 2022
1 parent 550f2e3 commit d0a2855
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 574 deletions.
1 change: 1 addition & 0 deletions changelog/565.doc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update sphinx from 4.5.0 to version 5.1, and take advantage of new options.
33 changes: 22 additions & 11 deletions docs/_static/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ let showHideSidebar;
document.addEventListener('DOMContentLoaded', () => {
mobileSearch = new SearchBar();


const search_box = document.querySelector(".search input[type='search']");
const updateSearchPlaceholder = function() {
if (window.matchMedia && window.matchMedia('(max-width: 600px)').matches) {
Expand Down Expand Up @@ -215,25 +214,25 @@ document.addEventListener('DOMContentLoaded', () => {


function focusSearch() {
$('input[name=q]').first().focus();
document.querySelector("input[name=q]")?.focus();
}


function unfocusSearch() {
$('input[name=q]').first().blur();
document.querySelector("input[name=q]")?.blur();
}


$(document).keydown((event) => {
document.addEventListener("keydown", (event) => {
if (event.altKey || event.metaKey)
return;

const focusedElement = document.activeElement;
if (["TEXTAREA", "INPUT", "SELECT", "BUTTON"].includes(focusedElement.tagName)) {
// handle `escape` in search field
if (!event.ctrlKey && !event.shiftKey && event.key === "Escape" && $('input[name=q]').first().is(focusedElement)) {
if (!event.ctrlKey && !event.shiftKey && event.key === "Escape" && document.querySelector("input[name=q]") === focusedElement) {
unfocusSearch();
return false;
return event.preventDefault();
}
// otherwise, ignore all key presses in inputs
return;
Expand All @@ -243,25 +242,37 @@ $(document).keydown((event) => {
// close modal using `escape`, if modal exists
if (event.key === "Escape" && activeModal) {
activeModal.close();
return false;
return event.preventDefault();
}

// focus search using `s` (`/` is already supported by Sphinx)
if (event.key === "s") {
focusSearch();
return false;
return event.preventDefault();
}
}

// focus search using `ctrl+k`
if (!event.shiftKey && event.ctrlKey && event.key == "k") {
focusSearch();
return false;
return event.preventDefault();
}
});

$(document).ready(function () {
$('a.external').attr('target', '_blank');

const _doc_ready = (callback) => {
if (document.readyState !== "loading") {
callback();
} else {
document.addEventListener("DOMContentLoaded", callback);
}
};


_doc_ready(() => {
document.querySelectorAll("a.external").forEach((a) => {
a.setAttribute("target", "_blank");
})
});

var url = new URL(window.location.href);
Expand Down
31 changes: 18 additions & 13 deletions docs/_static/scorer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use-strict';

let queryBeingDone = null;
let pattern = null;
let queryBeingDone = undefined;
let pattern = undefined;

const escapedRegex = /[-\/\\^$*+?.()|[\]{}]/g;
function escapeRegex(e) {
Expand All @@ -23,14 +23,28 @@ function __cleanNamespaces(query) {
return query.replace(/(disnake\.(ext\.)?)?(.+)/, '$3');
}

Scorer = {
function __setPattern() {
const params = new URLSearchParams(window.location.search);
queryBeingDone = params.get('q');
if (queryBeingDone) {
pattern = new RegExp(Array.from(queryBeingDone).map(escapeRegex).join('.*?'), 'i');
} else {
queryBeingDone = null;
pattern = null;
}
}

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;
const [, title, , , score,] = result;

if (queryBeingDone === undefined) {
__setPattern();
}

if (pattern !== null && title.startsWith('disnake.')) {
let _score = __score(title, pattern);
Expand Down Expand Up @@ -64,12 +78,3 @@ Scorer = {
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');
}
});

0 comments on commit d0a2855

Please sign in to comment.