-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcitations.js
More file actions
26 lines (25 loc) · 866 Bytes
/
citations.js
File metadata and controls
26 lines (25 loc) · 866 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
async function fetchCitation(doi, style) {
const url = `https://doi.org/${doi}`;
const headers = {
'Accept': style === 'bibtex' ? 'application/x-bibtex' : `text/x-bibliography; style=${style}`
};
const response = await fetch(url, { headers });
return response.text();
}
function renderCitation(doi, style, elementId) {
fetchCitation(doi, style).then(citation => {
const container = document.getElementById(elementId);
if (style === 'bibtex') {
const pre = document.createElement('pre');
pre.textContent = citation.trim()
.replace(/,\s(\w+\s*=\s*)/g, ',\n $1')
.replace(/\s+}$/g, '\n}');
container.appendChild(pre);
} else {
const link = document.createElement('a');
link.href = `https://doi.org/${doi}`;
link.innerHTML = citation;
container.appendChild(link);
}
});
}