项目作者: Zettlr

项目描述 :
A small library helping to parse citations between Markdown and CSL JSON
高级语言: TypeScript
项目地址: git://github.com/Zettlr/Citr.git
创建时间: 2019-06-10T17:14:41Z
项目社区:https://github.com/Zettlr/Citr

开源协议:GNU General Public License v3.0

下载


The code in this repository is no longer maintained. Most of the functionality has moved directly into the main Zettlr repository.



Citr

Citr

Converts Markdown Citations to CSL JSON

A small library for parsing Markdown citeproc citations to valid CSL JSON (and vice versa).

Description

This module transforms citations as they are described in the Pandoc manual into valid CSL JSON that can then — for instance — be passed to citeproc-js.

Install

With NPM:

  1. $ npm install @zettlr/citr

With Yarn:

  1. $ yarn add @zettlr/citr

Usage

  1. Citr.parseSingle(markdown, strict?) // Parses a single citation from Markdown to CSL JSON
  2. Citr.makeCitation(csl) // Converts a CSL JSON citation to Markdown
  3. Citr.util.extractCitations(text, strict?) // Extracts all citations from a text
  4. Citr.util.validateCitationID(key, strict?) // Validates a given citation key

Citr exposes a small API that you can conveniently use:

  1. const Citr = require('Citr')
  2. let myCitation = '[see -@doe99, pp. 33-35; also @smith04, chap. 1]'
  3. let csl = Citr.parseSingle(myCitation)
  4. /*
  5. [
  6. {
  7. prefix: 'see',
  8. suffix: '',
  9. id: 'doe99',
  10. locator: '33-35',
  11. label: 'page',
  12. 'suppress-author': true
  13. },
  14. {
  15. prefix: 'also',
  16. suffix: '',
  17. id: 'smith04',
  18. locator: '1',
  19. label: 'chapter',
  20. 'suppress-author': false
  21. }
  22. ]
  23. */

If the citation contains any malformed partial citations, Citr will throw an error, so to test for errors, use try/catch constructs:

  1. const Citr = require('Citr')
  2. let myCitation = '[Malformed ID inside @.this key]'
  3. let csl = ''
  4. try {
  5. csl = Citr.parseSingle(myCitation)
  6. } catch (err) {
  7. console.error(`The citation was malformed.`)
  8. }

To extract all citations that are inside a given Markdown file/text, Citr exposes a convenient function:

  1. const Citr = require('Citr')
  2. let myText = 'This is some Text, where both Doe [-@doe99] and others said something [see -@doe99, pp. 33-35; also @smith04, chap. 1]. Of course, this is debatable.'
  3. let citations = Citr.util.extractCitations(myText)
  4. /*
  5. [
  6. '[-doe99]',
  7. '[see -@doe99, pp. 33-35; also @smith04, chap. 1]'
  8. ]
  9. */

You can then afterwards pass all citations in a for-loop through the parseSingle-function.

If you simply want to conveniently check an ID, use the utility function validateCitationID:

  1. const Citr = require('Citr')
  2. let goodKey = '@Doe1990'
  3. let badKey = '@.wrongKey'
  4. Citr.util.validateCitationID(goodKey) // true
  5. Citr.util.validateCitationID(badKey) // false

Last but not least you may want to generate a Markdown citation string from a given CSL JSON object. To do so, simply pass a CSL JSON object to the makeCitation function. The only required attribute is id. Please note that this conversion is not language-sensitive, but will output everything as English text. Thereby it can be passed again to the parseSingle-function to retrieve the correct citation.

  1. const Citr = require('Citr')
  2. const csl = [
  3. {
  4. prefix: 'see',
  5. suffix: '',
  6. id: 'doe99',
  7. locator: '33-35',
  8. label: 'page',
  9. 'suppress-author': true
  10. },
  11. {
  12. prefix: 'also',
  13. suffix: '',
  14. id: 'smith04',
  15. locator: '1',
  16. label: 'chapter',
  17. 'suppress-author': false
  18. }
  19. ]
  20. let markdownCitation = Citr.makeCitation(csl)
  21. /*
  22. '[see -@doe99, pp. 33-35; also @smith04, chap. 1]'
  23. */

You can, of course, also pass one single object to the engine.

Legacy (“strict”) mode

The strict parameter is optional and restores the behavior of versions pre 1.1.0 in that functions validating citekeys can either apply a strict mode or a “loose” mode. In strict mode, only a very small subset of ASCII characters are allowed for citekeys (no umlauts as ö, ü, é, è, non-latin script, etc.), while the loose mode will allow as many letter characters as possible. By default, strict mode is off (strict = false). To enable strict mode, pass true to any of the functions that allow the strict mode.

Example:

  1. const Citr = require('Citr')
  2. let asciiKey = '@Doe1990'
  3. let unicodeKey = '@村上2018'
  4. Citr.util.validateCitationID(asciiKey) // true
  5. Citr.util.validateCitationID(asciiKey, true) // true (strict mode enabled)
  6. Citr.util.validateCitationID(unicodeKey) // true (Japanese characters are allowed)
  7. Citr.util.validateCitationID(unicodeKey, true) // false (only ASCII characters allowed)
  8. try {
  9. let citation = Citr.parseSingle(unicodeKey, true) // Enable strict mode
  10. } catch (err) {
  11. console.error('An error will be thrown, as parseSingle will call validateCitationID using strict mode')
  12. }

Contributions

Contributions and PRs are welcome. By contributing, you agree that your code will also be made available under the GNU GPL v3 license.

License

This software is licenced via the GNU GPL v3-License.

The brand (including name, icons and everything Citr can be identified with) is exluded and all rights reserved. If you want to fork Citr to develop another library, feel free but please change name and icons.