An image of Heer de Beer

Heer de Beer.org

Exploring the Computational Medium

Paja, Pandoc wrapped around in JavaScript

Huub de Beer

July, 2016

Paja is a simple JavaScript wrapper around pandoc, the great multi-format document converter. Paja is inspired by Paru, a Ruby wrapper for pandoc that I wrote earlier. Like Paru, Paja supports automating the use of pandoc. Paja is free software; Paja is licensed under the GNU General Public Licence version 3. Get the code at https://github.com/htdebeer/paja.

The current version of Paja is 0.0.3, which is an alpha version.

1 Installation

Because Paja is a wrapper around pandoc, pandoc is obviously a requirement for Paja. Install Paja with npm:

npm install paja

2 Usage

The obligatory “hello world” program with paja:

const paja = require("paja");
const INPUT = `
> Hello World! 

from **Paja**`;

const markdown2html = paja.Pandoc
    .converter()
    .from("markdown")
    .to("html");

markdown2html.run(INPUT, console.log);

which will output:

<blockquote>
<p>Hello World!</p>
</blockquote>
<p>from <strong>Paja</strong></p>

All of pandoc’s long-style command line options are mapped to a method of the Pandoc class. Option names without the prefix “--” are converted to camelCase method names. For example:

The table of contents’s short option is actually also supported—I got tired of typing the long form quite fast as I use that option often—so Pandoc.toc() works as well.

Pandoc can also be used as a transform stream. This makes it easy to pipe some read stream through pandoc. For example, this documentation file, index.md can be converted to HTML and outputted to STDOUT as follows:

const paja = require("paja");
const fs = require("fs");

const md2html = paja.Pandoc
    .converter()
        .from("markdown")
        .to("html")
        .css("style.css")
        .css("extra-style.css")
        .toc()
        .standalone()
    .stream();

const INPUT_FILE = "index.md";

fs.createReadStream(INPUT_FILE, {encoding: "utf8"})
    .pipe(md2html)
    .pipe(process.stdout);

Observe that the Pandoc.css method is called twice; Pandoc adds both stylesheets to the HEAD element of the generated HTML. All pandoc’s command line options that can occur more than once, such as --include-in-header or --variable, have the same behavior: you can call these methods as often as you like.