pandoc-ts
v1.0.5
Published
  ;
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);
}
});