citeproc-js Documentation

repository·master·Indexed 18 days ago

https://github.com/juris-m/citeproc-js

A robust JavaScript implementation of the Citation Style Language (CSL) for generating complex citations and bibliographies. It supports standard CSL (1.0, 1.0.1) and Juris-M (1.1mlz1) specifications, including specialized CSL-M extensions for legal and multilingual content. The module provides an API for disambiguation, sorting, and localization, with support for HTML, RTF, and plain text output formats. It is designed for use in text processors, web environments, and reference managers like Zotero and Mendeley.

Tokens
11.5K
Snippets
39
Records
47
Agent score
62%

What's inside citeproc-js

  1. What is citeproc-js?

    master

    citeproc-js is a JavaScript implementation of the Citation Style Language (CSL), an open XML-based standard for describing bibliographic citation styles. It allows for modular control over how citations and bibliographies are formatted, enabling users to switch styles (e.g., from sciences to humanities or law) without changing the underlying document data.

    Key capabilities include:

    • Disambiguation: Handles author-date disambiguation (e.g., following Chicago Manual of Style).
    • Sorting: Supports complex sorting rules with multiple keys and arbitrary orders (e.g., AGU journal styles).
    • Parallel Citations: Supports legal citation requirements, such as citing a case in multiple reporters.
    • On-the-fly Updates: The API supports targeted, context-sensitive updates to citations when document content is inserted, deleted, or edited.
    • Localization: Supports localized date formatting and style-supplied labels.
    • Name Handling: Sophisticated handling of European name particles (e.g., "von", "van", "di") for sorting and rendering.
    • In-field Formatting: Recognizes a subset of HTML for scientific markup (superscript, subscript, italics, etc.) and transforms it into the target output format (HTML, RTF, LaTeX, etc.).
    • Multi-lingual Support: Experimental support for transliteration, title translation, and alternative sort strings for Asian languages.
  2. What is citeproc-js

    master

    citeproc-js is a JavaScript implementation of the Citation Style Language (CSL). It is a CSL processor used to generate citations and bibliographies by applying CSL style rules to bibliographic data.

    Key features include:

    • Support for the standard CSL specification.
    • Support for CSL-M mode, a set of private extensions used by the Jurism reference manager to handle multilingual and legal content with high precision.
    • Proven reliability, having served as the citation formatter for Mendeley and Zotero.
  3. Format multilingual data for titles and strings

    master

    To support multilingual operation, provide alternative representations of field content (like titles) within a multi segment on the item. This segment is keyed by the field name and the language tag using the _keys element.

    Example structure for a title:

    { "title" : "民法",
         "multi": {
           "_keys": {
             "title": {
               "ja-alalc97": "Minpō",
               "en":"Civil Code"
             }
           }
         }
       }
    {
     "title" : "民法",
     "multi": {
       "_keys": {
         "title": {
           "ja-alalc97": "Minpō",
           "en":"Civil Code"
         }
       }
     }
    }
  4. Format multilingual data for names

    master

    For names, alternative representations are placed in a multi segment of the name object itself. Note that the name object uses the _key element (singular) instead of _keys.

    Example structure for an author name:

    { "author" : [
           {
             "family" : "穂積",
             "given" : "陳重",
             "multi": {
               "_key": {
                 "ja-alalc97": {
                   "family" : "Hozumi",
                   "given" : "Nobushige"
                 }
               }
             }
           }
         ]
       }
    {
     "author" : [
           {
             "family" : "穂積",
             "given" : "陳重",
             "multi": {
               "_key": {
                 "ja-alalc97": {
                   "family" : "Hozumi",
                   "given" : "Nobushige"
                 }
               }
             }
           }
         ]
       }
    }
  5. Configure XML parsing for different environments

    master

    Since JavaScript lacks a standard I/O method, the processor relies on supplementary code to parse XML files used for locales and styles. The choice of parser depends on your runtime environment:

    • Browser environments (or systems with DOM support): Use xmldom.js.
    • Node.js or Firefox worker threads (no native DOM): Use xmljson.js.

    Alternatively, you can pre-process XML into JSON using csl-json-walker (requires DOM) or the makejson.py Python script.

  6. Choose the correct processor file

    master

    The processor is distributed in two main formats. Depending on your environment, you should choose one of the following:

    • citeproc_commonjs.js: An ES6 module. This is the recommended version for most modern use cases.
    • citeproc.js: A raw bundle of JavaScript.

    Note that these files are automatically built when tests are run using the cslrun command.

  7. Format date fields in citation data

    master

    Dates should be provided as JavaScript objects containing a date-parts array. Each inner array represents a date (start or end) in the order: [year, month, day].

    • Types: Elements can be numeric strings or numbers.
    • Seasons: A season element can be included. Values 1 through 4 map to Spring, Summer, Fall, and Winter. Other strings are treated as literals.
    • Circa: For approximate dates, include a circa element with a non-nil value.
    • Ranges: To specify a date range, provide two arrays within date-parts. For an open-ended range, use [0, 0] for the end elements.
    • Literals: Use a literal element to pass a raw string (e.g., "13th century").
    // Standard date
    { "issued" : { "date-parts" : [ [ "2000", "1", "15" ] ] } }
    
    // Date range (Nov 2000 to Dec 2000)
    { "issued" : { "date-parts" : [ [ 2000, 11 ], [ 2000, 12 ] ] } }
    
    // Open-ended range
    { "issued" : { "date-parts" : [ [ 2008, 11 ], [ 0, 0 ] ] } }
    
    // Approximate date
    { "issued" : { "date-parts" : [ [ -225 ] ], "circa" : 1 } }
    
    // Literal string
    { "issued" : { "literal" : "13th century" } }
  8. Generate split citations using author-only and suppress-author

    master

    To support styles that require splitting citation content (e.g., placing the author name in the text and the numeric identifier in a superscript), you can use two control elements in sequence:

    1. author-only: When passed to makeCitationCluster(), it returns the author name without decorative markup (like italics or superscript). In numeric styles, it may provide a localized label and the numeric identifier.
    2. suppress-author: When passed to processCitationCluster() or appendCitationCluster(), it suppresses the rendering of names. In numeric styles, it suppresses output if the identifier was already provided in the first step.

    Workflow for split citations (e.g., Author-Date or GB7714-87):

    1. Call makeCitationCluster() with {"author-only": 1} to get the author part.
    2. Call processCitationCluster() with {"suppress-author": 1} to get the remaining citation part.
    // 1. Get the author part
    var my_ids = {
         ["ID-3", {"author-only": 1}]
    };
    var result = citeproc.makeCitationCluster( my_ids );
    
    // ... later ...
    
    // 2. Get the rest of the citation (suppressing the author)
    var citation, result;
    
    citation = {
         "citationItems": ["ID-3", {"suppress-author": 1}],
         "properties": { "noteIndex": 5 }
    };
    [data, result] = citeproc.processCitationCluster( citation );
  9. Use the <legal/> CSL element for modular legal support

    master

    The <legal/> CSL element is used to trigger the loading of jurisdiction-specific style modules. When the processor encounters this element, it invokes citeproc.sys.loadJurisdictionStyle() and hands over control of the item's processing to the matched legal style module.

    Usage Requirements

    • Placement: The <legal/> element should be placed at the beginning of processing, typically under the <citation/> or <bibliography/> sections.
    • Required Attribute:
      • default (<string>): Specifies the default jurisdiction to use if no matching style is found for the item's jurisdiction.
    • Sub-elements: The element requires specific sub-elements as defined in the CSL schema (refer to the implementation for specific required sub-elements).
    <!-- Example conceptual structure -->
    <citation>
      <legal default="US">
        <!-- subelements -->
      </legal>
      <!-- remaining citation processing -->
    </citation>
  10. Structure of CSL JSON fields

    master

    A CSL JSON object consists of three types of fields: ordinary fields, creator fields, and date fields. Each type has specific requirements for how data is structured to ensure correct citation rendering.

    Field Types Overview

    1. Required Metadata Fields:

      • id: Required. A unique string or numeric value used to identify the item.
      • type: Required. A valid CSL or CSL-M type (e.g., book).
    2. Ordinary Fields (e.g., title):

      • Can be set as simple strings or numbers.
    3. Creator Fields (e.g., author):

      • Must be an array of objects. Supports personal names (using family and given), discrete name components, and institutional names (using literal).
    4. Date Fields (e.g., issued):

      • Can be set using structured date-parts (arrays) or a raw string format.
    {
       "id": "item123456789",
       "type": "book",
       "title": "Book One",
       "author": [
          {
             "family": "Jones",
             "given": "Michael"
          }
       ],
       "issued": {
          "date-parts": [[ 2000, 3, 15 ]]
       }
    }
  11. Format author names with particles and suffixes

    master

    When providing author data, you can handle complex name structures using specific elements:

    • Particles: Use dropping-particle (e.g., "von") and non-dropping-particle (e.g., "van") to separate particles from the family and given names.
    • Suffixes: Use the suffix element for name suffixes like "Jr." or "III".
    • Comma Suffixes: If a suffix is preceded by a comma in the output, include the comma-suffix: "true" field.
    • Non-Latin/Cyrillic Scripts: The processor automatically handles non-Byzantine scripts (e.g., Kanji) by displaying the family name first.
    • Static Ordering: To force a Latin or Cyrillic transliteration to behave like a non-Byzantine name (fixed family-name-first ordering), include a static-ordering element in the name array with any truthy value.
    { "author" : [
           { "family" : "Humboldt",
             "given" : "Alexander",
             "dropping-particle" : "von"
           },
           { "family" : "Gogh",
             "given" : "Vincent",
             "non-dropping-particle" : "van"
           },
           { "family" : "Bennett",
             "given" : "Frank G.",
             "suffix" : "Jr.",
             "comma-suffix": "true"
           },
           { "family" : "Murakami",
             "given" : "Haruki",
             "static-ordering" : 1
           }
         ]
       }
  12. How to use citeproc-js in applications

    master

    While originally developed for use in Zotero, citeproc-js is a standalone JavaScript module with a relatively simple API. It can be deployed in various environments that generate dynamic content, such as:

    • Text processors
    • Word processors
    • Weblog environments
    • Dynamic websites

    By using appropriate wrappers, developers can integrate publisher-correct citation and bibliography facilities into their applications with minimal programming effort.