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

`Cite.plugins.output.config` is undefined #161

Closed jczerwinski closed 5 years ago

jczerwinski commented 5 years ago

I'm trying to import a new CSL style.

According to the docs (https://citation.js.org/api/tutorial-output_plugins_csl.html), we should be able to do:

let config = Cite.plugins.output.config.get('csl');
config.templates.add(citation_style_name, citation_stylesheet);

But Cite.plugins.output.config is undefined :(

Did the API change and the docs are just out of date? Or something else?

Cheers!

jczerwinski commented 5 years ago

For google's sake, the error message is:

TypeError: Cannot read property 'get' of undefined

jczerwinski commented 5 years ago

I'm using "citation-js": "^0.4.0-9"

jczerwinski commented 5 years ago

Looks like this is the new API:

Citation.CSL.register.addTemplate(citation_style_name, citation_stylesheet);
larsgw commented 5 years ago

No, that's the old API. The docs contain a typo, it should simply be Cite.plugins.config.get('csl'). Sorry for the confusion.

jczerwinski commented 5 years ago

No worries, and thanks for your help. I still can't seem to get it working, unfortunately.

Neither of the above methods for registering a style throw an error; however, when I try to format a citation or bibliography using said style, it doesn't seem to work, and just silently defaults back to apa. The built in styles do all work, though.

Can you confirm that this is the intended API workflow?

Cite.plugins.config.get('csl').templates.add('ieee', ieee_stylesheet);
let bibliography = new Cite();
bibliography.add(...);
bibliography.format('bibliography', {
    format: 'html',
    template: 'ieee'
});
larsgw commented 5 years ago

That should work. Only thing is that you can also pass items in the Cite constructor. As for the reason it doesn't work (I can't reproduce it so far), can you run this after adding ieee:

Object.keys(Cite.plugins.config.get('csl').templates.data)
jczerwinski commented 5 years ago
console.log(Object.keys(Cite.plugins.config.get('csl').templates.data))
> [ 'apa', 'vancouver', 'harvard1', 'ieee' ]

Still not working though.

I'm going to keep digging. It looks like it might have something to do with engines.js grabbing a fresh copy of the style registry, rather than using the updated one?

jczerwinski commented 5 years ago

Or alternatively, bibliography.js grabbing a new engine, rather than the existing one?

jczerwinski commented 5 years ago

Nevermind, I think it's on me. Async things happening in unexpected order in my code. Thanks!