doc-to-md-rag
v1.0.1
Published
**A RAG-Optimized PDF & DOCX to Markdown Converter for Node.js**
Readme
📄 doc-to-md-rag
A RAG-Optimized PDF & DOCX to Markdown Converter for Node.js
Most PDF parsers destroy table layouts, turning data into "word soup" that confuses LLMs. doc-to-md-rag uses a heuristic block segmentation algorithm to detect visual grids and preserve them as clean, token-efficient Markdown tables, while keeping non-tabular text as normal paragraphs.
Perfect for RAG (Retrieval-Augmented Generation) pipelines, chatbots, and data extraction.
🚀 Features
- RAG-First Design: Output is optimized for LLM context windows (no wasted tokens on empty rows/cols).
- Intelligent Layout Engine: Automatically distinguishes between "Text Blocks" and "Table Blocks."
- Native Node.js: No Python dependencies, no
pdfplumberbinaries needed. - Hybrid Support: Handles both
.pdfand.docx(via Mammoth) seamlessly. - Zero "Ghost Columns": Smart clustering prevents the "100 empty columns" bug common in other parsers.
📦 Installation
npm install doc-to-md-rag
🛠️ Usage
import { convertToMarkdown } from 'doc-to-md-rag';
(async () => {
try {
// Works with PDF
console.log("Converting PDF...");
const pdfMd = await convertToMarkdown('./financial-report.pdf');
console.log(pdfMd);
// Works with DOCX
console.log("Converting DOCX...");
const docxMd = await convertToMarkdown('./contract.docx');
console.log(docxMd);
} catch (err) {
console.error("Conversion failed:", err);
}
})();
📊 Output Comparison
Why does this library exist? Standard parsers flatten tables, destroying the relationship between headers and data.
Standard Parsers (Bad for RAG)
The LLM loses the grid relationship and cannot answer questions like "What was the Net Revenue in 2022?" accurately.
Revenue 2023 2022
Total $500 $400
Net $50 $40
doc-to-md-rag (Perfect for RAG)
Preserves the grid structure using Markdown, allowing the LLM to understand row/column relationships.
| Revenue | 2023 | 2022 |
| :--- | :--- | :--- |
| Total | $500 | $400 |
| Net | $50 | $40 |
🤝 Contributing
Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
📄 License
Distributed under the MIT License. See LICENSE for more information.
