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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import Cite from 'npm:citation-js';
import 'npm:@citation-js/plugin-bibtex'
// https://www.crossref.org/blog/dois-and-matching-regular-expressions/
const DOI_REGEX = /\b(10\.\d{4,9}\/[-._;()\/:\w]+)\b/i;
export const extractDOI = (content) => {
if (!content) return null;
if (!content.match) return null;
const doiRegex = DOI_REGEX;
const doiMatch = content.match(doiRegex);
let doi = doiMatch ? doiMatch[0] : null;
if (doi) {
doi = doi.replace(/^doi[:\/]/i, '');
// Remove version number (v1, v2, etc.) at the end of the DOI
doi = doi.replace(/v\d+$/, '');
}
return doi;
};
export async function getCitation(citationId: string, type = 'bibtex', opts) {
citationId = extractDOI(citationId);
console.log('citationId', citationId)
let example = await Cite.async(citationId);
let output;
if (type == 'json') {
// return json data
output = example.format('data', {
format: 'object',
...opts
});
} else if (type == 'html') {
// return string biblio
output = example.format('bibliography', {
format: 'html',
template: 'apa',
...opts
});
} else if (type == 'bibliography') {
// return string biblio
output = example.format('bibliography', {
format: 'text',
template: 'apa',
...opts
});
} else if (type == 'bibtex') {
// return string biblio
output = example.format('bibtex', {
template: 'apa',
...opts
});
}
else {
output = example.format('citation', {
format: 'html',
template: 'apa',
...opts
});
}
return output;
}
/*
citation js docs suck
- to get JSON output, set 'data' as the type and format: 'object'
- to get string output, set 'bibliograph' as the type and format: 'string'
- if you get "format error" it means the format of the input, NOT the output template
*/
// Example usage
// const citationId = 'https://journals.asm.org/doi/10.1128/aem.00807-24'; // extracts ctation
// const citationId = 'https://journals.asm.org/doi/10.1128/iai.00065-23'; // extracts ctation
// const citationId = '10.1128/aem.00807-24'; // need to look like this
// const citationId = '10.1016/j.xpro.2024.102949'; // need to look like this
// const citationId = 'https://www.biorxiv.org/content/10.1101/2024.07.24.604748v1'; // annoying biorxiv things
// const citationId = 'https://www.biorxiv.org/content/10.1101/2024.01.25.577194v4'; // annoying biorxiv things
// const citationId = '10.1101/2024.07.24.604748'; // extracts ctation
// getCitation(citationId, 'json').then(output => console.log(output));
// getCitation(citationId, 'html').then(output => console.log(output));
// getCitation(citationId, 'bibtex').then(output => console.log(output));
// getCitation(citationId, 'citation').then(output => console.log(output));
// getCitation(citationId, 'bibliography').then(output => console.log(output));
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
July 26, 2024