Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embed JS: fix incompatibilities with sphinx 6.x (jquery removal) #9359

Merged
merged 7 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 76 additions & 69 deletions readthedocs/core/static-src/core/js/doc-embed/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,6 @@ var xss = require('xss/lib/index');
var MAX_RESULT_PER_SECTION = 3;
var MAX_SUBSTRING_LIMIT = 100;

/**
* Use try...catch block to append html to contents
*
* @param {Object} contents html element on which additional html is be appended
* @param {String} template underscore.js template string
* @param {Object} data template vars and their values
*/
function append_html_to_contents(contents, template, data) {
// underscore.js throws variable not defined error
// because of change of syntax in new versions.
// See: https://stackoverflow.com/a/25881231/8601393
try {
// this is the pre-1.7 syntax from Underscore.js
contents.append(
$u.template(
template,
data
)
);
}
catch (error) {
// this is the new syntax
contents.append(
$u.template(template)(data)
);
}
}

/*
* Search query override for hitting our local API instead of the standard
Expand All @@ -50,7 +23,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 +37,70 @@ function attach_elastic_search_query_sphinx(data) {
}
};

/**
* Build a section with its matching results.
*
* A section has the form:
*
* <div>
* <a href={link}>{title}<a>
* </div>
* <div>
* {contents[0]}
* </div>
* <div>
* {contents[1]}
* </div>
*
* ...
*
* @param {String} title.
* @param {String} link.
* @param {Array} contents.
*/
var buildSection = function (title, link, contents) {
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 < contents.length; i += 1) {
var div_content = document.createElement("div");
div_content.innerHTML = contents[i];
html += div_content.outerHTML;
}
return html;
};

/**
* Build a domain section.
* A domain section has the form:
*
* <div>
* <a href={link}>{title}<a>
* </div>
* <div>
* {content}
* </div>
*
* @param {String} title.
* @param {String} link.
* @param {String} content.
*/
var buildDomain = 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);

var div_content = document.createElement("div");
div_content.innerHTML = content;

return div_title.outerHTML + div_content.outerHTML;
};

search_def
.then(function (data) {
var results = data.results || [];
Expand All @@ -80,7 +117,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 All @@ -100,28 +137,6 @@ function attach_elastic_search_query_sphinx(data) {

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

var section_template =
'<div>' +
'<a href="<%= section_subtitle_link %>">' +
'<%= section_subtitle %>' +
'</a>' +
'</div>' +
'<% for (var i = 0; i < section_content.length; ++i) { %>' +
'<div>' +
'<%= section_content[i] %>' +
'</div>' +
'<% } %>';

var domain_template =
'<div>' +
'<a href="<%= domain_subtitle_link %>">' +
'<%= domain_subtitle %>' +
'</a>' +
'</div>' +
'<div>' +
'<%= domain_content %>' +
'</div>';

// if the result is page section
if (current_block.type === "section") {
var section = current_block;
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 All @@ -178,15 +189,11 @@ function attach_elastic_search_query_sphinx(data) {

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

append_html_to_contents(
contents,
domain_template,
{
domain_subtitle_link: domain_subtitle_link,
domain_subtitle: domain_subtitle,
domain_content: domain_content
}
);
contents.append(buildDomain(
domain_subtitle,
domain_subtitle_link,
domain_content
));
}

contents.find('span').addClass('highlighted');
Expand Down
23 changes: 20 additions & 3 deletions readthedocs/core/static-src/core/js/readthedocs-doc-embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ var sphinx = require('./doc-embed/sphinx');
var search = require('./doc-embed/search');


// While much of this script relies on jQuery (which Sphinx relies on),
// we purposefully do not use it before DOMContentLoaded in case scripts are loaded out of order
// Parts of this script rely on jQuery,
// since Sphinx no longer includes it,
// we must inject it if isn't found before executing our script.
(function () {
function domReady(fn) {
// If the DOM is already done parsing
Expand All @@ -19,10 +20,26 @@ var search = require('./doc-embed/search');
}
}

domReady(function () {
function init() {
stsewd marked this conversation as resolved.
Show resolved Hide resolved
footer.init();
sphinx.init();
search.init();
sponsorship.init();
}

domReady(function () {
// Inject JQuery if isn't present already.
if (!window.jQuery) {
console.log("JQuery not found. Injecting.");
stsewd marked this conversation as resolved.
Show resolved Hide resolved
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://code.jquery.com/jquery-3.6.0.min.js";
script.integrity = "sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==";
script.crossOrigin = "anonymous";
script.onload = init;
document.head.appendChild(script);
} else {
init();
}
});
}());
Loading