Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First stab at adding support for the BibTeX format #94

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions formats/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
- [YAML](./yaml/)
- [CSV](./csv/)
- [TOML](./toml/)
- [BibTeX](./bibtex/)
8 changes: 8 additions & 0 deletions formats/bibtex/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# BibTeX Format

Uses the [`biblatex-csl-converter`](https://www.npmjs.com/package/biblatex-csl-converter) npm package.

See the [supported options](https://github.com/fiduswriter/biblatex-csl-converter/blob/1a9c5903e7bdf517bcc61e0d61b8befbea91f21e/src/import/biblatex.ts#L33-L110) for low-level usage.

Known issues:
- doesn't preserve the formatting in the source `.bib` file on saving (see https://github.com/fiduswriter/biblatex-csl-converter/issues/132)
22 changes: 22 additions & 0 deletions formats/bibtex/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Format from "../../src/format.js";
import { BibLatexParser, BibLatexExporter } from "https://esm.sh/biblatex-csl-converter/lib/index.js";

export default class BibTeX extends Format {
static defaultOptions = {
processUnexpected: true,
processUnknown: true,
Copy link
Contributor

@LeaVerou LeaVerou Jul 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we’d want to expose these options wholesale. We may want to expose the functionality, but with a different API. I’d not expose for now — once you expose, you need to support that forever.

Remember ideally we should be able to swap out the parser/serializer for a different package and our end-users shouldn't have to change much.

};

static extensions = ["bib"];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s the most popular one, but pretty sure there are a few more extensions in use.

static mimeTypes = ["text/plain"];

static parse (str, options) {
options = this.resolveOptions(options, "parse");
return new BibLatexParser(str, options).parse();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC this does not produce a useful format by itself, though it preserves duplicate keys which could be useful for some use cases like reference managers.
We should probably have some sort of option to get back an object of ids to metadata. And something to transform to CSL, though I'm not sure what that entails exactly. But since the functionality is there, we could provide an option.

}

static stringify (obj, options) {
options = this.resolveOptions(options, "stringify");
return new BibLatexExporter(obj.entries, false, options).parse();
}
}
2 changes: 2 additions & 0 deletions formats/index-fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export {default as Text} from "./text/index.js";
export {default as CSV} from "./csv/index.js";
export {default as Yaml} from "./yaml/index.js";
export {default as Toml} from "./toml/index.js";
export {default as BibTeX} from "./bibtex/index.js";
export {default as bibtex} from "./bibtex/index.js";