A while back I tweeted about making a simple Citation.js API Endpoint with RunKit.
Made a quick Citation.js API endpoint with @runkitdev : https://t.co/cdIc9bAEVX. Example https://t.co/GDSxaxTw4W
— Lars Willighagen (@larswillighagen) 23 juli 2017
Using the Express app helper and some type-checking code:
const express = require('@runkit/runkit/express-endpoint/1.0.0')
const app = express(exports)
const Cite = require('citation-js@0.3.0')
const docs = 'https://gist.github.com/larsgw/ada240ded0a78d5a6ee2a864fbcb8640'
const validStyle = style => ['bibtxt', 'bibtex', 'csl', 'citation'].includes(style) || /^citation-\w+$/.test(style)
const validType = type => ['string', 'html', 'json'].includes(type)
const last = array => array[array.length - 1]
const getOptions = params => {
const fragments = params.split('/')
let data, style = 'csl', type = 'html'
// parse fragments
// (got pretty complex, as data can contain '/'s)
return {data, style, type}
}
app
.get('/', (_, res) => res.redirect(docs))
.get('/*', ({params: {0: params}}, res) => {
const {data, style, type} = getOptions(params)
const cite = new Cite(data)
const output = cite.get({style, type})
res.send(output)
})
Full code here. Makes an API like this:
https://$CODE.runkit.sh/$DATA[/$STYLE[/$TYPE]]
Where
$CODE
is the API id (vf2453q1d6s5
in this case),$DATA
is the input data (DOI, Wikidata ID, or even a BibTeX string),$STYLE
(optional) is the output style,- and
$TYPE
(optional) is the output type (basically plain text vs html).
This makes it possible to link to a lot of Citation.js outputs:
- Bib.TXT output from DOI:
https://vf2453q1d6s5.runkit.sh/10.5281/zenodo.845934/bibtex
- Generated citation from Wikidata:
https://vf2453q1d6s5.runkit.sh/Q30000000/citation-apa
- BibTeX output from Wikidata:
https://vf2453q1d6s5.runkit.sh/Q30000000/bibtex
- CSL-JSON output from Wikidata:
https://vf2453q1d6s5.runkit.sh/Q30000000/csl
No comments:
Post a Comment