4
4
5
5
var rtddata = require ( './rtd-data' ) ;
6
6
var xss = require ( 'xss/lib/index' ) ;
7
+ var MAX_RESULT_PER_SECTION = 3 ;
8
+ var MAX_SUBSTRING_LIMIT = 100 ;
7
9
8
10
9
11
/*
@@ -35,14 +37,25 @@ function attach_elastic_search_query(data) {
35
37
for ( var i = 0 ; i < hit_list . length ; i += 1 ) {
36
38
var doc = hit_list [ i ] ;
37
39
var highlight = doc . highlight ;
40
+ var inner_hits = doc . inner_hits || [ ] ;
38
41
var list_item = $ ( '<li style="display: none;"></li>' ) ;
39
42
43
+ var title = doc . title ;
44
+ // if highlighted title is present,
45
+ // use that.
46
+ if ( highlight ) {
47
+ if ( highlight . title ) {
48
+ title = xss ( highlight . title [ 0 ] ) ;
49
+ }
50
+ }
51
+
40
52
// Creating the result from elements
41
- var link = doc . link + DOCUMENTATION_OPTIONS . FILE_SUFFIX +
42
- ' ?highlight=' + $ . urlencode ( query ) ;
53
+ var link = doc . link + DOCUMENTATION_OPTIONS . FILE_SUFFIX ;
54
+ var highlight_link = link + " ?highlight=" + $ . urlencode ( query ) ;
43
55
44
- var item = $ ( '<a>' , { 'href' : link } ) ;
45
- item . html ( doc . title ) ;
56
+ var item = $ ( '<a>' , { 'href' : highlight_link } ) ;
57
+ item . html ( title ) ;
58
+ item . find ( 'em' ) . addClass ( 'highlighted' ) ;
46
59
list_item . append ( item ) ;
47
60
48
61
// If the document is from subproject, add extra information
@@ -53,20 +66,111 @@ function attach_elastic_search_query(data) {
53
66
list_item . append ( extra ) ;
54
67
}
55
68
56
- // Show highlighted texts
57
- if ( highlight . content ) {
58
- for ( var index = 0 ; index < highlight . content . length ; index += 1 ) {
59
- if ( index < 3 ) {
60
- // Show up to 3 results for search
61
- var content = highlight . content [ index ] ;
62
- var content_text = xss ( content ) ;
63
- var contents = $ ( '<div class="context">' ) ;
64
-
65
- contents . html ( "..." + content_text + "..." ) ;
66
- contents . find ( 'em' ) . addClass ( 'highlighted' ) ;
67
- list_item . append ( contents ) ;
69
+ for ( var j = 0 ; j < inner_hits . length ; j += 1 ) {
70
+
71
+ var contents = $ ( '<div class="context">' ) ;
72
+
73
+ var section_template = '' +
74
+ '<div>' +
75
+ '<a href="<%= section_subtitle_link %>">' +
76
+ '<%= section_subtitle %>' +
77
+ '</a>' +
78
+ '</div>' +
79
+ '<% for (var i = 0; i < section_content.length; ++i) { %>' +
80
+ '<div>' +
81
+ '<%= section_content[i] %>' +
82
+ '</div>' +
83
+ '<% } %>' ;
84
+
85
+ var domain_template = '' +
86
+ '<div>' +
87
+ '<a href="<%= domain_subtitle_link %>">' +
88
+ '<%= domain_subtitle %>' +
89
+ '</a>' +
90
+ '</div>' +
91
+ '<span>' +
92
+ '<%= domain_content %>' +
93
+ '</span>' ;
94
+
95
+ // if the result is page section
96
+ if ( inner_hits [ j ] . type === "sections" ) {
97
+
98
+ var section = inner_hits [ j ] ;
99
+ var section_subtitle = section . _source . title ;
100
+ var section_subtitle_link = link + "#" + section . _source . id ;
101
+ var section_content = [ section . _source . content . substring ( 0 , MAX_SUBSTRING_LIMIT ) + " ..." ] ;
102
+
103
+ if ( section . highlight ) {
104
+ if ( section . highlight [ "sections.title" ] ) {
105
+ section_subtitle = xss ( section . highlight [ "sections.title" ] [ 0 ] ) ;
106
+ }
107
+
108
+ if ( section . highlight [ "sections.content" ] ) {
109
+ var content = section . highlight [ "sections.content" ] ;
110
+ section_content = [ ] ;
111
+ for (
112
+ var k = 0 ;
113
+ k < content . length && k < MAX_RESULT_PER_SECTION ;
114
+ k += 1
115
+ ) {
116
+ section_content . push ( "... " + xss ( content [ k ] ) + " ..." ) ;
117
+ }
118
+ }
68
119
}
120
+
121
+ contents . append (
122
+ $u . template (
123
+ section_template ,
124
+ {
125
+ section_subtitle_link : section_subtitle_link ,
126
+ section_subtitle : section_subtitle ,
127
+ section_content : section_content
128
+ }
129
+ )
130
+ ) ;
69
131
}
132
+
133
+ // if the result is a sphinx domain object
134
+ if ( inner_hits [ j ] . type === "domains" ) {
135
+
136
+ var domain = inner_hits [ j ] ;
137
+ var domain_subtitle = domain . _source . role_name ;
138
+ var domain_subtitle_link = link + "#" + domain . _source . anchor ;
139
+ var domain_content = "" ;
140
+ var domain_name = domain . _source . name ;
141
+
142
+ if (
143
+ typeof domain . _source . display_name === "string" &&
144
+ domain . _source . display_name . length >= 1
145
+ ) {
146
+ domain_subtitle = "(" + domain . _source . role_name + ") " + domain . _source . display_name ;
147
+ }
148
+
149
+ if ( domain . highlight ) {
150
+ if ( domain . highlight [ "domains.name" ] ) {
151
+ // domain_content = type_display -- name
152
+ domain_name = xss ( domain . highlight [ "domains.name" ] [ 0 ] ) ;
153
+ }
154
+ }
155
+
156
+ // domain_content = type_display -- name -- in doc_display
157
+ domain_content = domain . _source . type_display + " -- " + domain_name + " -- in " + domain . _source . doc_display ;
158
+
159
+ contents . append (
160
+ $u . template (
161
+ domain_template ,
162
+ {
163
+ domain_subtitle_link : domain_subtitle_link ,
164
+ domain_subtitle : domain_subtitle ,
165
+ domain_content : domain_content
166
+ }
167
+ )
168
+ ) ;
169
+ }
170
+
171
+ contents . find ( 'em' ) . addClass ( 'highlighted' ) ;
172
+ list_item . append ( contents ) ;
173
+ list_item . append ( $ ( "<br>" ) ) ;
70
174
}
71
175
72
176
Search . output . append ( list_item ) ;
0 commit comments