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

exception when trying to add locale #216

Closed eroux closed 3 years ago

eroux commented 3 years ago

First, thanks a lot for this very needed library!

When I add the zh_CN locale from an xml file, I'm getting citeproc-js error: unable to parse XML input (in both latest Chrome and Firefox). I made a self contained demo of the bug on http://eroux.fr/demobugzh.html (the error appears in the console)

here is the code for reference:

 <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>title</title>
     <script src="https://cdn.jsdelivr.net/npm/citation-js"></script> 
     <script>

      async function main() {

        async function getXml() {
          return fetch('https://raw.githubusercontent.com/citation-style-language/locales/master/locales-zh-CN.xml')
            .then(response => response.text());
          }
        const zh_CN_xml = await getXml();
        console.log(zh_CN_xml)  

        const Cite = require('citation-js')

        let config = Cite.plugins.config.get('@csl')
        config.locales.add("zh_CN", zh_CN_xml)

        const examplecsl = {
          "id":"bdr:MW23819",
          "type":"book",
          "source": "Buddhist Digital Resource Center (BDRC)",
          "url": "http://purl.bdrc.io/resource/MW23819",
          "accessed": {
               "date-parts": [[ 2021, 6, 18 ]]
          },
          "number-of-volumes": 4,
          "author": [
             {
                     "family": "Bstan ʼdzin rnam rgyal rab dgaʼ 2",
             }
          ],
          "title": "Bkaʼ brgyad gsan ba yon rdzogs",
          "publisher": "Ngodrup and sherab drimay",
          "publisher-place": "Lha sa",
          "issued": {"date-parts": [[1979]]},
          "ISBN": "014259154",
          "language": "en_US",
          "collection-number": "number 4",
          "collection-title": "parent series",
          "edition": "par gzhi1., par thengs 2."
        }

        var cite = new Cite(examplecsl, { 'forceType': '@csl/object' })
        var res = cite.format('bibliography', {
          format: 'html',
          template: 'chicago-fullnote-bibliography',
          lang: "zh_CN"
        })
        console.log(res)
      }
      main()

     </script>
  </head>
  <body>
  </body>
</html>
larsgw commented 3 years ago

I suppose that might be caused by the non-ASCII characters, but since it's directly from the CSL repository it's weird that it breaks citeproc-js.

larsgw commented 3 years ago

Nevermind, the problem is that citeproc replaces the underscore in zh_CN with a hyphen (zh-CN), and then tries to request it under that name. A workaround would be to do this instead:

config.locales.add("zh-CN", zh_CN_xml)

The next release of Citation.js will include a fix, however.

eroux commented 3 years ago

ah excellent, thanks a lot!

eroux commented 3 years ago

Thanks!