1.0.5 • Published 3 years ago

pandoc-ts v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

pandoc-ts

Support Node of LTS npm version Build passing Support dependencies pandoc License mit


Description

pandoc-ts is a pure typescript wrapper for nodejs of pandoc. Main purpose of this library is to provide synchronous and asynchronous methods to convert documents in all formats supported by pandoc.

How to use

Install dependencies:

You need to install pandoc and add it to the system path to use this library.
To install pandoc follow the official procedure at https://pandoc.org/installing.html

Import in node js

After pandoc is installed, simple run:

npm install --save pandoc-ts

or

yarn add pandoc-ts

Use in your project

Typescript

import { Pandoc, PandocOutFormat, PandocData } from "pandoc-ts";

Javascript

const pandoc = require("pandoc-ts");
const Pandoc = pandoc.Pandoc;
const PandocOutFormat = pandoc.PandocOutFormat;
const PandocData = pandoc.PandocData;

Configuration

To use corverter you have to configure the output format you need and create an instance of converter.

Typescript

const outFormats: PandocOutFormat[] = [
  {
    name: String,
    format: String,
    fname: String,
    outBin: Boolean,
    enc: String,
  },
  ...
];
const pandocInstance = new Pandoc('format', outFormats);
const outFormats = [
  {
    name: String,
    format: String,
    fname: String,
    outBin: Boolean,
    enc: String,
  },
  ...
];
const pandocInstance = new Pandoc('format', outFormats);

Where:

  • name: friendly name for the output
  • format: Pandoc format name for input or output see here for more details
  • fname: define the output file name for current format
  • outBin: if true the current format output will be returned in raw buffer format
  • enc: if outBin is false and fname is not defined set the output content encoding. For more information see here

outFormats: could also be single object rather than an array

Convert

Now you can call conversion function in the instance you created.

Typescript

pandocInstance.convert("text", callback);
pandocInstance.convertFile("file-name", callback);

// or async

const result: PandocData = await pandocInstance.convertAsync("text");
const result: PandocData = await pandocInstance.convertFileAsync("file-name");

Javascript

pandocInstance.convert("text", callback);
pandocInstance.convertFile("file-name", callback);

// or async

const result = await pandocInstance.convertAsync("text");
const result = await pandocInstance.convertFileAsync("file-name");

Where:

  • text: string text to convert in the specific format
  • file-name: name of the file to convert
  • callback: function to call when the operation is completed

Callback signature: (data: PandocData, err: Error | null) => void
where data is a PandocData value and Error is Nodejs Error

PandocData is defined as { [format: string]: string | Buffer } | null.
A nullable key value-value pair where key is the name of the out format and value is the result of coversion.

Example

In this sample we convert a docx file (input.docx) in two format.

  • gfm (github markdown)
  • html

First one will be converted in utf-8 string (as default) and shown in console
The second saved in an output file "sample.html"

import { Pandoc, PandocOutFormat } from "pandoc-ts";

const outs: PandocOutFormat[] = [
  { name: "markdown", name: "gfm", outBin: false },
  { name: "markdownFile", name: "gfm", fname: "sample.md" },
  { name: "html", name: "html", fname: "sample.html" },
];

const pandocInstance = new Pandoc("docx", outs);

pandocInstance.convertFile("input.docx", (result, err) => {
  if (err) {
    console.error(err);
  }
  if (result) {
    console.log(result.markdown);
    console.log(result.markdownFile);
    console.log(result.html);
  }
});
1.0.2

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago