larsgw/citation.js

How to properly format a JSON source to parse a book?

Closed this issue · 2 comments

Current situation
Attempting to follow the instructions of the demo provides no guidance as of what is the expected JSON data structure for a book.

Attempted
This data was used (JSON object is inline in a JS file, which explains its quotes):

const toFormat = [
  {
    type: 'book',
    title: 'Aesthetics of Interaction in Digital Art',
    author: [
      {
        given: 'Katja',
        family: 'Kwastek',
      }
    ],
    issued: [
      {
        'date-parts': ['2013']
      }
    ],
    publisher: {
      name: 'MIT Press',
      city: 'Cambridge',
      state: 'MA',
      country: 'USA',
    },
  }
]

Then, parsing the data was done like this:

parseAsync(toFormat)
  .then(response => {
    const formatted = cite
      .set(response)
      .get({
        format: 'string',
        type: 'html',
        style: 'apa',
        lang: 'en-US',
      });

    console.log('formatted:', formatted);
  });

This provides this response:

<div class="csl-bib-body">
  <div data-csl-entry-id="temp_id_18548734920642862" class="csl-entry">Kwastek, K. (2013). <i>Aesthetics of Interaction in Digital Art</i>.</div>
</div>

Expected results

  • The publisher is missing from the response. Shouldn't the response in APA be in this format?

Author, A. A. (Year of publication). Title of work: Capital letter also for subtitle. Publisher Name. DOI (if available)

  • How would one know what is the data structure that this method expects?
  • Are APA (citation-apa), Vancouver (citation-vancouver), and Harvard (citation-harvard1) the only styles accepted? I tried with MLA (citation-mla) and Chicago (citation-chicago) and got nothing different than what is shown above.
    How would one know what are the accepted values?

Sorry, I have not updated the demo in a while. It shows both outdated code and is not very clear. The JSON format there is CSL-JSON, described here. In that format, it would look like this:

const toFormat = [
  {
    type: 'book',
    title: 'Aesthetics of Interaction in Digital Art',
    author: [
      {
        given: 'Katja',
        family: 'Kwastek',
      }
    ],
    // Note for 'issued': the order of the outer two brackets changed, from
    // [ { date-parts: [] } ] to { date-parts: [ [] ] }
    issued: {
      'date-parts': [['2013']]
    },
    // The publisher would be split into 'publisher' and 'publisher-place'. Unfortunately,
    // CSL-JSON has no way of specifying whether the place is a city etc
    publisher: 'MIT Press',
    'publisher-place': 'Cambridge, MA, USA'
  }
]

Are APA (citation-apa), Vancouver (citation-vancouver), and Harvard (citation-harvard1) the only styles accepted? I tried with MLA (citation-mla) and Chicago (citation-chicago) and got nothing different than what is shown above.
How would one know what are the accepted values?

The accepted values are listed in the documentation, on this page. To add different styles, you can use the following:

const Cite = require('citation-js')

const styleName = 'mla'
const styleConfig = Cite.plugins.config.get('@csl')

const styleData = Cite.util.fetchFile('https://zotero.org/styles/' + styleName)
styleConfig.templates.add(styleName, styleData)

Then you could format like so:

const cite = new Cite(toFormat)

cite.get({
  type: 'html',
  style: 'citation-mla'
})

Or, with the recent API:

const cite = new Cite(toFormat)

cite.format('bibliography', {
  format: 'html',
  template: 'mla'
})

Try it online!

Thanks @larsgw that was extremely helpful!