-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Highlight searchterms in mkdocs projects
Sphinx has a theme agnostic feature to highlight terms given by the "highlight" GET parameter. This is used by RTD when linking from the search page to the documentation page. This did not work for mkdocs projects yet. We add the logic for this to readthedocs-embed-js in order to make this feature available independently of the used mkdocs theme.
- Loading branch information
1 parent
f6d8b2f
commit 04077a9
Showing
4 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
readthedocs/core/static-src/core/js/highlight-searchterm.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Allow highlighting of search terms, passed in as "highlight" GET parameter. | ||
* | ||
* This imitades the behaviour of what is implemented in Sphinx's doctools.js. | ||
* Mkdocs does not provide a similiar logic, we implement it here instead which | ||
* will work theme agnostic. | ||
*/ | ||
|
||
|
||
require('./jquery.highlighttext'); | ||
|
||
|
||
function init() { | ||
highlightSearchWords(); | ||
} | ||
|
||
|
||
function urldecode(x) { | ||
return decodeURIComponent(x).replace(/\+/g, ' '); | ||
} | ||
|
||
|
||
function getQueryParameters(s) { | ||
if (typeof s == 'undefined') | ||
s = document.location.search; | ||
var parts = s.substr(s.indexOf('?') + 1).split('&'); | ||
var result = {}; | ||
for (var i = 0; i < parts.length; i++) { | ||
var tmp = parts[i].split('=', 2); | ||
var key = urldecode(tmp[0]); | ||
var value = urldecode(tmp[1]); | ||
if (key in result) | ||
result[key].push(value); | ||
else | ||
result[key] = [value]; | ||
} | ||
return result; | ||
} | ||
|
||
|
||
function highlightSearchWords() { | ||
var params = getQueryParameters(); | ||
var terms = (params.highlight) ? params.highlight[0].split(/\s+/) : []; | ||
if (terms.length) { | ||
var body = $('div.body'); | ||
if (!body.length) { | ||
var body = $('body'); | ||
} | ||
window.setTimeout(function() { | ||
$.each(terms, function() { | ||
body.highlightText(this.toLowerCase(), 'highlighted'); | ||
}); | ||
}, 10); | ||
} | ||
} | ||
|
||
|
||
module.exports = { | ||
init: init | ||
}; |
29 changes: 29 additions & 0 deletions
29
readthedocs/core/static-src/core/js/jquery.highlighttext.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* highlight a given string on a jquery object by wrapping it in | ||
* span elements with the given class name. | ||
*/ | ||
jQuery.fn.highlightText = function(text, className) { | ||
function highlight(node) { | ||
if (node.nodeType == 3) { | ||
var val = node.nodeValue; | ||
var pos = val.toLowerCase().indexOf(text); | ||
if (pos >= 0 && !jQuery(node.parentNode).hasClass(className)) { | ||
var span = document.createElement("span"); | ||
span.className = className; | ||
span.appendChild(document.createTextNode(val.substr(pos, text.length))); | ||
node.parentNode.insertBefore(span, node.parentNode.insertBefore( | ||
document.createTextNode(val.substr(pos + text.length)), | ||
node.nextSibling)); | ||
node.nodeValue = val.substr(0, pos); | ||
} | ||
} | ||
else if (!jQuery(node).is("button, select, textarea")) { | ||
jQuery.each(node.childNodes, function() { | ||
highlight(this); | ||
}); | ||
} | ||
} | ||
return this.each(function() { | ||
highlight(this); | ||
}); | ||
}; |
Oops, something went wrong.