Skip to content

Getting Started ​

Installation ​

Install the package using npm:

bash
npm install @cognipeer/to-markdown

Or using yarn:

bash
yarn add @cognipeer/to-markdown

Or using pnpm:

bash
pnpm add @cognipeer/to-markdown

The package requires Node.js 18 or newer.

Optional: OCR ​

OCR is opt-in. If you want to use the default tesseract OCR provider, install its peer dependency as well:

bash
npm install tesseract.js

The vision-model (VLM) OCR providers use the native fetch API and need no extra dependency. See the OCR guide for details.

Basic Usage ​

Converting from File Path ​

The simplest way to convert a file is to provide its path:

typescript
import { convertToMarkdown } from '@cognipeer/to-markdown';

const markdown = await convertToMarkdown('./path/to/document.pdf');
console.log(markdown);

Converting from Buffer ​

You can also convert files that are already loaded as buffers:

typescript
import { convertToMarkdown } from '@cognipeer/to-markdown';
import { readFileSync } from 'fs';

const buffer = readFileSync('./document.docx');
const markdown = await convertToMarkdown(buffer, {
  fileName: 'document.docx' // Optional but recommended
});
console.log(markdown);

Converting from Base64 ​

Convert base64-encoded data directly:

typescript
import { convertToMarkdown } from '@cognipeer/to-markdown';

// With data URL
const base64 = 'data:application/pdf;base64,JVBERi0xLjU...';
const markdown = await convertToMarkdown(base64);

// Or plain base64
const plainBase64 = 'JVBERi0xLjU...';
const markdown2 = await convertToMarkdown(plainBase64, {
  fileName: 'document.pdf' // Helps with format detection
});

Saving Output ​

Use the saveToMarkdownFile function to save converted markdown to a file:

typescript
import { convertToMarkdown, saveToMarkdownFile } from '@cognipeer/to-markdown';

const markdown = await convertToMarkdown('./document.pdf');
const outputPath = await saveToMarkdownFile(
  markdown,
  'converted-document', // filename without extension
  './output' // output directory
);

console.log(`Saved to: ${outputPath}`);

TypeScript Support ​

The library is written in TypeScript and provides full type definitions:

typescript
import {
  convertToMarkdown,
  saveToMarkdownFile,
  type ConverterOptions,
  type ConverterInput
} from '@cognipeer/to-markdown';

const options: ConverterOptions = {
  fileName: 'document.pdf',
  forceExtension: '.pdf'
};

const input: ConverterInput = buffer; // or string
const result = await convertToMarkdown(input, options);

Next Steps ​

Studio · Pulse · Console · Agent SDK and more — the Cognipeer documentation hub