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
219 stars 30 forks source link

bibtex file not read correctly #198

Closed anyuzx closed 3 years ago

anyuzx commented 4 years ago

Hello, I am trying to parse a bibtex file, it seems the result CSL-JSON miss many fields in the bibtex entry.

Here is the test.bib file

@article{Thirumalai2017,
    Author = {D. Thirumalai and Guang Shi},
    Date-Added = {2020-05-25 16:29:18 -0500},
    Date-Modified = {2020-05-25 16:29:18 -0500},
    Doi = {10.1016/j.bpj.2016.11.902},
    Journal = {Biophysical Journal},
    Month = feb,
    Number = {3},
    Pages = {411--412},
    Publisher = {Elsevier {BV}},
    Title = {Chromatin Is Stretched but Intact When the Nucleus Is Squeezed through Constrictions},
    Url = {https://doi.org/10.1016/j.bpj.2016.11.902},
    Volume = {112},
    Year = {2017},
    Bdsk-Url-1 = {https://doi.org/10.1016/j.bpj.2016.11.902}}

I tried to read it like this,

const fs = require('fs');
const citejs = require('citation-js');
const bibfile = fs.readFileSync('./test.bib', 'utf8');

let citations = new citejs(bibfile)
console.log(citations.data)

The output is,

[
  {
    type: 'article-journal',
    'citation-label': 'Thirumalai2017',
    id: 'Thirumalai2017',
    _graph: [ [Object], [Object], [Object], [Object] ]
  }
]

The title, authors, etc are missing from the result. Thanks.

larsgw commented 4 years ago

Sorry, that is an issue with my BibTeX parser. This version does not recognize fields that are not lowercase yet. I am working on a new version, and I think I might take some time in the next two weeks to finish it.

larsgw commented 4 years ago

Alternatively, you can add this snippet (once, when importing citation-js) to proxy the BibTeX parser to fix it:

citejs.plugins.input.addDataParser('@bibtex/object', {
  async: false,
  parser (data) {
    for (const prop in data.properties) {
      data.properties[prop.toLowerCase()] = data.properties[prop]
      delete data.properties[prop]
    }
    return citejs.parse.bibtex.json(data)
  }
})

DEMO

anyuzx commented 4 years ago

@larsgw thanks for the snippet. This works well.

larsgw commented 3 years ago

This issue should be fixed in v0.5.0-alpha.10. If not, please re-open the issue.