Skip to content

Commit 2928586

Browse files
committed
Adjust warning to say when anchor content not found
1 parent 3bec276 commit 2928586

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

sphinx/themes/basic/static/searchtools.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,19 @@ const Search = {
163163
htmlToText: (htmlString, anchor) => {
164164
const htmlElement = new DOMParser().parseFromString(htmlString, 'text/html');
165165
htmlElement.querySelectorAll(".headerlink").forEach((el) => { el.remove() });
166-
let docContent = undefined;
167-
if (anchor && htmlElement.querySelector(anchor)) {
168-
docContent = htmlElement.querySelector(anchor);
169-
} else {
170-
docContent = htmlElement.querySelector('[role="main"]');
166+
if (anchor) {
167+
const anchorContent = htmlElement.querySelector(anchor);
168+
if (anchorContent) return anchorContent.textContent;
169+
170+
console.warn(
171+
`Anchor block not found. Sphinx search tries to obtain it via '${anchor}'. Check your theme or template.`
172+
);
171173
}
174+
175+
// if anchor not specified or not found, fall back to main content
176+
const docContent = htmlElement.querySelector('[role="main"]');
172177
if (docContent) return docContent.textContent;
178+
173179
console.warn(
174180
"Content block not found. Sphinx search tries to obtain it via '[role=main]'. Could you check your theme or template."
175181
);

0 commit comments

Comments
 (0)