From 7dd89dd15ef993c9740ba4d4361e6be8edd1284b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20St=C3=B6r?= Date: Wed, 8 Jun 2016 22:32:21 +0200 Subject: [PATCH] Fix search form in RTD docs --- docs/js/extra.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/docs/js/extra.js b/docs/js/extra.js index 7c00bfad..a6816960 100644 --- a/docs/js/extra.js +++ b/docs/js/extra.js @@ -8,6 +8,7 @@ var nodemcu = nodemcu || {}; $(document).ready(function () { addToc(); + fixSearch(); hideNavigationForAllButSelectedLanguage(); addLanguageSelectorToRtdFlyOutMenu(); replaceRelativeLinksWithStaticGitHubUrl(); @@ -43,6 +44,34 @@ var nodemcu = nodemcu || {}; } } + /* + * RTD messes up MkDocs' search feature by tinkering with the search box defined in the theme, see + * https://github.com/rtfd/readthedocs.org/issues/1088. This function sets up a DOM4 MutationObserver + * to react to changes to the search form (triggered by RTD on doc ready). It then reverts everything + * the RTD JS code modified. + */ + function fixSearch() { + var target = document.getElementById('rtd-search-form'); + var config = {attributes: true, childList: true}; + + var observer = new MutationObserver(function(mutations) { + // if it isn't disconnected it'll loop infinitely because the observed element is modified + observer.disconnect(); + var form = $('#rtd-search-form'); + form.empty(); + form.attr('action', 'https://' + window.location.hostname + '/en/' + determineSelectedBranch() + '/search.html'); + $('').attr({ + type: "text", + name: "q", + placeholder: "Search docs" + }).appendTo(form); + }); + + if (window.location.origin.indexOf('readthedocs') > -1) { + observer.observe(target, config); + } + } + function hideNavigationForAllButSelectedLanguage() { var selectedLanguageCode = determineSelectedLanguageCode(); var selectedLanguageName = languageCodeToNameMap[selectedLanguageCode];