Skip to content

Commit

Permalink
Merge pull request sphinx-doc#10335 from AA-Turner/fix-highlighting-d…
Browse files Browse the repository at this point in the history
…iv-body

Fix search highlighting
  • Loading branch information
tk0miya committed Apr 16, 2022
2 parents c1ee5bc + 0874d6f commit 978b6c8
Showing 1 changed file with 21 additions and 26 deletions.
47 changes: 21 additions & 26 deletions sphinx/themes/basic/static/doctools.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,16 @@ const Documentation = {
* highlight the search words provided in the url in the text
*/
highlightSearchWords: () => {
const highlight = new URLSearchParams(window.location.search).get(
"highlight"
);
const terms = highlight ? highlight.split(/\s+/) : [];
const highlight =
new URLSearchParams(window.location.search).get("highlight") || "";
const terms = highlight.toLowerCase().split(/\s+/);
if (terms.length === 0) return; // nothing to do

let body = document.querySelectorAll("div.body");
if (!body.length) body = document.querySelector("body");
// There should never be more than one element matching "div.body"
const divBody = document.querySelectorAll("div.body");
const body = divBody.length ? divBody[0] : document.querySelector("body");
window.setTimeout(() => {
terms.forEach((term) =>
_highlightText(body, term.toLowerCase(), "highlighted")
);
terms.forEach((term) => _highlightText(body, term, "highlighted"));
}, 10);

const searchBox = document.getElementById("searchbox");
Expand Down Expand Up @@ -169,14 +167,14 @@ const Documentation = {
.querySelectorAll("span.highlighted")
.forEach((el) => el.classList.remove("highlighted"));
const url = new URL(window.location);
url.searchParams.delete('highlight');
window.history.replaceState({}, '', url);
url.searchParams.delete("highlight");
window.history.replaceState({}, "", url);
},

/**
* helper function to focus on search bar
*/
focusSearchBar : () => {
focusSearchBar: () => {
document.querySelectorAll("input[name=q]")[0]?.focus();
},

Expand Down Expand Up @@ -206,9 +204,11 @@ const Documentation = {

initOnKeyListeners: () => {
// only install a listener if it is really needed
if (!DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS &&
!DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS)
return;
if (
!DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS &&
!DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS
)
return;

const blacklistedElements = new Set([
"TEXTAREA",
Expand All @@ -218,14 +218,12 @@ const Documentation = {
]);
document.addEventListener("keydown", (event) => {
if (blacklistedElements.has(document.activeElement.tagName)) return; // bail for input elements
if (event.altKey || event.ctrlKey || event.metaKey)
return; // bail with special keys
if (event.altKey || event.ctrlKey || event.metaKey) return; // bail with special keys

if (!event.shiftKey) {
switch (event.key) {
case "ArrowLeft":
if (!DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS)
break;
if (!DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS) break;

const prevLink = document.querySelector('link[rel="prev"]');
if (prevLink && prevLink.href) {
Expand All @@ -234,8 +232,7 @@ const Documentation = {
}
break;
case "ArrowRight":
if (!DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS)
break;
if (!DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS) break;

const nextLink = document.querySelector('link[rel="next"]');
if (nextLink && nextLink.href) {
Expand All @@ -244,18 +241,16 @@ const Documentation = {
}
break;
case "Escape":
if (!DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS)
break;
if (!DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS) break;
Documentation.hideSearchWords();
event.preventDefault();
}
}

// some keyboard layouts may need Shift to get /
switch (event.key) {
case '/':
if (!DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS)
break;
case "/":
if (!DOCUMENTATION_OPTIONS.ENABLE_SEARCH_SHORTCUTS) break;
Documentation.focusSearchBar();
event.preventDefault();
}
Expand Down

0 comments on commit 978b6c8

Please sign in to comment.