From 9d9e1b6dfd3734d46ab6660a7ee50beb2a2bdf7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Wed, 16 Nov 2016 15:36:43 -0500 Subject: [PATCH] Fix ReadTheDocs search feature --- docs/js/fix_search.js | 44 +++++++++++++++++++++++++++++++++++++++++++ mkdocs.yml | 3 +++ 2 files changed, 47 insertions(+) create mode 100644 docs/js/fix_search.js diff --git a/docs/js/fix_search.js b/docs/js/fix_search.js new file mode 100644 index 000000000..6dd9fb202 --- /dev/null +++ b/docs/js/fix_search.js @@ -0,0 +1,44 @@ +(function (){ + var MutationObserver = (function () { + var prefixes = ['WebKit', 'Moz', 'O', 'Ms', ''] + for (var i=0; i < prefixes.length; i++) { + if (prefixes[i] + 'MutationObserver' in window) { + return window[prefixes[i] + 'MutationObserver']; + } + } + return false; + }()); + + /* + * 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. + * + * @see https://github.com/rtfd/readthedocs.org/issues/1088#issuecomment-224715045 + */ + $(document).ready(function () { + if (!MutationObserver) { + return; + } + 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'); + var path = window.location.pathname; + var branch = path.split('/')[2]; + form.empty(); + form.attr('action', window.location.origin + '/en/' + branch + '/search.html'); + $('').attr({ + type: "text", + name: "q", + placeholder: "Search docs" + }).appendTo(form); + }); + + observer.observe(target, config); + }); +}()); diff --git a/mkdocs.yml b/mkdocs.yml index 7aa135bfd..7bfed0986 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -4,6 +4,9 @@ repo_url: https://github.com/geerlingguy/drupal-vm site_description: 'Drupal VM - A VM for local Drupal development, built with Vagrant + Ansible' theme: readthedocs +extra_javascript: + - js/fix_search.js + markdown_extensions: - toc: permalink: True