larsgw / citation.js

Citation.js converts formats like BibTeX, Wikidata JSON and ContentMine JSON to CSL-JSON to convert to other formats like APA, Vancouver and back to BibTeX.
https://citation.js.org/
MIT License
226 stars 30 forks source link

JSON parsing error for rendered output #142

Closed rmzelle closed 6 years ago

rmzelle commented 6 years ago

I'm having trouble using citation.js to convert CSL JSON to rendered citations. Converting to bibtex works as expected.

Based on https://runkit.com/larsgw/591b5651bd9b40001113931c, I get the following results:

const Cite = require("citation-js")

const data = new Cite([{"id":78,"type":"article-journal","title":"Identification of Positive Regulators of the Yeast Fps1 Glycerol Channel","container-title":"PLoS Genet","page":"e1000738","volume":"5","issue":"11","source":"PLoS Genet","journalAbbreviation":"PLoS Genet","author":[{"family":"Beese","given":"Sara E."},{"family":"Negishi","given":"Takahiro"},{"family":"Levin","given":"David E."}]}])

data.get({
  type: 'html',
  style: 'bibtex'
})

gives

@article{BeeseIdentification,

    author={Sara E. Beese and Takahiro Negishi and David E. Levin},
    journal={PLoS Genet},
    issue=11,
    pages={e1000738},
    title={{Identification of Positive Regulators of the Yeast Fps1 Glycerol Channel}},
    volume=5,

}

In contrast,

const Cite = require("citation-js")

const data = new Cite([{"id":78,"type":"article-journal","title":"Identification of Positive Regulators of the Yeast Fps1 Glycerol Channel","container-title":"PLoS Genet","page":"e1000738","volume":"5","issue":"11","source":"PLoS Genet","journalAbbreviation":"PLoS Genet","author":[{"family":"Beese","given":"Sara E."},{"family":"Negishi","given":"Takahiro"},{"family":"Levin","given":"David E."}]}])

data.get({
  type: 'html',
  style: 'citation-apa'
})

gives the error

SyntaxError: Unexpected token u in JSON at position 0

(see https://runkit.com/5af4a2c58a83410012ddbf47/5af4a2d65bba2900126994f2#)

If I use new Cite('Q21972834') instead of using raw CSL-JSON as input both work. Any idea what's going on here?

larsgw commented 6 years ago

I'm not entirely sure what the problem is, but it seems to be fixed in the v0.4.0-1 beta (which I see doesn't have a bundle yet). If you're okay with using this version (it has some new APIs, but should be backwards compatible), I can add the bundles to the archive branch. Alternatively, they are also available on the site:

larsgw commented 6 years ago

Okay, it seems to be caused by the ID being a number, while citeproc-js casts it to a string. Because of that, the citeproc-js engine gets undefined, which it doesn't handle particularly graceful. In the new versions of citation-js, strings and other types of IDs are always cast to a string.

I could backport a fix if needed.

rmzelle commented 6 years ago

Thanks for sorting this out!

No need to backport this for me. I probably want to be on 0.4.x anyway for the (experimental) RIS support.

I prefer to use a tagged release URL (to the minimized library), so it would be handy to have a 0.4.x release.

On Fri, May 11, 2018 at 07:20 Lars Willighagen notifications@github.com wrote:

Okay, it seems to be caused by the ID being a number, while citeproc-js casts it to a string, and then gets undefined, which it doesn't handle particularly graceful. In the new versions of citation-js, strings and other types of IDs are always cast to a string (which wasn't really my intention, as far as I can recall).

I could backport a fix if needed.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/larsgw/citation.js/issues/142#issuecomment-388377963, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEwf8cCVcA39eX3ZboNfwAn_j3muYzSks5txZ4wgaJpZM4T6fPS .

larsgw commented 6 years ago

I released the v0.4.0-3 beta with RIS support, the CDN should load up soon:

larsgw commented 6 years ago

Here's a demo on how to use the RIS output BTW: https://runkit.com/larsgw/citation.js-ris-output-demo

Basically this:

Cite(...).get({
  format: 'string',
  type: 'string',
  style: 'ris'
})

Or, with the new API:

Cite(...).format('ris')

Additionally, you can pass parameters:

Cite(...).format('ris', {
  type: 'text' // alternative: 'object' gives you an object representation
})