Skip to content

Instantly share code, notes, and snippets.

@benjaminkreen
Last active June 19, 2019 20:45
Show Gist options
  • Save benjaminkreen/729021706d050e066832b386b8335ed8 to your computer and use it in GitHub Desktop.
Save benjaminkreen/729021706d050e066832b386b8335ed8 to your computer and use it in GitHub Desktop.
Similar Article code
let containerClass = 'related-articles-container';
let listId = 'related-articles-list';
let header = 'Similar PLOS Articles';
let params = new URLSearchParams(window.location.search);
let relatedDoi = params.get('id');
let allowedTypes = ['Research Article', 'Front Matter'];
// GTM doesn't support backtick notation, refactor
function relatedCallback (data) {
console.log(data)
let relatedDocs = data['moreLikeThis'][relatedDoi]['docs']
let listItems = relatedDocs.reduce(function(list, doc) {
let listItem = `<li><a href="https://dx.plos.org/${doc.id}">${doc.title}</a></li>`;
return allowedTypes.indexOf(doc.article_type) != -1 ? list + listItem : list;
}, '')
if (listItems == '') { return null; } // do nothing if no thingies
$(`<div class="${containerClass}"><h3>${header}</h3><ul id="${listId}">${listItems}</ul></div>`).insertAfter('a[data-target="crossmark"]')
}
if ($(`.${containerClass}`).length == 0) {
$.ajax({
url: `https://api.plos.org/search?wt=json&json.wrf=relatedCallback&fl=id,title,article_type&mlt.fl=abstract&mlt=true&q=id:${relatedDoi}` ,
type: 'GET',
dataType: 'jsonp'
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment