Skip to content

Commit ea67e59

Browse files
committed
Emebed JS: fix compatibility with sphinx 6.x (jquery removal)
1 parent b525177 commit ea67e59

File tree

3 files changed

+70
-73
lines changed

3 files changed

+70
-73
lines changed

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

Lines changed: 55 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,6 @@ var xss = require('xss/lib/index');
77
var MAX_RESULT_PER_SECTION = 3;
88
var MAX_SUBSTRING_LIMIT = 100;
99

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-
}
3710

3811
/*
3912
* Search query override for hitting our local API instead of the standard
@@ -50,7 +23,7 @@ function attach_elastic_search_query_sphinx(data) {
5023
var search_url = document.createElement('a');
5124

5225
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 +
5427
'&version=' + version + '&language=' + language;
5528

5629
/*
@@ -64,6 +37,48 @@ function attach_elastic_search_query_sphinx(data) {
6437
}
6538
};
6639

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+
6782
search_def
6883
.then(function (data) {
6984
var results = data.results || [];
@@ -80,7 +95,7 @@ function attach_elastic_search_query_sphinx(data) {
8095
title = xss(result.highlights.title[0]);
8196
}
8297

83-
var link = result.path + "?highlight=" + $.urlencode(query);
98+
var link = result.path + "?highlight=" + encodeURIComponent(query);
8499

85100
var item = $('<a>', {'href': link});
86101

@@ -100,33 +115,11 @@ function attach_elastic_search_query_sphinx(data) {
100115

101116
var contents = $('<div class="context">');
102117

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-
125118
// if the result is page section
126119
if (current_block.type === "section") {
127120
var section = current_block;
128121
var section_subtitle = section.title;
129-
var section_subtitle_link = link + "#" + section.id;
122+
var section_subtitle_link = xss(link + "#" + section.id);
130123
var section_content = [section.content.substr(0, MAX_SUBSTRING_LIMIT) + " ..."];
131124

132125
if (section.highlights.title.length) {
@@ -145,15 +138,11 @@ function attach_elastic_search_query_sphinx(data) {
145138
}
146139
}
147140

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+
));
157146
}
158147

159148
// if the result is a sphinx domain object
@@ -178,15 +167,11 @@ function attach_elastic_search_query_sphinx(data) {
178167

179168
var domain_subtitle = "[" + domain_role_name + "]: " + domain_name;
180169

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+
));
190175
}
191176

192177
contents.find('span').addClass('highlighted');

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,22 @@ var search = require('./doc-embed/search');
1919
}
2020
}
2121

22-
domReady(function () {
22+
function init() {
2323
footer.init();
2424
sphinx.init();
2525
search.init();
2626
sponsorship.init();
27-
});
27+
}
28+
29+
// Inject JQuery if isn't present already.
30+
if (!window.jQuery) {
31+
console.log("JQuery not found. Injecting.");
32+
var script = document.createElement("script");
33+
script.type = 'text/javascript';
34+
script.src = "https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js";
35+
script.onload = function () { domReady(init); };
36+
document.head.appendChild(script);
37+
} else {
38+
domReady(init);
39+
}
2840
}());

0 commit comments

Comments
 (0)