介绍
# Markdown-Formatter - Beautify Your Markdown
**Vernox 实用技能 - 让您的 Markdown 看起来更专业。**
## 概述
Markdown-Formatter 是一个功能强大的工具,用于格式化、检查和美化 Markdown 文档。支持多种风格指南(CommonMark、GitHub Flavored Markdown、自定义规则),可处理从简单清理到复杂重新格式化的所有工作。
## 功能
### ✅ 格式化引擎 - 多种风格指南(CommonMark、GitHub、自定义) - 保留文档结构 - 处理嵌套列表、代码块、表格 - 可配置的行宽和缩进 - 智能标题规范化 - 链接引用优化
### ✅ 检查与清理 - 删除尾随空格 - 规范化行尾(LF 与 CRLF) - 修复不一致的列表标记 - 删除末尾的空行 - 修复多个连续空行
### ✅ 美化 - 改进标题层级 - 优化列表格式 - 使用适当的间距格式化代码块 - 在配置的宽度处换行 - 在强调符号周围添加适当的间距
### ✅ 验证 - 检查 Markdown 语法有效性 - 报告检查错误 - 提出改进建议 - 验证链接和引用
## 安装
```bash clawhub install markdown-formatter ```
## 快速开始
### 格式化文档
```javascript const result = await formatMarkdown({ markdown: '# My Document\n\n\n## Section 1\nContent here...', style: 'github', options: { maxWidth: 80, headingStyle: 'atx' } });
console.log(result.formattedMarkdown); ```
### 美化多个文件
```javascript const results = await formatBatch({ markdownFiles: ['./doc1.md', './doc2.md', './README.md'], style: 'github', options: { wrapWidth: 80 } });
results.forEach(result => { console.log(`${result.file}: ${result.warnings} warnings`); }); ```
### 检查并修复
```javascript const result = await lintMarkdown({ markdown: '# My Document\n\n\nBad list\n\n- item 1\n- item 2', style: 'github' });
console.log(`Errors found: ${result.errors}`); console.log(`Fixed: ${result.fixed}`); ```
## 工具函数
### `formatMarkdown` 根据风格指南格式化 Markdown 内容。
**参数:** - `markdown` (字符串,必填): 要格式化的 Markdown 内容 - `style` (字符串,必填): 风格指南名称 ('commonmark', 'github', 'commonmark', 'custom') - `options` (对象,可选): - `maxWidth` (数字): 自动换行宽度 (默认: 80) - `headingStyle` (字符串): 'atx' | 'setext' | 'underlined' | 'consistent' (默认: 'atx') - `listStyle` (字符串): 'consistent' | 'dash' | 'asterisk' | 'plus' (默认: 'consistent') - `codeStyle` (字符串): 'fenced' | 'indented' (默认: 'fenced') - `emphasisStyle` (字符串): 'underscore' | 'asterisk' (默认: 'asterisk') - `strongStyle` (字符串): 'asterisk' | 'underline' (默认: 'asterisk') - `linkStyle` (字符串): 'inline' | 'reference' | 'full' (默认: 'inline') - `preserveHtml` (布尔值): 保持 HTML 不变 (默认: false) - `fixLists` (布尔值): 修复不一致的列表标记 (默认: true) - `normalizeSpacing` (布尔值): 修复格式符号周围的间距 (默认: true)
**返回:** - `formattedMarkdown` (字符串): 格式化后的 Markdown - `warnings` (数组): 警告消息数组 - `stats` (对象): 格式化统计信息 - `lintResult` (对象): 检查错误和修复 - `originalLength` (数字): 原始字符数 - `formattedLength` (数字): 格式化后的字符数
### `formatBatch` 一次格式化多个 Markdown 文件。
**参数:** - `markdownFiles` (数组,必填): 文件路径数组 - `style` (字符串): 风格指南名称 - `options` (对象,可选): 与 formatMarkdown 选项相同
**返回:** - `results` (数组): 格式化结果数组 - `totalFiles` (数字): 处理的文件数量 - `totalWarnings` (数字): 所有文件的总警告数 - `processingTime` (数字): 耗时(毫秒)
### `lintMarkdown` 检查 Markdown 问题而不进行格式化。
**参数:** - `markdown` (字符串,必填): 要检查的 Markdown 内容 - `style` (字符串): 风格指南名称 - `options` (对象,可选): 额外的检查选项 - `checkLinks` (布尔值): 验证链接 (默认: true) - `checkHeadingLevels` (布尔值): 检查标题层级 (默认: true) - `checkListConsistency` (布尔值): 检查列表标记一致性 (默认: true) - `checkEmphasisBalance` (布尔值): 检查强调符号配对 (默认: false)
**返回:** - `errors` (数组): 错误对象数组 - `warnings` (数组): 警告对象数组 - `stats` (对象): 检查统计信息 - `suggestions` (数组): 建议的修复
## 风格指南
### CommonMark (默认) - 标准 CommonMark 规范 - ATX 标题 (ATX 样式) - 引用式链接 [text] - 下划线强调 - 星号强调
### GitHub Flavored Markdown - 使用 \`\`\` 的围栏代码块 - 使用竖线的表格 - 带有 x 的任务列表 [ ] - 删除线 `~~text~~` - 使用 <https://url> 的自动链接
### Consistent (默认) - 一致的 ATX 标题层级 - 一致的列表标记 - 一致的强调样式 - 一致的代码块样式
### Custom - 用户定义的规则 - 基于正则表达式的转换 - 自定义标题样式
## 使用场景
### 文档清理 - 修复 README 文件中不一致的格式 - 规范化标题样式 - 修复列表标记 - 清理多余的空白字符
### 内容创作 - 使用一致的样式格式化文章 - 在发布前美化博客文章 - 确保标题层级一致
### 技术写作 - 格式化代码文档 - 美化 API 规范 - 清理来自 LLM 的混乱 Markdown
### README 生成 - 格式化和美化项目 README 文件 - 确保结构一致 - 开源项目的专业外观
### Markdown 转换 - 将 HTML 转换为 Markdown - 从一种样式重新格式化为另一种样式 - 从其他格式提取并格式化 Markdown
## 配置
### 编辑 `config.json`: ```json { "defaultStyle": "github", "maxWidth": 80, "headingStyle": "atx", "listStyle": "consistent", "codeStyle": "fenced", "emphasisStyle": "asterisk", "linkStyle": "inline", "customRules": [], "linting": { "checkLinks": true, "checkHeadingLevels": true, "checkListConsistency": true } } ```
## 示例
### 简单格式化 ```javascript const result = await formatMarkdown({ markdown: '# My Title\n\n\nThis is content.', style: 'github' });
console.log(result.formattedMarkdown); ```
### 复杂美化 ```javascript const result = await formatMarkdown({ markdown: '# Header 1\n## Header 2\n\nParagraph...', style: 'github', options: { fixLists: true, normalizeSpacing: true, wrapWidth: 80 } });
console.log(result.formattedMarkdown); ```
### 检查与修复 ```javascript const result = await lintMarkdown({ markdown: '# Title\n\n- Item 1\n- Item 2\n\n## Section 2', style: 'github' });
console.log(`Errors: ${result.errors.length}`); result.errors.forEach(err => { console.log(` - ${err.message} at line ${err.line}`); });
// Fix automatically const fixed = await formatMarkdown({ markdown: result.fixed, style: 'github' }); ```
### 批量处理 ```javascript const results = await formatBatch({ markdownFiles: ['./doc1.md', './doc2.md', './README.md'], style: 'github' });
console.log(`Processed ${results.totalFiles} files`); console.log(`Total warnings: ${results.totalWarnings}`); ```
## 性能
### 速度 - **小型文档** (<1000 字): <50ms - **中型文档** (1000-5000 字): 50-200ms - **大型文档** (5000+ 字): 200-500ms
### 准确性 - **结构保留率:** 100% - **风格指南合规率:** 95%+ - **空白规范化率:** 100%
## 错误处理
### 输入无效 - 清晰的错误消息 - 建议检查文件路径 - 格式化前验证 Markdown 内容
### Markdown 解析错误 - 清晰报告解析问题 - 建议手动修复 - 错误时的优雅降级
### 文件 I/O 错误 - 包含文件路径的清晰错误 - 检查文件是否存在 - 建议修复权限 - 批处理时遇到错误继续执行
## 故障排除
### 格式未应用 - 检查样式是否正确 - 验证选项是否生效 - 检查是否有冲突规则 - 使用简单示例进行测试
### 检查显示过多错误 - 某些错误是风格选择,并非真正的问题 - 考虑禁用特定检查 - 使用自定义规则满足特定需求
## 提示
### 最佳效果 - 使用一致的风格指南 - 启用 fixLists、normalizeSpacing 选项 - 为您的输出介质设置适当的 maxWidth - 先在小样本上进行测试
### 性能优化 - 分批处理大文件 - 禁用未使用的检查 - 对常见模式使用更简单的规则
## 许可证
MIT
---
**格式化 Markdown。让您的文档保持美观。** 🔮