Skip to content

Commit ed16e56

Browse files
committed
show multiple results per section, if present
1 parent d62bf3e commit ed16e56

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

readthedocs/core/static-src/core/js/doc-embed/search.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
var rtddata = require('./rtd-data');
66
var xss = require('xss/lib/index');
7+
var MAX_RESULT_PER_SECTION = 3;
78

89

910
/*
@@ -74,9 +75,11 @@ function attach_elastic_search_query(data) {
7475
<%= section_subtitle %> \
7576
</a> \
7677
</div> \
77-
<span> \
78-
<%= section_content %> \
79-
</span>';
78+
<% for (var i = 0; i < section_content.length; ++i) { %> \
79+
<div> \
80+
<%= section_content[i] %> \
81+
</div> \
82+
<% } %>';
8083

8184
var domain_template =
8285
'<div> \
@@ -94,15 +97,19 @@ function attach_elastic_search_query(data) {
9497
var section = inner_hits[j];
9598
var section_subtitle = section._source.title;
9699
var section_subtitle_link = link + "#" + section._source.id;
97-
var section_content = section._source.content.substring(0, 100) + " ...";
100+
var section_content = [ section._source.content.substring(0, 100) + " ..." ];
98101

99102
if (section.highlight) {
100103
if (section.highlight["sections.title"]) {
101104
section_subtitle = xss(section.highlight["sections.title"][0]);
102105
}
103106

104107
if (section.highlight["sections.content"]) {
105-
section_content = "... " + xss(section.highlight["sections.content"][0]) +" ...";
108+
var content = section.highlight["sections.content"];
109+
section_content = [];
110+
for (var k = 0; k < content.length && k < MAX_RESULT_PER_SECTION; ++k) {
111+
section_content.push("... " + xss(content[k]) + " ...");
112+
}
106113
}
107114
}
108115

@@ -128,9 +135,9 @@ function attach_elastic_search_query(data) {
128135

129136
if (
130137
typeof domain._source.display_name === "string" &&
131-
domain._source.display_name.length >= 2 // "2" because some domain display_name are "-"
138+
domain._source.display_name.length >= 1
132139
) {
133-
domain_subtitle = "(" + domain._source.role_name + ")" + domain._source.display_name;
140+
domain_subtitle = "(" + domain._source.role_name + ") " + domain._source.display_name;
134141
}
135142

136143
// preparing domain_content

readthedocs/core/static/core/js/readthedocs-doc-embed.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readthedocs/templates/search/elastic_search.html

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,7 @@ <h3>
188188
<p>
189189
<a href="{% doc_url result.project|get_project result.version inner_hit.source.doc_name %}#{{ inner_hit.source.anchor }}">
190190

191-
{% comment %} ">= 2" because some display_name are just "-" {% endcomment %}
192-
{% if inner_hit.source.display_name|length >= 2 %}
191+
{% if inner_hit.source.display_name|length >= 1 %}
193192
({{ inner_hit.source.role_name }}) {{ inner_hit.source.display_name}}
194193
{% else %}
195194
{{ inner_hit.source.role_name }}
@@ -218,15 +217,17 @@ <h3>
218217
{% endif %}
219218
</a>
220219
</p>
221-
<p class="fragment">
222-
{% if inner_hit.highlight|get_key_or_none:"sections.content" %}
223-
{% with l=inner_hit.highlight|get_key_or_none:"sections.content" %}
224-
... {{ l.0|safe }} ...
225-
{% endwith %}
226-
{% else %}
227-
{{ inner_hit.source.content|slice:"100" }} ...
228-
{% endif %}
229-
</p>
220+
{% if inner_hit.highlight|get_key_or_none:"sections.content" %}
221+
{% with section_content=inner_hit.highlight|get_key_or_none:"sections.content" %}
222+
{% for content in section_content %}
223+
<p class="fragment">
224+
... {{ content|safe }} ...
225+
</p>
226+
{% endfor %}
227+
{% endwith %}
228+
{% else %}
229+
{{ inner_hit.source.content|slice:"100" }} ...
230+
{% endif %}
230231
{% endif %}
231232
{% endfor %}
232233
{% endif %}

0 commit comments

Comments
 (0)