@aidalinfo/office-to-markdown

中文文档
1.0.2发布于 2025/08/27MITTypeScriptES Module近 30 天下载 2847

用于将Office文档(DOCX)转换为Markdown格式的现代TypeScript库,通过增强表支持和数学等式转换,为 Bun 运行时优化.

中文使用文档
文档目录34 节
  1. * 特性
  2. * 安装
  3. 使用 Bun( 建议)
  4. 以npm/yarn/pnpm计
  5. 所需依赖
  6. * 转换工作流程
  7. 简单使用
  8. 基本转换
  9. 高级 API
  10. 从不同来源转换
  11. QQ 配置选项
  12. 技术结构
  13. 模块结构
  14. 转换管道
  15. 数学方程式处理
  16. 支持的数学元素
  17. 高级使用示例
  18. 处理错误
  19. 能力检查
  20. 使用节点js
  21. 测试和验证
  22. 测试结果
  23. 业绩
  24. 发展
  25. 先决条件
  26. 开发安装
  27. 可用的脚本
  28. 测试
  29. * 路线图
  30. 捐款
  31. 贡献进程
  32. 许可证
  33. 承认
  34. ++ 辅助

@ aidalinfo/ 办公室到标记下

npm version TypeScript Bun

一个现代化的TypeScript库,用于将Office文档(DOCX)转换为Markdown格式,在高级支持数学方程式和表格的情况下,为Bun生态系统进行了优化.

通过微软的逆向工程创建 标记下方 - TypeScript的再执行,将Python的文档转换能力带入JavaScript/Bun生态系统,增强性能和类型安全.

* 特性

  • ✅ DOCX 到 Markdown 转换 结构保护
  • ✅ 数学等式支持 (OMML ~ LaTeX) (中文(简体) )
  • ✅ 表格处理 自动格式化
  • ✅ 样式保存 (粗体、斜体、标题)
  • ✅ 图像处理 带备选案文文本
  • ✅ 简单而高级的 API 用于不同用途的情况
  • ✅ 强力错误处理 带有特定错误代码
  • ✅ 最佳业绩 用 Bun 运行时间
  • ✅ 完整类型脚本类型 用于更好的 DX

* 安装

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)

* 转换工作流程

转换过程遵循这些步骤:

  1. 文件检测 - MIME类型和扩展核查
  2. 预处理 - DOCX内容提取和修改
  3. 数学处理 - OMML — LaTeX 转换
  4. 主要转换 - DOCX – HTML 通过长毛象
  5. 后处理 - 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'标记下行向样式
preserveTablesbooleantrue保留表格
convertMathbooleantrue转换数学等式
styleMapstring- 说吧自定义长毛图映射

技术结构

模块结构

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

转换管道

  1. 文件检测 - MIME类型和扩展核查
  2. 预处理 - DOCX内容提取和修改
  3. 数学处理 - OMML — LaTeX 转换
  4. 主要转换 - DOCX – HTML 通过长毛象
  5. 后处理 - 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 批量模式 - 命令线接口

捐款

欢迎捐款! 请看我们的 捐款指南。 。 。 .

贡献进程

  1. 叉头 存储器
  2. 创建 特性分支( e)git checkout -b feature/amazing-feature(中文(简体) )
  3. 提交 更改( c)git commit -m 'Add amazing feature'(中文(简体) )
  4. 推开 对分支机构(git push origin feature/amazing-feature(中文(简体) )
  5. 打开 a 拉动请求

许可证

这个项目的许可证根据 麻省理工学院 - 看这个 自由主义 详细文件.

承认

++ 辅助


@ aidarinfo/ 办公室对标记

简单、快和可靠的 DOCX 到 Markdown 转换 ⚡

版本与发布记录

@aidalinfo/office-to-markdown 共发布 3 个版本,首次发布于 2025/08/11;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.2latest2025/08/271.2 年前
1.0.12025/08/111.2 年前
1.0.02025/08/111.2 年前

关于 @aidalinfo/office-to-markdown 的常见问题

@aidalinfo/office-to-markdown 是什么?

用于将Office文档(DOCX)转换为Markdown格式的现代TypeScript库,通过增强表支持和数学等式转换,为 Bun 运行时优化.

如何安装 @aidalinfo/office-to-markdown?

在项目目录运行 npm install @aidalinfo/office-to-markdown,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:typescript(>=4.5.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@aidalinfo/office-to-markdown 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aidalinfo/office-to-markdown 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@aidalinfo/office-to-markdown 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@aidalinfo/office-to-markdown 可以商用吗?

@aidalinfo/office-to-markdown 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@aidalinfo/office-to-markdown 的下载量有多少?

根据 npm 官方下载统计,@aidalinfo/office-to-markdown 在 2026/10/05 – 2026/11/03 这 30 天内被下载 2,847 次,日均约 95 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aidalinfo/office-to-markdown 还在维护吗?

最新版本 1.0.2 发布于 2025/08/27(1.2 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/08/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。