Skip to content

Commit

Permalink
Emebed JS: fix compatibility with sphinx 6.x (jquery removal)
Browse files Browse the repository at this point in the history
  • Loading branch information
stsewd committed Jun 21, 2022
1 parent b525177 commit 422ae31
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
35 changes: 23 additions & 12 deletions readthedocs/core/static-src/core/js/doc-embed/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function attach_elastic_search_query_sphinx(data) {
var search_url = document.createElement('a');

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

/*
Expand All @@ -64,6 +64,21 @@ function attach_elastic_search_query_sphinx(data) {
}
};

var buildSection = function (title, link, content) {
var div_title = document.createElement("div");
var a_element = document.createElement("a");
a_element.href = link;
a_element.innerHTML = title;
div_title.appendChild(a_element);
html = div_title.outerHTML
for (var i = 0; i < content.length; i++) {
var div_content = document.createElement("div");
div_content.innerHTML = content[i];
html += div_content.outerHTML;
}
return html;
};

search_def
.then(function (data) {
var results = data.results || [];
Expand All @@ -80,7 +95,7 @@ function attach_elastic_search_query_sphinx(data) {
title = xss(result.highlights.title[0]);
}

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

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

Expand Down Expand Up @@ -126,7 +141,7 @@ function attach_elastic_search_query_sphinx(data) {
if (current_block.type === "section") {
var section = current_block;
var section_subtitle = section.title;
var section_subtitle_link = link + "#" + section.id;
var section_subtitle_link = xss(link + "#" + section.id);
var section_content = [section.content.substr(0, MAX_SUBSTRING_LIMIT) + " ..."];

if (section.highlights.title.length) {
Expand All @@ -145,15 +160,11 @@ function attach_elastic_search_query_sphinx(data) {
}
}

append_html_to_contents(
contents,
section_template,
{
section_subtitle_link: section_subtitle_link,
section_subtitle: section_subtitle,
section_content: section_content
}
);
contents.append(buildSection(
section_subtitle,
section_subtitle_link,
section_content
));
}

// if the result is a sphinx domain object
Expand Down
18 changes: 16 additions & 2 deletions readthedocs/core/static-src/core/js/readthedocs-doc-embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,24 @@ var search = require('./doc-embed/search');
}
}

domReady(function () {
// Inject JQuery if isn't present already.
function injectJQuery(fn) {
if (!window.jQuery) {
let script = document.createElement("script");
document.head.appendChild(script);
script.type = 'text/javascript';
script.src = "https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js";
script.onload = fn;
} else {
fn();
}
}

function init() {
footer.init();
sphinx.init();
search.init();
sponsorship.init();
});
}
domReady(injectJQuery(init));
}());
Loading

0 comments on commit 422ae31

Please sign in to comment.