$ show harshankur/officeParser
This repository contains 'officeParser', a robust Node.js and browser library for parsing 12 office document formats (DOCX, PPTX, XLSX, ODT, ODP, ODS, PDF, RTF, CSV, MD, HTML, EPUB) into a unified abstract syntax tree (AST) and generating high-fidelity output in 9 formats (JSON, text, Markdown, HTML, CSV, RTF, PDF, EPUB, and RAG-focused chunks). It provides a first-class CLI (`npx officeparser`) and a programmatic API (`parseOffice()` → AST → `.to(format, config?)`), with extensive configuration for OCR, chunking strategies, security limits, and format-specific dialects. The architecture follows a strict pipeline: format-specific parsers normalize documents into `OfficeParserAST`, then format-agnostic generators produce the desired output, enabling cross-format conversion and round-trip fidelity. The codebase demonstrates high craftsmanship with reusable utility modules (sanitization, typed error/warning systems, lazy ZIP handling, MathML-to-LaTeX conversion) and a comprehensive test suite covering parser baselines, generator baselines, security injection tests, and exhaustively tested edge cases. It supports browser and Node runtimes via dual CJS/ESM packages and bundled builds, including slim variants that minimize dependencies for browser environments. Key data flows include document metadata extraction, attachment handling (images, charts), OCR via Tesseract.js, PDF generation via Puppeteer, and semantic chunking for LLM ingestion. External dependencies include `pdfjs-dist`, `tesseract.js`, `fflate`, `chart.js`, and `puppeteer` (optional). While the repository is actively maintained with recent commits and CI across multiple Node versions, it shows some technical drift: deprecated API fields remain documented alongside replacements, and large files (e.g., OpenOfficeParser.ts at ~1600 lines) increase maintenance complexity. The builder has addressed past packaging issues (webpack context-module) and maintains sophisticated browser shims. Overall, it is a production-grade document conversion toolkit with exceptional reusability in its core utilities and a clean separation between parsing and generation.
The LLM identified 5 files worth highlighting for reuse:
Centralized escaping module for all generated output formats. Provides context-aware escaping functions for HTML (attribute allowlist, tag allowlist), CSS (value sanitization ordering, expression/image-set rejection), URLs (scheme allowlist), CSV (formula injection prefix), RTF (unicode escaping), and Markdown (raw HTML neutralization). This is the single source of truth for security-critical escaping, designed to be reused by any generator that produces text-based output from untrusted AST nodes.
Provides typed error and warning objects with codes, messages, and optional node/details context. Explicitly warns not to pass AbortErrors; all other errors are wrapped into a consistent `OfficeError` structure. This pattern enables callers to programmatically handle warnings (e.g., log, filter, abort) without parsing message strings, and is reusable in any library needing structured diagnostics.
Lazy entry reader for ZIP-based formats (DOCX, PPTX, XLSX, OD*). Implements bounded decompression (max entries, max size, max uncompressed bytes), part validation using the parser's own regex/predicate to prevent drift between validation and subsequent reads, and memory-efficient stream-based entry access. This module is self-contained and could be extracted for any project needing safe, efficient ZIP processing.
Converts OOXML MathML (`ommlToLatex`) and generic MathML (`mathmlTreeToLatex`) to LaTeX. Includes proper escaping of LaTeX metacharacters, conditional brace insertion for superscripts, and special handling of math alphabets (e.g., script attribute for blackboard bold). This is a non-trivial, reusable conversion pattern that could be applied in any document-processing pipeline requiring math rendering.
Implements three chunking strategies (document-structure, fixed-size, semantic) for producing RAG-ready chunks from the Office AST. Handles block-level boundary detection (headings, tables, lists), footnote folding into their referencing chunks, and metadata injection (closest heading, page/slide number, sheet name). The semantic strategy uses an async embedding function and cosine similarity threshold. This generator is format-agnostic and could be reused in any system needing chunked output from structured document data.