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

Export only a single ID from the database #101

Closed languitar closed 10 months ago

languitar commented 6 years ago

I can't find any documentation how to do that: But, similar to the workflow with bibtex, I would like to define my data one, with multiple entries, and generate HTML at different places in my document for individual entries. Is this possible?

larsgw commented 6 years ago

This is not possible with a single Cite instance at the moment. You could possibly do something like this: (can't test right now)

const entries = new Cite(bibtex)

for (let entry of entries) {
  const html = (new Cite(entry)).get(options)
  // add html to document
}

I am planning to improve the output of Cite, including the CSL part, because the current output options scheme doesn't lend itself to operations like this in my opinion.

jczerwinski commented 5 years ago

I'm running into this issue as well, for exactly the same use case.

Could you maybe provide a few hints about how I might get started with this and I'll see if I can contribute?

larsgw commented 5 years ago

Hi @jczerwinski, thanks for your interest in contributing! I'm in the process of moving a lot of the code to a lerna repo at citation-js/citation-js; this GitHub repository and the npm package will remain for backwards compatibility, as a preset of plugins, and for browser builds. Therefore contributions won't be incorporated at the moment (but will later).

Here's my ideas on how to solve this:

Exporting one or a few entries could be handled either when exporting, or earlier in a separate, manual stage. If it's done at the exporting level, there either need to be general options (2) or the option needs to be re-implemented in each output format (1). Since the idea is that those formats are extendable now, that may not be the best way to go. A Cite instance method taking an ID or a list of IDs and returning a new Cite instance with only those entries would be the easiest, I think (3).

  1. Output format code is in the following packages:

  2. General options would be handled in Cite/get.js. If you want to change that, we need to discuss more, because there would be some breaking changes.

  3. A new Cite method like subset/filter would be a new file in this directory.

Which option do you think would be best? Or do you have a different idea?

jczerwinski commented 5 years ago

Maybe I'll specify the way I see the use case first, because I actually think that it's already pretty well supported.

Suppose we already have a Cite instance loaded up with a bunch of entries. Let's think of this instance as a bibliography:

let bibliography = new Cite();
// Load it up...
references.forEach(ref => bibliography.add(ref));

It's pretty easy to output the entire bibliography:

bibliography.format('bibliography');

It also often works to output a single (or subset of) citation(s) from the bibliography:

bibliography.format('citation', {
  entry: ['1']
});
// Prints '[1]'

The only problem I've noticed is when the citation style you're using uses citation numbers in its citation format:

bibliography.format('citation', {
  template: 'ieee',
  entry: ['3']
});
// This prints '[1]' instead of '[3]'

Basically, the citation number/order is lost.

Unless I'm missing something, other than this, exporting single citations seems to work pretty good, at least for citeproc generated output.

A couple of tweaks to [get/modules/csl/citation.js] seems to fix this without breaking anything. I just submitted a PR to this effect. Feedback is welcome!

Brand2 commented 3 years ago

Hi @larsgw, was wondering if you had made any progress on this or know if there is a workaround for the issue where if you were to do say:

const bib = new Cite()
refs.forEach((v) => bib.add(v))
bib.format('citation', {template: 'vancouver', entry: ['2']})
// Prints (1) instead of (2)

It results in the wrong number? This seems to be consistent for numbered styles. I realise I could probably do a workaround where I do a regex replace with the number I know it should be (or something similar), but that's obviously not ideal.

larsgw commented 3 years ago

I haven't implemented it for anything other than in-text citations yet. In your case the problem is that using entry removes all the context, in this case the fact that there's another reference that comes before it. I think I can work on it later this week.

larsgw commented 10 months ago

entry now works for bibliography output as well. It also has the asEntryArray option which returns an array of id-ref pairs.