Skip to content

Commit 464785d

Browse files
authored
Get location of search.html and link to it properly in RTD search (#115)
* Get location of search.html and link to it properly in RTD search * Add case for where the current page is also search.html * Add slash to the url from RTD
1 parent 9dbb780 commit 464785d

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

docs/_static/search.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,37 @@ function txtSearchChange(event) {
290290
matchedResults.sort((a, b) => b.score - a.score);
291291

292292
// Add the "Search for..." item at the top
293-
resultPanel.innerHTML = `<div class='search_result_item' data-type='search'><a href="search.html?q=${encodeURIComponent(searchText)}"><span>Search Documentation for "${escapeHTML(searchText)}"</span></a></div>`;
293+
// Get the URL root from the RTD documentation options
294+
let urlRoot = DOCUMENTATION_OPTIONS.URL_ROOT;
295+
296+
// If the URL root is not set (e.g. for local development), get the script path and go up one directory
297+
if (urlRoot) {
298+
urlRoot = urlRoot + '/';
299+
} else {
300+
// If we're already on the search page, use relative path
301+
if (window.location.pathname.endsWith('/search.html')) {
302+
urlRoot = '';
303+
} else {
304+
try {
305+
const scriptSrc = document.currentScript ? document.currentScript.src :
306+
(document.scripts[document.scripts.length - 1] ? document.scripts[document.scripts.length - 1].src : '');
307+
if (scriptSrc) {
308+
const url = new URL(scriptSrc);
309+
const pathParts = url.pathname.split('/');
310+
pathParts.pop(); // Remove the script filename
311+
pathParts.pop(); // Go up one directory from _static
312+
urlRoot = url.origin + pathParts.join('/') + '/';
313+
} else {
314+
urlRoot = '';
315+
}
316+
} catch (e) {
317+
urlRoot = '';
318+
}
319+
}
320+
}
321+
322+
searchUrl = urlRoot + 'search.html';
323+
resultPanel.innerHTML = `<div class='search_result_item' data-type='search'><a href="${searchUrl}?q=${encodeURIComponent(searchText)}"><span>Search Documentation for "${escapeHTML(searchText)}"</span></a></div>`;
294324

295325
// Add the rest of the results
296326
resultPanel.innerHTML += matchedResults.map(r =>

docs/_templates/sidebar/search.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
<div id="searchKeybindHint" style="visibility: hidden; text-align: center; font-size: 0.75em; margin-bottom: 4px; color: var(--color-foreground-muted);">Press Ctrl+Enter to search page contents</div>
21
<div id="tocSearchPanel">
32
<div id="tocSearchPanelInner">
43
<input type="text" id="txtSearch" placeholder="Search..." autocomplete="off" />
54
</div>
65
<div id="tocSearchResult" style="display: none;"></div>
7-
</div>
6+
</div>

0 commit comments

Comments
 (0)