-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add section linking for the search result #5829
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
Changes from 50 commits
ee1ba1a
4b05f8a
79d2459
54ceb5c
b11e357
5b81471
762a79d
7a61dbd
644565b
fa51a1c
0bc6be5
0139993
53a02e8
11ba9e7
6207f4e
a251a98
b6847b9
af2d69f
32d0bed
d472f29
878343d
f98d91c
7c1c641
fd8e8f7
f6221ec
8840606
60e229c
3835e2e
ae5033c
28e7cbf
1e2a40b
7b7a3c9
3931bc0
84a2494
5cae508
adb74ed
d500d98
9461d4f
75dcc2f
ea36138
451c0f4
0817d43
5305458
68cb7af
d62bf3e
ed16e56
0ed64f7
f988302
429b3e9
897e09f
aeaba6f
6f9b2bc
6135cde
d3566ac
992c72e
f0babf1
4527839
7e75d7e
1e6721d
2a4c070
4beec39
01346a0
91282de
cfe8f5b
7e99f6a
b7ce777
6701a4e
cee24ed
685f6db
d7edeee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
var rtddata = require('./rtd-data'); | ||
var xss = require('xss/lib/index'); | ||
var MAX_RESULT_PER_SECTION = 3; | ||
|
||
|
||
/* | ||
|
@@ -35,14 +36,25 @@ function attach_elastic_search_query(data) { | |
for (var i = 0; i < hit_list.length; i += 1) { | ||
var doc = hit_list[i]; | ||
var highlight = doc.highlight; | ||
var inner_hits = doc.inner_hits || []; | ||
var list_item = $('<li style="display: none;"></li>'); | ||
|
||
var title = doc.title; | ||
// if highlighted title is present, | ||
// use that. | ||
if (highlight) { | ||
if (highlight.title) { | ||
title = xss(highlight.title[0]); | ||
} | ||
} | ||
|
||
// Creating the result from elements | ||
var link = doc.link + DOCUMENTATION_OPTIONS.FILE_SUFFIX + | ||
'?highlight=' + $.urlencode(query); | ||
var link = doc.link + DOCUMENTATION_OPTIONS.FILE_SUFFIX; | ||
var highlight_link = link + "?highlight=" + $.urlencode(query); | ||
|
||
var item = $('<a>', {'href': link}); | ||
item.html(doc.title); | ||
var item = $('<a>', {'href': highlight_link}); | ||
item.html(title); | ||
item.find('em').addClass('highlighted'); | ||
list_item.append(item); | ||
|
||
// If the document is from subproject, add extra information | ||
|
@@ -53,20 +65,118 @@ function attach_elastic_search_query(data) { | |
list_item.append(extra); | ||
} | ||
|
||
// Show highlighted texts | ||
if (highlight.content) { | ||
for (var index = 0; index < highlight.content.length; index += 1) { | ||
if (index < 3) { | ||
// Show up to 3 results for search | ||
var content = highlight.content[index]; | ||
var content_text = xss(content); | ||
var contents = $('<div class="context">'); | ||
|
||
contents.html("..." + content_text + "..."); | ||
contents.find('em').addClass('highlighted'); | ||
list_item.append(contents); | ||
for (var j = 0; j < inner_hits.length; j += 1) { | ||
|
||
var contents = $('<div class="context">'); | ||
|
||
var section_template = '' + | ||
'<div>' + | ||
'<a href="<%= section_subtitle_link %>">' + | ||
'<%= section_subtitle %>' + | ||
'</a>' + | ||
'</div>' + | ||
'<% for (var i = 0; i < section_content.length; ++i) { %>' + | ||
'<div>' + | ||
'<%= section_content[i] %>' + | ||
'</div>' + | ||
'<% } %>'; | ||
|
||
var domain_template = '' + | ||
'<div>' + | ||
'<a href="<%= domain_subtitle_link %>">' + | ||
'<%= domain_subtitle %>' + | ||
'</a>' + | ||
'</div>' + | ||
'<span>' + | ||
'<%= domain_content %>' + | ||
'</span>'; | ||
|
||
// if the result is page section | ||
if(inner_hits[j].type === "sections") { | ||
|
||
var section = inner_hits[j]; | ||
var section_subtitle = section._source.title; | ||
var section_subtitle_link = link + "#" + section._source.id; | ||
var section_content = [section._source.content.substring(0, 100) + " ..."]; | ||
|
||
if (section.highlight) { | ||
if (section.highlight["sections.title"]) { | ||
section_subtitle = xss(section.highlight["sections.title"][0]); | ||
} | ||
|
||
if (section.highlight["sections.content"]) { | ||
var content = section.highlight["sections.content"]; | ||
section_content = []; | ||
for ( | ||
var k = 0; | ||
k < content.length && k < MAX_RESULT_PER_SECTION; | ||
k += 1 | ||
) { | ||
section_content.push("... " + xss(content[k]) + " ..."); | ||
} | ||
} | ||
} | ||
|
||
contents.append( | ||
$u.template( | ||
section_template, | ||
{ | ||
section_subtitle_link: section_subtitle_link, | ||
section_subtitle: section_subtitle, | ||
section_content: section_content | ||
} | ||
) | ||
); | ||
} | ||
|
||
// if the result is a sphinx domain object | ||
if (inner_hits[j].type === "domains") { | ||
|
||
var domain = inner_hits[j]; | ||
var domain_subtitle = domain._source.role_name; | ||
var domain_subtitle_link = link + "#" + domain._source.anchor; | ||
var domain_content = ""; | ||
|
||
if ( | ||
typeof domain._source.display_name === "string" && | ||
domain._source.display_name.length >= 1 | ||
) { | ||
domain_subtitle = "(" + domain._source.role_name + ") " + domain._source.display_name; | ||
} | ||
|
||
// preparing domain_content | ||
// domain_content = type_display -- | ||
domain_content = domain._source.type_display + " -- "; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels weird. Should this be a template also? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have reduced the |
||
if (domain.highlight) { | ||
if (domain.highlight["domains.name"]) { | ||
// domain_content = type_display -- name | ||
domain_content += xss(domain.highlight["domains.name"][0]); | ||
} else { | ||
// domain_content = type_display -- name | ||
domain_content += domain._source.name; | ||
} | ||
} else { | ||
// domain_content = type_display -- name | ||
domain_content += domain._source.name; | ||
} | ||
// domain_content = type_display -- name -- in doc_display | ||
domain_content += " -- in " + domain._source.doc_display; | ||
|
||
contents.append( | ||
$u.template( | ||
domain_template, | ||
{ | ||
domain_subtitle_link: domain_subtitle_link, | ||
domain_subtitle: domain_subtitle, | ||
domain_content: domain_content | ||
} | ||
) | ||
); | ||
} | ||
|
||
contents.find('em').addClass('highlighted'); | ||
list_item.append(contents); | ||
list_item.append($("<br>")); | ||
} | ||
|
||
Search.output.append(list_item); | ||
|
Large diffs are not rendered by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These strings are really cumbersome, is there not a good way to do multi-line strings in JS? :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are, but eslint is not allowing any: https://travis-ci.org/readthedocs/readthedocs.org/jobs/556276376#L593
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davidfischer thoughts here? Is it too fancy and new?