Skip to content

Commit 7dd89dd

Browse files
committed
Fix search form in RTD docs
1 parent 95d48ad commit 7dd89dd

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

docs/js/extra.js

+29
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var nodemcu = nodemcu || {};
88

99
$(document).ready(function () {
1010
addToc();
11+
fixSearch();
1112
hideNavigationForAllButSelectedLanguage();
1213
addLanguageSelectorToRtdFlyOutMenu();
1314
replaceRelativeLinksWithStaticGitHubUrl();
@@ -43,6 +44,34 @@ var nodemcu = nodemcu || {};
4344
}
4445
}
4546

47+
/*
48+
* RTD messes up MkDocs' search feature by tinkering with the search box defined in the theme, see
49+
* https://github.com/rtfd/readthedocs.org/issues/1088. This function sets up a DOM4 MutationObserver
50+
* to react to changes to the search form (triggered by RTD on doc ready). It then reverts everything
51+
* the RTD JS code modified.
52+
*/
53+
function fixSearch() {
54+
var target = document.getElementById('rtd-search-form');
55+
var config = {attributes: true, childList: true};
56+
57+
var observer = new MutationObserver(function(mutations) {
58+
// if it isn't disconnected it'll loop infinitely because the observed element is modified
59+
observer.disconnect();
60+
var form = $('#rtd-search-form');
61+
form.empty();
62+
form.attr('action', 'https://' + window.location.hostname + '/en/' + determineSelectedBranch() + '/search.html');
63+
$('<input>').attr({
64+
type: "text",
65+
name: "q",
66+
placeholder: "Search docs"
67+
}).appendTo(form);
68+
});
69+
70+
if (window.location.origin.indexOf('readthedocs') > -1) {
71+
observer.observe(target, config);
72+
}
73+
}
74+
4675
function hideNavigationForAllButSelectedLanguage() {
4776
var selectedLanguageCode = determineSelectedLanguageCode();
4877
var selectedLanguageName = languageCodeToNameMap[selectedLanguageCode];

0 commit comments

Comments
 (0)