Skip to content

Commit

Permalink
Fix minor HTML search summary issues (#10548)
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftinv committed Jul 12, 2022
1 parent b3e03d9 commit ede71fd
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions sphinx/themes/basic/static/searchtools.js
Expand Up @@ -158,7 +158,7 @@ const Search = {
const htmlElement = document
.createRange()
.createContextualFragment(htmlString);
_removeChildren(htmlElement.querySelectorAll(".headerlink"));
htmlElement.querySelectorAll(".headerlink").forEach((el) => el.parentNode.removeChild(el));
const docContent = htmlElement.querySelector('[role="main"]');
if (docContent !== undefined) return docContent.textContent;
console.warn(
Expand Down Expand Up @@ -504,19 +504,20 @@ const Search = {
* latter for highlighting it.
*/
makeSearchSummary: (htmlText, keywords, highlightWords) => {
const text = Search.htmlToText(htmlText).toLowerCase();
const text = Search.htmlToText(htmlText);
if (text === "") return null;

const textLower = text.toLowerCase();
const actualStartPosition = [...keywords]
.map((k) => text.indexOf(k.toLowerCase()))
.map((k) => textLower.indexOf(k.toLowerCase()))
.filter((i) => i > -1)
.slice(-1)[0];
const startWithContext = Math.max(actualStartPosition - 120, 0);

const top = startWithContext === 0 ? "" : "...";
const tail = startWithContext + 240 < text.length ? "..." : "";

let summary = document.createElement("div");
let summary = document.createElement("p");
summary.classList.add("context");
summary.innerText = top + text.substr(startWithContext, 240).trim() + tail;

Expand Down

0 comments on commit ede71fd

Please sign in to comment.