@aidalinfo/office-to-markdown
Version:
Modern TypeScript library for converting Office documents (DOCX) to Markdown format, optimized for Bun runtime with enhanced table support and math equation conversion.
129 lines • 4.63 kB
TypeScript
import type { ConverterOptions } from "../types/converter.js";
/**
* Custom HTML to Markdown converter with enhanced features.
*
* Extends the base Turndown service with:
* - Enhanced table processing with proper column alignment
* - Math equation preservation (LaTeX)
* - Improved handling of complex HTML structures
* - Custom preprocessing and postprocessing
* - Configurable output formatting
*
* **Key Features:**
* - Table preservation with Markdown table syntax
* - Math equation detection and LaTeX preservation
* - Clean handling of nested elements
* - Configurable heading styles (ATX vs Setext)
* - Proper whitespace normalization
*
* @example
* ```typescript
* const converter = new CustomHtmlToMarkdown({
* headingStyle: 'atx',
* preserveTables: true
* });
* const markdown = converter.convert('<h1>Title</h1><p>Content</p>');
* ```
*/
export declare class CustomHtmlToMarkdown {
private turndown;
constructor(options?: ConverterOptions);
/**
* Convert HTML string to Markdown using custom processing pipeline.
*
* **Processing Pipeline:**
* 1. **Preprocessing**: Handles special HTML structures and LaTeX equations
* 2. **Core Conversion**: Uses Turndown service with custom rules
* 3. **Postprocessing**: Cleans up output and normalizes formatting
*
* @param html - Input HTML string to convert
* @returns Clean Markdown string with proper formatting
*
* @example
* ```typescript
* const html = '<table><tr><th>Name</th><th>Value</th></tr><tr><td>A</td><td>1</td></tr></table>';
* const markdown = converter.convert(html);
* // Returns: | Name | Value |\n|------|-------|\n| A | 1 |
* ```
*/
convert(html: string): string;
/**
* Add enhanced table handling rules to the Turndown service.
*
* Configures custom rules for:
* - Table structure (`<table>`, `<thead>`, `<tbody>`)
* - Table rows (`<tr>`) with proper line breaks
* - Header cells (`<th>`) with separator row generation
* - Data cells (`<td>`) with proper column separation
*
* **Generated Format:**
* ```markdown
* | Header 1 | Header 2 |
* |----------|----------|
* | Cell 1 | Cell 2 |
* ```
*
* @private
*/
private addTableRules;
/**
* Add custom conversion rules for enhanced HTML to Markdown conversion.
*
* Configures specialized rules for:
* - **Headings**: Consistent ATX format with proper spacing
* - **Math equations**: Preserves LaTeX formatting (inline and block)
* - **Paragraphs**: Smart handling with math equation detection
* - **Line breaks**: Context-aware conversion (inside paragraphs vs standalone)
* - **Images**: Data URI handling and cleanup
* - **Links**: JavaScript removal and safe URL processing
* - **Code blocks**: Language detection and proper formatting
* - **Lists**: Enhanced nested content handling
*
* @private
*/
private addCustomRules;
/**
* Pre-process HTML before conversion to handle special cases.
*
* **Processing Steps:**
* 1. Remove script and style tags for security
* 2. Normalize excessive whitespace while preserving structure
* 3. Clean up Word-specific XML namespace tags
* 4. Prepare HTML for optimal Turndown processing
*
* @param html - Raw HTML string to preprocess
* @returns Cleaned HTML ready for conversion
* @private
*/
private preprocessHtml;
/**
* Post-process Markdown after conversion to clean up formatting.
*
* **Processing Steps:**
* 1. Normalize line endings (Windows to Unix)
* 2. Clean up excessive blank lines (max 3 consecutive)
* 3. Fix spacing around headings
* 4. Ensure proper list formatting
* 5. Clean up table formatting
* 6. Remove trailing spaces (except intentional line breaks)
* 7. Ensure document ends with single newline
*
* @param markdown - Raw Markdown string to postprocess
* @returns Cleaned and formatted Markdown
* @private
*/
private postprocessMarkdown;
/**
* Check if a table row looks like a header row using heuristics.
*
* Uses simple pattern matching to detect:
* - Bold text formatting (`**text**`)
* - Common header keywords (name, title, date, etc.)
*
* @param row - Table row string to analyze
* @returns True if the row appears to be a header
* @private
*/
private looksLikeHeaderRow;
}
//# sourceMappingURL=html-to-markdown.d.ts.map