From bd40f208334fbdc4d55535762eeed90ac0fcca60 Mon Sep 17 00:00:00 2001 From: abaumann Date: Fri, 27 Oct 2017 17:37:23 -0400 Subject: [PATCH] Trying to fix issue with search not working with read the docs: https://github.com/rtfd/readthedocs.org/issues/1487 using fix here https://github.com/nodemcu/nodemcu-firmware/commit/7dd89dd15ef993c9740ba4d4361e6be8edd1284b And added extra_javascript to mkdocs.yml --- docs/js/extra.js | 219 +++++++++++++++++++++++++++++++++++++++++++++++ mkdocs.yml | 2 + 2 files changed, 221 insertions(+) create mode 100644 docs/js/extra.js diff --git a/docs/js/extra.js b/docs/js/extra.js new file mode 100644 index 00000000000..a0224705a88 --- /dev/null +++ b/docs/js/extra.js @@ -0,0 +1,219 @@ +var nodemcu = nodemcu || {}; +(function () { + 'use strict'; + //var languageCodeToNameMap = {en: 'English', de: 'Deutsch'}; + var languageCodeToNameMap = {en: 'English'}; + var languageNames = values(languageCodeToNameMap); + var defaultLanguageCode = 'en'; + + $(document).ready(function () { + addToc(); + fixSearch(); + hideNavigationForAllButSelectedLanguage(); + addLanguageSelectorToRtdFlyOutMenu(); + replaceRelativeLinksWithStaticGitHubUrl(); + }); + + /** + * Adds a TOC-style table to each page in the 'Modules' section. + */ + function addToc() { + var func, intro, tocHtmlTable; + if (isModulePage()) { + tocHtmlTable = ''; + $('h2').each(function (index) { + // 'slice' cuts off the single permalink character at the end of the text (e.g. '¶') + func = $(this).text().slice(0, -1); + // get the first sentence of the paragraph directly below h2 + intro = $(this).next().text(); + intro = intro.substring(0, intro.indexOf('.') + 1); + tocHtmlTable += createTocTableRow(func, intro); + }); + tocHtmlTable += '
'; + $(tocHtmlTable).insertBefore($('h2').first()) + } + function isModulePage() { + // if the breadcrumb contains 'Modules »' it must be an API page + return $("ul.wy-breadcrumbs li:contains('Modules »')").size() > 0; + } + function createTocTableRow(func, intro) { + // fragile attempt to auto-create the in-page anchor + var href = func.replace(/\.|:/g, '').replace('()', '').replace(' --', '-').replace(/ /g, '-'); + var link = '' + func + ''; + return '' + link + '' + intro + ''; + } + } + + /* + * 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]; + // Finds all subnav elements and hides them if they're /language/ subnavs. Hence, all 'Modules' subnav elements + // won't be hidden. + //