Skip to content

Markdown 与其他格式转换

Markdown 作为一种轻量级标记语言,其优势之一是可以轻松转换为其他格式。在本章中,我们将介绍 Markdown 与其他格式的转换方法。

Markdown 转 HTML

命令行工具

Pandoc

Pandoc 是一个功能强大的文档转换工具,支持 Markdown 转换为 HTML。

bash
# 基本转换
pandoc input.md -o output.html

# 转换为带样式的 HTML
pandoc input.md -o output.html --css style.css

# 转换为自包含的 HTML(内联 CSS)
pandoc input.md -o output.html --self-contained

marked

marked 是一个 Node.js 库,用于将 Markdown 转换为 HTML。

bash
# 安装
npm install -g marked

# 转换
marked input.md > output.html

markdown-it

markdown-it 是一个功能强大的 Markdown 解析器,支持插件扩展。

javascript
const markdownIt = require('markdown-it');
const fs = require('fs');

const md = markdownIt();
const input = fs.readFileSync('input.md', 'utf8');
const output = md.render(input);

fs.writeFileSync('output.html', output);

在线工具

编辑器转换

许多 Markdown 编辑器都支持导出为 HTML:

  • VS Code:使用 "Markdown Preview Enhanced" 插件
  • Typora:文件 -> 导出 -> HTML
  • Atom:使用 "markdown-preview-plus" 插件

Markdown 转 PDF

命令行工具

Pandoc

Pandoc 可以将 Markdown 转换为 PDF,但需要安装 LaTeX。

bash
# 转换为 PDF
pandoc input.md -o output.pdf

# 使用模板
pandoc input.md -o output.pdf --template eisvogel

wkhtmltopdf

wkhtmltopdf 可以将 HTML 转换为 PDF,结合 marked 使用。

bash
# 安装
npm install -g marked wkhtmltopdf

# 转换
marked input.md | wkhtmltopdf - output.pdf

在线工具

编辑器转换

许多 Markdown 编辑器都支持导出为 PDF:

  • Typora:文件 -> 导出 -> PDF
  • VS Code:使用 "Markdown PDF" 插件
  • Atom:使用 "markdown-pdf" 插件

Markdown 转 Word

命令行工具

Pandoc

Pandoc 可以将 Markdown 转换为 Word 文档。

bash
# 转换为 Word
pandoc input.md -o output.docx

# 使用参考文档
pandoc input.md -o output.docx --reference-doc=template.docx

在线工具

编辑器转换

一些 Markdown 编辑器支持导出为 Word:

  • Typora:文件 -> 导出 -> Word
  • VS Code:使用 "Markdown to Word" 插件

其他格式转 Markdown

HTML 转 Markdown

命令行工具

Pandoc

Pandoc 可以将 HTML 转换为 Markdown。

bash
# 转换 HTML 为 Markdown
pandoc input.html -o output.md

html2md

html2md 是一个专门将 HTML 转换为 Markdown 的工具。

bash
# 安装
npm install -g html2md

# 转换
html2md input.html > output.md

Word 转 Markdown

命令行工具

Pandoc

Pandoc 可以将 Word 文档转换为 Markdown。

bash
# 转换 Word 为 Markdown
pandoc input.docx -o output.md

mammoth

mammoth 是一个专门将 Word 文档转换为 HTML 或 Markdown 的工具。

bash
# 安装
npm install -g mammoth

# 转换
mammoth input.docx output.md

PDF 转 Markdown

PDF 转 Markdown 比较复杂,因为 PDF 是一种二进制格式,需要先提取文本。

命令行工具

pdftotext + pandoc

bash
# 安装
apt-get install poppler-utils  # Ubuntu/Debian
brew install poppler          # macOS

# 提取文本
pdftotext input.pdf output.txt

# 转换为 Markdown
pandoc output.txt -o output.md

在线工具

批量转换

命令行批量转换

使用 bash 脚本

bash
#!/bin/bash

# 批量将 Markdown 转换为 HTML
for file in *.md; do
  pandoc "$file" -o "${file%.md}.html"
done

使用 Node.js 脚本

javascript
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

// 批量转换 Markdown 文件
const files = fs.readdirSync('.').filter(file => path.extname(file) === '.md');

files.forEach(file => {
  const output = path.join(path.dirname(file), path.basename(file, '.md') + '.html');
  execSync(`pandoc "${file}" -o "${output}"`);
  console.log(`Converted ${file} to ${output}`);
});

自动化工具

  • GitHub Actions:使用 GitHub Actions 自动转换 Markdown 文件
  • Jenkins:使用 Jenkins 自动化构建流程中转换 Markdown 文件
  • Gulp:使用 Gulp 自动化工作流中转换 Markdown 文件

最佳实践

转换前准备

  • 检查语法:确保 Markdown 语法正确
  • 清理内容:移除不必要的内容和格式
  • 备份原始文件:在转换前备份原始 Markdown 文件

转换后处理

  • 检查输出:检查转换后的文件是否正确
  • 调整格式:根据需要调整转换后的格式
  • 测试兼容性:测试转换后的文件在目标平台上的显示效果

工具选择

  • 简单转换:使用在线工具或编辑器内置功能
  • 复杂转换:使用 Pandoc 等命令行工具
  • 批量转换:使用脚本或自动化工具

小结

Markdown 与其他格式的转换是 Markdown 的重要优势之一,通过使用适当的工具和方法,你可以轻松地在 Markdown 和其他格式之间转换。无论是转换为 HTML、PDF、Word,还是从其他格式转换为 Markdown,都有相应的工具和方法。

在接下来的附录中,我们将介绍项目实战练习,帮助你通过实践掌握 Markdown 的使用技巧。