Skip to content

Commit 422ae31

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

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

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

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function attach_elastic_search_query_sphinx(data) {
5050
var search_url = document.createElement('a');
5151

5252
search_url.href = data.proxied_api_host + '/api/v2/search/';
53-
search_url.search = '?q=' + $.urlencode(query) + '&project=' + project +
53+
search_url.search = '?q=' + encodeURIComponent(query) + '&project=' + project +
5454
'&version=' + version + '&language=' + language;
5555

5656
/*
@@ -64,6 +64,21 @@ function attach_elastic_search_query_sphinx(data) {
6464
}
6565
};
6666

67+
var buildSection = function (title, link, content) {
68+
var div_title = document.createElement("div");
69+
var a_element = document.createElement("a");
70+
a_element.href = link;
71+
a_element.innerHTML = title;
72+
div_title.appendChild(a_element);
73+
html = div_title.outerHTML
74+
for (var i = 0; i < content.length; i++) {
75+
var div_content = document.createElement("div");
76+
div_content.innerHTML = content[i];
77+
html += div_content.outerHTML;
78+
}
79+
return html;
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

@@ -126,7 +141,7 @@ function attach_elastic_search_query_sphinx(data) {
126141
if (current_block.type === "section") {
127142
var section = current_block;
128143
var section_subtitle = section.title;
129-
var section_subtitle_link = link + "#" + section.id;
144+
var section_subtitle_link = xss(link + "#" + section.id);
130145
var section_content = [section.content.substr(0, MAX_SUBSTRING_LIMIT) + " ..."];
131146

132147
if (section.highlights.title.length) {
@@ -145,15 +160,11 @@ function attach_elastic_search_query_sphinx(data) {
145160
}
146161
}
147162

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-
);
163+
contents.append(buildSection(
164+
section_subtitle,
165+
section_subtitle_link,
166+
section_content
167+
));
157168
}
158169

159170
// if the result is a sphinx domain object

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

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

22-
domReady(function () {
22+
// Inject JQuery if isn't present already.
23+
function injectJQuery(fn) {
24+
if (!window.jQuery) {
25+
let script = document.createElement("script");
26+
document.head.appendChild(script);
27+
script.type = 'text/javascript';
28+
script.src = "https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js";
29+
script.onload = fn;
30+
} else {
31+
fn();
32+
}
33+
}
34+
35+
function init() {
2336
footer.init();
2437
sphinx.init();
2538
search.init();
2639
sponsorship.init();
27-
});
40+
}
41+
domReady(injectJQuery(init));
2842
}());

0 commit comments

Comments
 (0)