import fs from 'fs'; import { AxiosInstance, AxiosResponse } from 'axios'; import { DocumentIntelligenceClient } from '@azure-rest/ai-document-intelligence'; import TurndownService from 'turndown'; type TextContent = { type: "text"; text: string; }; type ImageContent = { type: "image_url"; image_url: { url: string; }; }; type MessageContent = TextContent | ImageContent; type Message = { role: "user" | "assistant" | "system"; content: MessageContent[]; }; type LlmCallInputParams = { messages?: Message[]; imageBase64?: string; file?: fs.ReadStream; }; type LlmCall = ((params: LlmCallInputParams) => Promise) | null | undefined; interface MarkItDownOptions { requestsSession?: AxiosInstance; llmCall?: LlmCall; styleMap?: any; docintelEndpoint?: string | null; } /** * Abstract base class for document converters. * Provides a common interface for all document conversion implementations. * @abstract */ declare abstract class DocumentConverter { /** * Lower priority values are tried first. * Used for specific file formats like .docx, .pdf, .xlsx, or specific pages like Wikipedia. */ static readonly PRIORITY_SPECIFIC_FILE_FORMAT: number; /** * Used for near catch-all converters for mimetypes like text/*, etc. * These are tried after more specific converters. */ static readonly PRIORITY_GENERIC_FILE_FORMAT: number; /** * The priority of this converter. * @private */ private _priority; /** * Initialize the DocumentConverter with a given priority. * * Priorities work as follows: By default, most converters get priority * DocumentConverter.PRIORITY_SPECIFIC_FILE_FORMAT (== 0). The exception * is the PlainTextConverter, which gets priority PRIORITY_GENERIC_FILE_FORMAT (== 10), * with lower values being tried first (i.e., higher priority). * * Just prior to conversion, the converters are sorted by priority, using * a stable sort. This means that converters with the same priority will * remain in the same order, with the most recently registered converters * appearing first. * * @param {number} priority - The priority of this converter */ constructor(priority?: number); /** * Converts a local file to the target format. * @abstract * @param {string} localPath - The path to the local file to convert * @param {ConversionOptions} [options] - Optional conversion configuration * @returns {Promise} A promise that resolves with the conversion result or null if conversion is not applicable * @throws {Error} May throw implementation-specific errors during conversion */ abstract convert(localPath: string, options?: ConversionOptions): Promise; /** * Gets the priority of the converter in the converter list. * Lower values are tried first (higher priority). * @returns {number} The priority value */ get priority(): number; /** * Sets the priority of the converter. * @param {number} value - The new priority value */ set priority(value: number); } type DocumentConverterResult = { title: string | null; textContent: string; } | null; type ConversionOptions = { fileExtension: string; parentConverters?: DocumentConverter[]; url?: string; requestsSession?: AxiosInstance; } & MarkItDownOptions; /** * Converts plain text files to a standard document format. * Handles various text-based content types including plain text and JSON files. * * @extends DocumentConverter * * @example * ```typescript * const plaintextConverter = new PlainTextConverter(); * let result = await plaintextConverter.convert('document.txt', { * fileExtension: '.txt' * }); * * // Using Markitdown * const converter = new Markitdown(); * let result = await converter.convert('document.txt'); * ``` */ declare class PlainTextConverter extends DocumentConverter { constructor(priority?: number); /** * Converts a text file to the standard document format. * Automatically detects content type based on file extension and only processes * files that have text/* MIME types or application/json. * * @param {string} localPath - Path to the text file * @param {ConversionOptions} options - Conversion options including file extension * @returns {Promise} Object containing the file content as textContent (title is null), or returns null if the file type is not supported * @throws {Error} If the file cannot be read or decoded */ convert(localPath: string, options: ConversionOptions): Promise; } /** * Converts HTML files to Markdown format. * Handles both standalone HTML files and HTML content from other converters. * Removes scripts and styles while preserving content structure. * * @extends DocumentConverter * * @example * ```typescript * const htmlConverter = new HtmlConverter(); * let result = await htmlConverter.convert('page.html', { fileExtension: '.html' }); * * // Using Markitdown * const converter = new Markitdown(); * let result = await converter.convert('page.html'); * ``` */ declare class HtmlConverter extends DocumentConverter { constructor(priority?: number); /** * Converts an HTML file to Markdown format. * * @param {string} localPath - Path to the local HTML file * @param {ConversionOptions} options - Conversion options * @param {string} [options.fileExtension] - File extension (must be .html or .htm) * @returns {Promise} Conversion result or null if: * - File extension is not .html or .htm * - File cannot be read * - Conversion fails */ convert(localPath: string, options: ConversionOptions): Promise; /** * Converts HTML content to Markdown format. * Internal method used by both direct HTML conversion and other converters. * * @param {string} htmlContent - Raw HTML content to convert * @returns {DocumentConverterResult} Object containing title and converted markdown content * * @remarks * - Removes all