mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Fix minor HTML search summary issues (#10548)
This commit is contained in:
parent
b3e03d9d07
commit
ede71fd644
@ -158,7 +158,7 @@ const Search = {
|
|||||||
const htmlElement = document
|
const htmlElement = document
|
||||||
.createRange()
|
.createRange()
|
||||||
.createContextualFragment(htmlString);
|
.createContextualFragment(htmlString);
|
||||||
_removeChildren(htmlElement.querySelectorAll(".headerlink"));
|
htmlElement.querySelectorAll(".headerlink").forEach((el) => el.parentNode.removeChild(el));
|
||||||
const docContent = htmlElement.querySelector('[role="main"]');
|
const docContent = htmlElement.querySelector('[role="main"]');
|
||||||
if (docContent !== undefined) return docContent.textContent;
|
if (docContent !== undefined) return docContent.textContent;
|
||||||
console.warn(
|
console.warn(
|
||||||
@ -504,11 +504,12 @@ const Search = {
|
|||||||
* latter for highlighting it.
|
* latter for highlighting it.
|
||||||
*/
|
*/
|
||||||
makeSearchSummary: (htmlText, keywords, highlightWords) => {
|
makeSearchSummary: (htmlText, keywords, highlightWords) => {
|
||||||
const text = Search.htmlToText(htmlText).toLowerCase();
|
const text = Search.htmlToText(htmlText);
|
||||||
if (text === "") return null;
|
if (text === "") return null;
|
||||||
|
|
||||||
|
const textLower = text.toLowerCase();
|
||||||
const actualStartPosition = [...keywords]
|
const actualStartPosition = [...keywords]
|
||||||
.map((k) => text.indexOf(k.toLowerCase()))
|
.map((k) => textLower.indexOf(k.toLowerCase()))
|
||||||
.filter((i) => i > -1)
|
.filter((i) => i > -1)
|
||||||
.slice(-1)[0];
|
.slice(-1)[0];
|
||||||
const startWithContext = Math.max(actualStartPosition - 120, 0);
|
const startWithContext = Math.max(actualStartPosition - 120, 0);
|
||||||
@ -516,7 +517,7 @@ const Search = {
|
|||||||
const top = startWithContext === 0 ? "" : "...";
|
const top = startWithContext === 0 ? "" : "...";
|
||||||
const tail = startWithContext + 240 < text.length ? "..." : "";
|
const tail = startWithContext + 240 < text.length ? "..." : "";
|
||||||
|
|
||||||
let summary = document.createElement("div");
|
let summary = document.createElement("p");
|
||||||
summary.classList.add("context");
|
summary.classList.add("context");
|
||||||
summary.innerText = top + text.substr(startWithContext, 240).trim() + tail;
|
summary.innerText = top + text.substr(startWithContext, 240).trim() + tail;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user