extra.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. var nodemcu = nodemcu || {};
  2. (function () {
  3. 'use strict';
  4. $(document).ready(function () {
  5. fixSearch();
  6. });
  7. /*
  8. * RTD messes up MkDocs' search feature by tinkering with the search box defined in the theme, see
  9. * https://github.com/rtfd/readthedocs.org/issues/1088. This function sets up a DOM4 MutationObserver
  10. * to react to changes to the search form (triggered by RTD on doc ready). It then reverts everything
  11. * the RTD JS code modified.
  12. */
  13. function fixSearch() {
  14. var target = document.getElementById('rtd-search-form');
  15. var config = {attributes: true, childList: true};
  16. var observer = new MutationObserver(function (mutations) {
  17. // if it isn't disconnected it'll loop infinitely because the observed element is modified
  18. observer.disconnect();
  19. var form = $('#rtd-search-form');
  20. form.empty();
  21. form.attr('action', 'https://' + window.location.hostname + '/en/' + determineSelectedBranch() + '/search.html');
  22. $('<input>').attr({
  23. type: "text",
  24. name: "q",
  25. placeholder: "Search docs"
  26. }).appendTo(form);
  27. });
  28. if (window.location.origin.indexOf('readthedocs') > -1) {
  29. observer.observe(target, config);
  30. }
  31. }
  32. /**
  33. * Analyzes the URL of the current page to find out what the selected GitHub branch is. It's usually
  34. * part of the location path. The code needs to distinguish between running MkDocs standalone
  35. * and docs served from RTD. If no valid branch could be determined 'dev' returned.
  36. *
  37. * @returns GitHub branch name
  38. */
  39. function determineSelectedBranch() {
  40. var branch = 'dev', path = window.location.pathname;
  41. if (window.location.origin.indexOf('readthedocs') > -1) {
  42. // path is like /en/<branch>/<lang>/build/ -> extract 'lang'
  43. // split[0] is an '' because the path starts with the separator
  44. var thirdPathSegment = path.split('/')[2];
  45. // 'latest' is an alias on RTD for the 'dev' branch - which is the default for 'branch' here
  46. if (thirdPathSegment !== 'latest') {
  47. branch = thirdPathSegment;
  48. }
  49. }
  50. return branch;
  51. }
  52. }());