@ aidalinfo/ 办公室到标记下
一个现代化的TypeScript库,用于将Office文档(DOCX)转换为Markdown格式,在高级支持数学方程式和表格的情况下,为Bun生态系统进行了优化.
通过微软的逆向工程创建 标记下方 - TypeScript的再执行,将Python的文档转换能力带入JavaScript/Bun生态系统,增强性能和类型安全.
* 特性
- ✅ DOCX 到 Markdown 转换 结构保护
- ✅ 数学等式支持 (OMML ~ LaTeX) (中文(简体) )
- ✅ 表格处理 自动格式化
- ✅ 样式保存 (粗体、斜体、标题)
- ✅ 图像处理 带备选案文文本
- ✅ 简单而高级的 API 用于不同用途的情况
- ✅ 强力错误处理 带有特定错误代码
- ✅ 最佳业绩 用 Bun 运行时间
- ✅ 完整类型脚本类型 用于更好的 DX
* 安装
使用 Bun( 建议)
bun add @aidalinfo/office-to-markdown
以npm/yarn/pnpm计
npm install @aidalinfo/office-to-markdown
# or
yarn add @aidalinfo/office-to-markdown
# or
pnpm add @aidalinfo/office-to-markdown
所需依赖
自动安装以下依赖性:
mammoth- DOCX 到 HTML 转换turndown- HTML 到Markdown 转换jszip- ZIP存档操作(DOCX)
* 转换工作流程
转换过程遵循这些步骤:
- 文件检测 - MIME类型和扩展核查
- 预处理 - DOCX内容提取和修改
- 数学处理 - OMML — LaTeX 转换
- 主要转换 - DOCX – HTML 通过长毛象
- 后处理 - HTML – 有自定义规则的标记
简单使用
基本转换
import { docxToMarkdown } from '@aidalinfo/office-to-markdown';
// Simple file conversion
const markdown = await docxToMarkdown('./document.docx');
console.log(markdown);
高级 API
import { OfficeToMarkdown } from '@aidalinfo/office-to-markdown';
const converter = new OfficeToMarkdown({
headingStyle: 'atx', // Use ## for headings
preserveTables: true, // Preserve tables
convertMath: true, // Convert equations to LaTeX
});
// Conversion with options
const result = await converter.convertDocx('./document.docx');
console.log('Title:', result.title);
console.log('Content:', result.markdown);
从不同来源转换
import { OfficeToMarkdown } from '@aidalinfo/office-to-markdown';
const converter = new OfficeToMarkdown();
// From file path
const result1 = await converter.convert('./document.docx');
// From Buffer
const buffer = await Bun.file('./document.docx').arrayBuffer();
const result2 = await converter.convert(buffer);
// From Bun file
const file = Bun.file('./document.docx');
const result3 = await converter.convert(file);
// Batch processing
const results = await converter.convertMultiple([
'./doc1.docx',
'./doc2.docx',
buffer
]);
QQ 配置选项
| 选项 | 类型 | 默认 | 说明 |
|---|---|---|---|
headingStyle | 'atx' | 'setext' | 'atx' | 标记下行向样式 |
preserveTables | boolean | true | 保留表格 |
convertMath | boolean | true | 转换数学等式 |
styleMap | string | - 说吧 | 自定义长毛图映射 |
技术结构
模块结构
src/
├── converters/ # Document converters
│ ├── base-converter.ts # Abstract base class
│ └── docx-converter.ts # Specialized DOCX converter
├── preprocessing/ # Preliminary processing
│ └── docx-preprocessor.ts # DOCX preprocessing (math)
├── math/ # Mathematical processing
│ └── omml-processor.ts # OMML → LaTeX converter
├── utils/ # Utilities
│ ├── html-to-markdown.ts # HTML → Markdown conversion
│ ├── file-detector.ts # File type detection
│ └── error-handler.ts # Error handling
└── types/ # TypeScript definitions
├── converter.ts # Converter types
├── result.ts # Result types
└── stream-info.ts # File info types
转换管道
- 文件检测 - MIME类型和扩展核查
- 预处理 - DOCX内容提取和修改
- 数学处理 - OMML — LaTeX 转换
- 主要转换 - DOCX – HTML 通过长毛象
- 后处理 - HTML – 有自定义规则的标记
数学方程式处理
等式转换遵循这个过程:
// OMML (Office Math Markup Language)
<m:f>
<m:num>1</m:num>
<m:den>2</m:den>
</m:f>
// ↓ Preprocessing
<w:r><w:t>$\frac{1}{2}$</w:t></w:r>
// ↓ Mammoth (HTML)
<p>$\frac{1}{2}$</p>
// ↓ Turndown (Markdown)
$\frac{1}{2}$
支持的数学元素
| OMML 数据 | 拉特克语Name | 说明 |
|---|---|---|
<m:f> | \frac{}{} | 小数 |
<m:sSup> | ^{} | 指标 |
<m:sSub> | _{} | 下标 |
<m:rad> | \sqrt{} | 平方根数 |
<m:rad><m:deg> | \sqrt[]{} | 第九个根 |
高级使用示例
处理错误
import {
OfficeToMarkdown,
FileConversionException,
UnsupportedFormatException
} from '@aidalinfo/office-to-markdown';
async function convertSafely(filePath: string) {
try {
const converter = new OfficeToMarkdown();
const result = await converter.convertDocx(filePath);
return result.markdown;
} catch (error) {
if (error instanceof UnsupportedFormatException) {
console.error('Unsupported format:', error.message);
} else if (error instanceof FileConversionException) {
console.error('Conversion error:', error.message);
} else {
console.error('Unexpected error:', error.message);
}
throw error;
}
}
能力检查
import { OfficeToMarkdown } from '@aidalinfo/office-to-markdown';
const converter = new OfficeToMarkdown();
// Check supported types
const info = converter.getSupportedTypes();
console.log('Extensions:', info.extensions); // ['.docx']
console.log('MIME types:', info.mimeTypes);
// Check if a file is supported
const isSupported = await converter.isSupported('./document.pdf');
console.log('PDF supported:', isSupported); // false
// Get file information
const fileInfo = await converter.getFileInfo('./document.docx');
console.log('MIME type:', fileInfo.mimetype);
console.log('Supported:', fileInfo.supported);
使用节点js
import { readFile } from 'fs/promises';
import { OfficeToMarkdown } from '@aidalinfo/office-to-markdown';
// From Node.js Buffer
const buffer = await readFile('./document.docx');
const converter = new OfficeToMarkdown();
const result = await converter.convert(buffer);
console.log(result.markdown);
测试和验证
测试结果
- XQHTML – 有表格的标记下转换
- QQ 文件类型检测( DOCX vs 他人)
- — OMML — LaTeX 数学转换
- QQ 处理特定代码出错
- * 用真实文件完成管道测试
业绩
- 速度: ~80ms 平均文档 (7KB)
- 忠诚: 完全保留结构和内容
- 强健性: 处理后退错误
发展
先决条件
- 阿宝 >= 1.2.0(建议)或 节点.js 页:1
- 类型脚本 * 4.5.0
开发安装
git clone https://github.com/aidalinfo/extract-kit.git
cd extract-kit/packages/office-to-markdown
bun install
可用的脚本
bun run build # Complete build (ESM + types)
bun run dev # Development mode with watch
bun run clean # Clean dist/ folder
测试
# Basic functionality test
bun run src/test.ts
# Test with real DOCX file
bun run test-docx.ts "your-file.docx"
* 路线图
- PPT/PPTX格式支持 - 演示文稿转换
- XLS/XLSX 格式支持 - 电子表格转换
- 流出 API - 大文件流处理
- 插件系统 - 支持自定义转换器
- 网络接口 - 可选用户界面
- 嵌入式图像支持 - 图像取出和转换
- CLI 批量模式 - 命令线接口
捐款
欢迎捐款! 请看我们的 捐款指南。 。 。 .
贡献进程
- 叉头 存储器
- 创建 特性分支( e)
git checkout -b feature/amazing-feature(中文(简体) ) - 提交 更改( c)
git commit -m 'Add amazing feature'(中文(简体) ) - 推开 对分支机构(
git push origin feature/amazing-feature(中文(简体) ) - 打开 a 拉动请求
许可证
这个项目的许可证根据 麻省理工学院 - 看这个 自由主义 详细文件.
承认
++ 辅助
- 问题数字 : GitHub 问题
- 文档数字 : GitHub 仓库
- 电子邮件数字 : 联系人@aidarinfo.com
简单、快和可靠的 DOCX 到 Markdown 转换 ⚡