@@ -7,33 +7,6 @@ var xss = require('xss/lib/index');
7
7
var MAX_RESULT_PER_SECTION = 3 ;
8
8
var MAX_SUBSTRING_LIMIT = 100 ;
9
9
10
- /**
11
- * Use try...catch block to append html to contents
12
- *
13
- * @param {Object } contents html element on which additional html is be appended
14
- * @param {String } template underscore.js template string
15
- * @param {Object } data template vars and their values
16
- */
17
- function append_html_to_contents ( contents , template , data ) {
18
- // underscore.js throws variable not defined error
19
- // because of change of syntax in new versions.
20
- // See: https://stackoverflow.com/a/25881231/8601393
21
- try {
22
- // this is the pre-1.7 syntax from Underscore.js
23
- contents . append (
24
- $u . template (
25
- template ,
26
- data
27
- )
28
- ) ;
29
- }
30
- catch ( error ) {
31
- // this is the new syntax
32
- contents . append (
33
- $u . template ( template ) ( data )
34
- ) ;
35
- }
36
- }
37
10
38
11
/*
39
12
* Search query override for hitting our local API instead of the standard
@@ -50,7 +23,7 @@ function attach_elastic_search_query_sphinx(data) {
50
23
var search_url = document . createElement ( 'a' ) ;
51
24
52
25
search_url . href = data . proxied_api_host + '/api/v2/search/' ;
53
- search_url . search = '?q=' + $ . urlencode ( query ) + '&project=' + project +
26
+ search_url . search = '?q=' + encodeURIComponent ( query ) + '&project=' + project +
54
27
'&version=' + version + '&language=' + language ;
55
28
56
29
/*
@@ -64,6 +37,48 @@ function attach_elastic_search_query_sphinx(data) {
64
37
}
65
38
} ;
66
39
40
+ /**
41
+ * Build a section with its matching results.
42
+ *
43
+ * @param {String } title.
44
+ * @param {String } link.
45
+ * @param {Array } contents.
46
+ */
47
+ var buildSection = function ( title , link , contents ) {
48
+ var div_title = document . createElement ( "div" ) ;
49
+ var a_element = document . createElement ( "a" ) ;
50
+ a_element . href = link ;
51
+ a_element . innerHTML = title ;
52
+ div_title . appendChild ( a_element ) ;
53
+ html = div_title . outerHTML ;
54
+ for ( var i = 0 ; i < contents . length ; i += 1 ) {
55
+ var div_content = document . createElement ( "div" ) ;
56
+ div_content . innerHTML = contents [ i ] ;
57
+ html += div_content . outerHTML ;
58
+ }
59
+ return html ;
60
+ } ;
61
+
62
+ /**
63
+ * Build a domain section.
64
+ *
65
+ * @param {String } title.
66
+ * @param {String } link.
67
+ * @param {String } content.
68
+ */
69
+ var buildDomain = function ( title , link , content ) {
70
+ var div_title = document . createElement ( "div" ) ;
71
+ var a_element = document . createElement ( "a" ) ;
72
+ a_element . href = link ;
73
+ a_element . innerHTML = title ;
74
+ div_title . appendChild ( a_element ) ;
75
+
76
+ var div_content = document . createElement ( "div" ) ;
77
+ div_content . innerHTML = content ;
78
+
79
+ return div_title . outerHTML + div_content . outerHTML ;
80
+ } ;
81
+
67
82
search_def
68
83
. then ( function ( data ) {
69
84
var results = data . results || [ ] ;
@@ -80,7 +95,7 @@ function attach_elastic_search_query_sphinx(data) {
80
95
title = xss ( result . highlights . title [ 0 ] ) ;
81
96
}
82
97
83
- var link = result . path + "?highlight=" + $ . urlencode ( query ) ;
98
+ var link = result . path + "?highlight=" + encodeURIComponent ( query ) ;
84
99
85
100
var item = $ ( '<a>' , { 'href' : link } ) ;
86
101
@@ -100,28 +115,6 @@ function attach_elastic_search_query_sphinx(data) {
100
115
101
116
var contents = $ ( '<div class="context">' ) ;
102
117
103
- var section_template =
104
- '<div>' +
105
- '<a href="<%= section_subtitle_link %>">' +
106
- '<%= section_subtitle %>' +
107
- '</a>' +
108
- '</div>' +
109
- '<% for (var i = 0; i < section_content.length; ++i) { %>' +
110
- '<div>' +
111
- '<%= section_content[i] %>' +
112
- '</div>' +
113
- '<% } %>' ;
114
-
115
- var domain_template =
116
- '<div>' +
117
- '<a href="<%= domain_subtitle_link %>">' +
118
- '<%= domain_subtitle %>' +
119
- '</a>' +
120
- '</div>' +
121
- '<div>' +
122
- '<%= domain_content %>' +
123
- '</div>' ;
124
-
125
118
// if the result is page section
126
119
if ( current_block . type === "section" ) {
127
120
var section = current_block ;
@@ -145,15 +138,11 @@ function attach_elastic_search_query_sphinx(data) {
145
138
}
146
139
}
147
140
148
- append_html_to_contents (
149
- contents ,
150
- section_template ,
151
- {
152
- section_subtitle_link : section_subtitle_link ,
153
- section_subtitle : section_subtitle ,
154
- section_content : section_content
155
- }
156
- ) ;
141
+ contents . append ( buildSection (
142
+ section_subtitle ,
143
+ section_subtitle_link ,
144
+ section_content
145
+ ) ) ;
157
146
}
158
147
159
148
// if the result is a sphinx domain object
@@ -178,15 +167,11 @@ function attach_elastic_search_query_sphinx(data) {
178
167
179
168
var domain_subtitle = "[" + domain_role_name + "]: " + domain_name ;
180
169
181
- append_html_to_contents (
182
- contents ,
183
- domain_template ,
184
- {
185
- domain_subtitle_link : domain_subtitle_link ,
186
- domain_subtitle : domain_subtitle ,
187
- domain_content : domain_content
188
- }
189
- ) ;
170
+ contents . append ( buildDomain (
171
+ domain_subtitle ,
172
+ domain_subtitle_link ,
173
+ domain_content
174
+ ) ) ;
190
175
}
191
176
192
177
contents . find ( 'span' ) . addClass ( 'highlighted' ) ;
0 commit comments