@retorquere/bibtex-parser

中文文档
13.0.11发布于 2026/11/02ISCTypeScriptES Module构建来源已验证

BibTeX parser

原始使用文档
文档目录6 节
  1. What is BibTeX?
  2. Choosing a Parser
  3. Zotero Plugins
  4. URL Commands
  5. Unknown TeX Commands
  6. TypeScript Breaking Changes Since v11

bibtex-parser

A node/npm package for parsing bibtex (.bib) files. This is the parser that drives Better BibTeX for Zotero bib(la)tex imports.

While parsing, the parser will apply markup for commands such as \textbf, \textsc, etc, and braces wich protect text against case-changes. Commands will be replaced with their unicode text equivalents where possible.

What is BibTeX?

BibTeX is a reference management program that makes it easy for users of TeX and LaTeX to generate bibliographies for books and journal articles. When possible, it is usually stylized in the TeX font the way TeX and LaTeX are. LaTeX was first created in 1985 by Leslie Lamport and Oren Patashnik.

Documentation is available at https://retorque.re/bibtex-parser/.

Choosing a Parser

The Rust/WebAssembly implementation is the default export (ESM only, see below). The pure TypeScript implementation, which also needs no initialization, is available as an ESM-only /js subpath:

import { parse } from '@retorquere/bibtex-parser/js'

const library = parse('@article{example, title={A title}}')

For the scanner-level representation, use parseVerbatim. Its fields are strings and have not been normalized or interpreted as LaTeX:

import { parseVerbatim } from '@retorquere/bibtex-parser/js'

const library = parseVerbatim('@article{example, title={A title}}')

The generated API documentation covers the normalized TypeScript API (/js), its parseVerbatim export, and the WASM exports available from the default entry point.

All parser results expose preambles: string[], retaining multiple @preamble directives in source order. The normalized result property was previously named preamble; callers should use preambles instead.

The WebAssembly implementation is the ESM-only default entry point. Initialize it before calling the parser. In a browser, init() loads the binary relative to the module:

import init, { parse } from '@retorquere/bibtex-parser'

await init()
const library = parse('@article{example, title={A title}}')

In Node.js, load the exported binary explicitly:

import { readFile } from 'node:fs/promises'
import init, { parse } from '@retorquere/bibtex-parser'

const binary = new URL(import.meta.resolve('@retorquere/bibtex-parser/bibtex_parser_bg.wasm'))
await init({ module_or_path: await readFile(binary) })
const library = parse('@article{example, title={A title}}')

Both entry points include TypeScript declarations. Building the package from source also builds WebAssembly and requires Rust with the wasm32-unknown-unknown target and wasm-pack; consumers do not need these tools.

Zotero Plugins

Include the default JavaScript entry point in your plugin's build and package the WASM binary as a plugin resource. In the plugin's privileged Zotero context, initialize the parser during startup, before any parsing calls:

import init, { parse } from '@retorquere/bibtex-parser'

await init({ module_or_path: 'chrome://zotero-better-bibtex/content/resource/bibtex/bibtex_parser_bg.wasm' })

const library = parse('@article{example, title={A title}}')

Await initialization once during startup; subsequent parse or parseAsync calls reuse the initialized module.

URL Commands

Both parsers accept richURL, a boolean that defaults to true. With richURL: true, \href{http://example.org/{id}}{url} renders as <a href="http://example.org/{id}">url</a> without case-protection spans. With richURL: false, it renders as <span class="nocase">http://example.org/{id}</span>, discarding the label and preserving the destination's casing regardless of the caseProtection setting. The option also applies to \url.

Unknown TeX Commands

Both parse and parseAsync accept an unsupported option:

  • ignore: replace unknown commands with nothing.
  • keep (default): preserve literal TeX, including the backslash, and record a diagnostic in errors.
  • throw: throw an error identifying the unsupported TeX command (parse throws and parseAsync rejects). This bypasses the normal errors collection.

These policies also apply to unsupported environments. Custom handler functions are no longer supported.

TypeScript Breaking Changes Since v11

  • Options.unsupported no longer accepts a callback (node, tex, entry) => string for custom replacement text. Use ignore, keep, or throw. The default remains keep, matching master; passing any other value now throws a TypeError during parser setup.
  • The new unsupported: 'throw' mode propagates unsupported-command/environment errors out of parse (or rejects parseAsync) instead of adding them to the entry's errors and continuing.
  • Merged keywords now retain encounter order and exact duplicate strings. Previously, exact duplicates were removed and the remaining values were sorted case-insensitively. Case variants were not deduplicated before either.

Migrating from 8.x to 9.0

  • Move sentenceCase.langids to the top-level english option. It still accepts a language array or a boolean; include '' in a custom array to match entries with no langid.
  • Move sentenceCase.language to the top-level languageAsLangid option. It still controls whether the language field is used as a fallback when langid and hyphenation are absent.

The Entry and ParseError shapes, including the verbatim parser's entry shape, did not change between 8.0.23 and 9.0.0. Bundled unabbreviations also remain enabled by default.

版本与发布记录

@retorquere/bibtex-parser 共发布 290 个版本,首次发布于 2019/08/17;过去 12 个月发布了 29 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
13.0.11latest2026/11/021 天前
13.0.102026/11/021 天前
13.0.92026/11/011 天前
13.0.82026/11/011 天前
13.0.72026/11/012 天前
13.0.62026/11/012 天前

关于 @retorquere/bibtex-parser 的常见问题

@retorquere/bibtex-parser 是什么?

助理型解析器

如何安装 @retorquere/bibtex-parser?

在项目目录运行 npm install @retorquere/bibtex-parser,或使用 pnpm add / yarn add / bun add。

@retorquere/bibtex-parser 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@retorquere/bibtex-parser 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@retorquere/bibtex-parser 可以商用吗?

@retorquere/bibtex-parser 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@retorquere/bibtex-parser 还在维护吗?

最新版本 13.0.11 发布于 2026/11/02(1 天前);至今共发布 290 个版本,其中过去 12 个月发布了 29 个;首次发布于 2019/08/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。