Skip to content

Commit

Permalink
sphinx-doc#1618 make search results reader friendly
Browse files Browse the repository at this point in the history
request results as HTML instead of source files
retrieve preview snippet text from HTML
  • Loading branch information
TimKam committed Aug 23, 2017
1 parent ddbc205 commit 14e39ad
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions sphinx/themes/basic/static/searchtools.js_t
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ var Search = {
_queued_query : null,
_pulse_status : -1,

htmlToText : function(htmlString) {
var htmlElement = document.createElement('span');
htmlElement.innerHTML = htmlString;
$(htmlElement).find('.headerlink').remove();
docContent = $(htmlElement).find('[role=main]')[0];
return docContent.textContent || docContent.innerText;
},

init : function() {
var params = $.getQueryParameters();
if (params.q) {
Expand Down Expand Up @@ -268,8 +276,7 @@ var Search = {
displayNextItem();
});
} else if (DOCUMENTATION_OPTIONS.HAS_SOURCE) {
var suffix = DOCUMENTATION_OPTIONS.SOURCELINK_SUFFIX;
$.ajax({url: DOCUMENTATION_OPTIONS.URL_ROOT + '_sources/' + item[5] + (item[5].slice(-suffix.length) === suffix ? '' : suffix),
$.ajax({url: DOCUMENTATION_OPTIONS.URL_ROOT + item[0] + '.html',
dataType: "text",
complete: function(jqxhr, textstatus) {
var data = jqxhr.responseText;
Expand Down Expand Up @@ -462,7 +469,8 @@ var Search = {
* words. the first one is used to find the occurrence, the
* latter for highlighting it.
*/
makeSearchSummary : function(text, keywords, hlwords) {
makeSearchSummary : function(htmlText, keywords, hlwords) {
var text = Search.htmlToText(htmlText);
var textLower = text.toLowerCase();
var start = 0;
$.each(keywords, function() {
Expand Down

0 comments on commit 14e39ad

Please sign in to comment.