Complete TypeScript type definitions for @cognipeer/to-markdown.
Input type for the converter function.
type ConverterInput = string | Buffer;
Can be:
Configuration options for file conversion.
interface ConverterOptions {
fileName?: string;
forceExtension?: string;
url?: string;
}
See Converter Options for detailed documentation.
Enumeration of supported file extensions.
enum FileExtension {
PDF = '.pdf',
DOCX = '.docx',
HTML = '.html',
HTM = '.htm',
TXT = '.txt',
IPYNB = '.ipynb',
XML = '.xml',
RSS = '.rss',
ATOM = '.atom',
XLSX = '.xlsx',
XLS = '.xls',
CSV = '.csv',
MP3 = '.mp3',
WAV = '.wav',
PPTX = '.pptx',
ZIP = '.zip',
JPG = '.jpg',
JPEG = '.jpeg',
PNG = '.png',
GIF = '.gif',
}
Metadata extracted from image files.
interface ImageMetadata {
width?: number;
height?: number;
format?: string;
size?: number;
}
Metadata extracted from audio files.
interface AudioMetadata {
title?: string;
artist?: string;
album?: string;
duration?: number;
bitrate?: number;
}
type OCRProvider =
| 'tesseract'
| 'openai-vlm'
| 'anthropic-vlm'
| 'ollama-vlm'
| 'azure-vision'
| 'custom-vlm'
| 'handler';
Use 'handler' when you supply your own async OCR function. The library does not perform HTTP requests in that mode.
Metadata passed to a custom OCR handler.
interface OCRHandlerContext {
page?: number;
pageCount?: number;
mimeType?: string;
sourceExtension?: string;
fileName?: string;
imageWidth?: number;
imageHeight?: number;
}
type OCRHandler = (
buffer: Buffer,
context: OCRHandlerContext
) => Promise<string>;
interface OCROptions {
provider?: OCRProvider;
lang?: string;
pdfMode?: 'auto' | 'always' | 'never';
vlm?: VLMOptions;
handler?: OCRHandler;
}
When provider is 'handler', handler is required.
Options for HTML to Markdown conversion using Turndown.
interface TurndownOptions {
headingStyle: 'setext' | 'atx';
hr: string;
bulletListMarker: string;
codeBlockStyle: 'indented' | 'fenced';
emDelimiter: string;
keepHeaderLevels?: boolean;
}
Result of file type detection.
interface FileTypeResult {
ext: string;
mime: string;
}
import {
convertToMarkdown,
type ConverterInput,
type ConverterOptions,
FileExtension
} from '@cognipeer/to-markdown';
// Type-safe input
const input: ConverterInput = './document.pdf';
// Type-safe options
const options: ConverterOptions = {
forceExtension: FileExtension.PDF
};
// Type-safe conversion
const markdown: string = await convertToMarkdown(input, options);
Check input types:
function isBufferInput(input: ConverterInput): input is Buffer {
return Buffer.isBuffer(input);
}
if (isBufferInput(input)) {
// input is Buffer
console.log('Processing buffer of size:', input.length);
} else {
// input is string
console.log('Processing file:', input);
}
Create type-safe wrapper functions:
async function convertFile<T extends ConverterInput>(
input: T,
options?: ConverterOptions
): Promise<string> {
return await convertToMarkdown(input, options);
}
Import only the types you need:
// Import type only (doesn't include in runtime bundle)
import type { ConverterOptions } from '@cognipeer/to-markdown';
// Import enum (includes in runtime)
import { FileExtension } from '@cognipeer/to-markdown';
// Import everything
import { convertToMarkdown, type ConverterOptions, FileExtension } from '@cognipeer/to-markdown';