npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bibtex-parse

v2.1.0

Published

bibtex parser for javascript

Downloads

479

Readme

bibtex-parse

Parse BibTeX to JSON.

npm install bibtex-parse

Example

const bibtexParse = require('bibtex-parse');
const fs = require('fs');
const bibtex = fs.readFileSync('example.bib', 'utf8');
bibtexParse.entries(bibtex);

example.bib:

@preamble{"Reference list"}
@string{ian = "Brown, Ian"}
@string{jane = "Woods, Jane"}
%references
@inproceedings{Smith2009,
  author=jane,
  year=2009,
  month=dec,
  title={{Quantum somethings}},
  journal={Journal of {B}lah}
}

@book{IP:1990,
author = ian # " and " # jane,
year = {1990},
title = {Methods for Research}
}

output:

[
  {
    "key": "Smith2009",
    "type": "inproceedings",
    "AUTHOR": "Woods, Jane",
    "YEAR": 2009,
    "MONTH": "December",
    "TITLE": "Quantum somethings",
    "JOURNAL": "Journal of Blah"
  },
  {
    "key": "IP:1990",
    "type": "book",
    "AUTHOR": "Brown, Ian and Woods, Jane",
    "YEAR": "1990",
    "TITLE": "Methods for Research"
  }
]

Note: fields are always output in capitals to distinguish from build in attributes (key and type).

Abstract Syntax Tree

To get the AST, use:

bibtexParse.parse(bibtex, options);

Where bibtex is a string representing bibtex to be parsed, and options an object with any of the following:

  • number: tells the parser how numbers should be returned.
    • number: "auto" (default): return a JavaScript number, unless over MAX_SAFE_INTEGER (9007199254740991), in which case a BigInt is returned.
    • number: "number": always return a JavaScript number, even if over MAX_SAFE_INTEGER (in which case the precision will be lost).
    • number: "bigint": always return a BigInt.
    • number: "string": return number as a string.

This will return an array of items. There are four types of item:

  • string: { itemtype: 'string', name, value, datatype, enclosed }
  • preamble: { itemtype: 'preamble', value, datatype, enclosed }
  • comment: { itemtype: 'comment', comment }
  • entry: { itemtype: 'entry', type, key, enclosed, fields },
    • type: e.g. article, inproceedings, book
    • key: the unique identifier for the entry
    • fields: e.g. title, year [{ name: 'year', value: 2001, datatype: 'number' }, ...]

Datatype can be one of the following:

  • number: an integer number
  • identifier: a string variable name
  • braced: a value which was enclosed in curly braces
  • quoted: a value which was enclosed in double quotes
  • concatinate: in which case the corresponding value property will be an array [{ datatype, value }, ...]
  • null: used when no value has been assigned, e.g. @string{a}
  • unenclosed: used when a preamble has not been properly enclosed (e.g @preamble{this isn't enclosed}).

Enclosed can be one of the following:

  • braces - when item is enclosed in curly braces (e.g. @string{a = 1})
  • parentheses - when item is enclosed in brackets (e.g. @string(a = 1))
[
  {
    "itemtype": "preamble",
    "enclosed": "braces",
    "value": "Reference list",
    "datatype": "quoted"
  },
  { "itemtype": "comment", "comment": "\n" },
  {
    "itemtype": "string",
    "name": "ian",
    "value": "Brown, Ian",
    "datatype": "quoted"
  },
  { "itemtype": "comment", "comment": "\n" },
  {
    "itemtype": "string",
    "name": "jane",
    "value": "Woods, Jane",
    "datatype": "quoted"
  },
  { "itemtype": "comment", "comment": "\n%references\n" },
  {
    "itemtype": "entry",
    "type": "inproceedings",
    "enclosed": "braces",
    "key": "Smith2009",
    "fields": [
      { "name": "author", "value": "jane", "datatype": "identifier" },
      { "name": "year", "value": 2009, "datatype": "number" },
      { "name": "month", "value": "dec", "datatype": "identifier" },
      {
        "name": "title",
        "value": "{Quantum somethings}",
        "datatype": "braced"
      },
      { "name": "journal", "value": "Journal of {B}lah", "datatype": "braced" }
    ]
  },
  { "itemtype": "comment", "comment": "\n\n" },
  {
    "itemtype": "entry",
    "type": "inproceedings",
    "enclosed": "braces",
    "key": "Smith2009",
    "fields": [
      { "name": "author", "value": "jane", "datatype": "identifier" },
      { "name": "year", "value": 2009, "datatype": "number" },
      { "name": "month", "value": "dec", "datatype": "identifier" },
      {
        "name": "title",
        "value": "{Quantum somethings}",
        "datatype": "braced"
      },
      { "name": "journal", "value": "Journal of {B}lah", "datatype": "braced" }
    ]
  }
]

Resources

  • A summary of BibTex - http://maverick.inria.fr/~Xavier.Decoret/resources/xdkbibtex/bibtex_summary.html
  • ORCID bibtex parse library - https://github.com/ORCID/bibtexParseJs