Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #712: Fix BLT's ReadTheDocs search. #713

Merged
merged 1 commit into from
Nov 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ site_description: 'BLT - Acquia Build & Launch Tools'
theme: readthedocs
strict: true

extra_javascript:
- readme/js/fix_search.js

markdown_extensions:
- toc:
permalink: True
Expand Down
44 changes: 44 additions & 0 deletions readme/js/fix_search.js
Original file line number Diff line number Diff line change
@@ -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');
$('<input>').attr({
type: "text",
name: "q",
placeholder: "Search docs"
}).appendTo(form);
});

observer.observe(target, config);
});
}());