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

Use try...catch block with underscore.js template. #5984

Merged
merged 6 commits into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
61 changes: 43 additions & 18 deletions readthedocs/core/static-src/core/js/doc-embed/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,33 @@ 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 Down Expand Up @@ -129,15 +156,14 @@ function attach_elastic_search_query(data) {
}
}

contents.append(
$u.template(
section_template,
{
section_subtitle_link: section_subtitle_link,
section_subtitle: section_subtitle,
section_content: section_content
}
)
append_html_to_contents(
contents,
section_template,
{
section_subtitle_link: section_subtitle_link,
section_subtitle: section_subtitle,
section_content: section_content
}
);
}

Expand Down Expand Up @@ -167,15 +193,14 @@ function attach_elastic_search_query(data) {
// domain_content = type_display -- name -- in doc_display
domain_content = domain._source.type_display + " -- " + domain_name + " -- in " + domain._source.doc_display;

contents.append(
$u.template(
domain_template,
{
domain_subtitle_link: domain_subtitle_link,
domain_subtitle: domain_subtitle,
domain_content: domain_content
}
)
append_html_to_contents(
contents,
domain_template,
{
domain_subtitle_link: domain_subtitle_link,
domain_subtitle: domain_subtitle,
domain_content: domain_content
}
);
}

Expand Down
Loading