markdown-lint
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseMarkdown Lint
Markdown Lint
为仓库配置 markdown 格式检查(markdownlint + 水平线禁止)和 pre-commit hook。
为仓库配置Markdown格式检查(markdownlint + 禁止使用水平线)和pre-commit hook。
Prerequisites
前置条件
| Tool | Type | Required | Install |
|---|---|---|---|
| Node.js | cli | Yes | |
| markdownlint-cli2 | cli | Yes | |
| pre-commit | cli | No | |
Do NOT proactively verify these tools on skill load. If a command fails due to a missing tool, directly guide the user through installation and configuration step by step.
| 工具 | 类型 | 是否必需 | 安装方式 |
|---|---|---|---|
| Node.js | 命令行工具 | 是 | |
| markdownlint-cli2 | 命令行工具 | 是 | |
| pre-commit | 命令行工具 | 否 | |
加载本技能时无需主动验证这些工具。如果命令因缺少工具而失败,直接逐步引导用户完成安装和配置。
When to Use
使用场景
- 新仓库初始化:第一次为仓库添加 markdown 格式标准
- 检查/修复:运行格式检查或批量修复现有文件
- 迁移:将格式标准复制到另一个仓库
不适用:
- 单文件检查:直接运行 ,不需要走 setup 流程
npx markdownlint-cli2 file.md - 文章内容审校:使用 writing-proofreading skill(步骤 6 会引用本 skill)
- 新仓库初始化:首次为仓库添加Markdown格式标准
- 检查/修复:运行格式检查或批量修复现有文件
- 迁移:将格式标准复制到另一个仓库
不适用场景:
- 单文件检查:直接运行 ,无需走配置流程
npx markdownlint-cli2 file.md - 文章内容审校:使用writing-proofreading技能(步骤6会引用本技能)
Setup 流程
配置流程
1. 检查仓库状态
1. 检查仓库状态
bash
undefinedbash
undefined已有配置?跳到「检查/修复」
已有配置?跳到「检查/修复」环节
ls .markdownlint.json .pre-commit-config.yaml 2>/dev/null
undefinedls .markdownlint.json .pre-commit-config.yaml 2>/dev/null
undefined2. 创建配置文件
2. 创建配置文件
.markdownlint.jsonjson
{
"default": true,
"MD013": false,
"MD024": { "siblings_only": true },
"MD033": false,
"MD035": false,
"MD036": false,
"MD041": false,
"MD060": false
}关闭规则说明:
| 规则 | 理由 |
|---|---|
| MD013 | CJK 文本和表格不适合行长度限制 |
| MD033 | 允许 |
| MD035 | 水平线完全禁止,由独立脚本检查 |
| MD036 | 允许 bold 作视觉标题 |
| MD041 | YAML frontmatter 导致首行非标题 |
| MD060 | 表格管道符间距样式过于严格 |
按需追加禁用(根据仓库内容酌情添加):
| 规则 | 场景 | 理由 |
|---|---|---|
| MD025 | 仓库含导入/vendor 文档 | 协议文档常有多个 H1 |
| MD045 | 仓库含导入/vendor 文档 | vendor 文档图片无 alt text |
| MD051 | CJK 锚点链接 | |
| MD056 | 复杂参考表格 | 合并行/变长列的表格触发误报 |
.pre-commit-config.yamlyaml
repos:
- repo: https://github.com/DavidAnson/markdownlint-cli2
rev: v0.20.0 # 运行 pre-commit autoupdate 获取最新
hooks:
- id: markdownlint-cli2
- repo: local
hooks:
- id: no-horizontal-rules
name: no horizontal rules outside frontmatter
entry: scripts/check-horizontal-rules.sh
language: script
types: [markdown]创建后必须运行,上面的 rev 可能已过时。pre-commit autoupdate
scripts/check-horizontal-rules.sh从 scripts/check-horizontal-rules.sh 复制,然后 。
chmod +xWindows 用户:脚本需要在 Git Bash 或 WSL 中运行。.sh
.gitignoretext
node_modules/
.mypy_cache/
__pycache__/.markdownlint.jsonjson
{
"default": true,
"MD013": false,
"MD024": { "siblings_only": true },
"MD033": false,
"MD035": false,
"MD036": false,
"MD041": false,
"MD060": false
}关闭规则说明:
| 规则 | 理由 |
|---|---|
| MD013 | CJK文本和表格不适合行长度限制 |
| MD033 | 允许使用 |
| MD035 | 完全禁止水平线,由独立脚本检查 |
| MD036 | 允许使用粗体作为视觉标题 |
| MD041 | YAML前置元数据导致首行非标题 |
| MD060 | 表格管道符间距规则过于严格 |
按需追加禁用规则(根据仓库内容酌情添加):
| 规则 | 场景 | 理由 |
|---|---|---|
| MD025 | 仓库包含导入/第三方文档 | 协议文档通常有多个H1标题 |
| MD045 | 仓库包含导入/第三方文档 | 第三方文档图片无alt文本 |
| MD051 | CJK锚点链接 | |
| MD056 | 复杂参考表格 | 合并行/变长列的表格会触发误报 |
.pre-commit-config.yamlyaml
repos:
- repo: https://github.com/DavidAnson/markdownlint-cli2
rev: v0.20.0 # 运行pre-commit autoupdate获取最新版本
hooks:
- id: markdownlint-cli2
- repo: local
hooks:
- id: no-horizontal-rules
name: no horizontal rules outside frontmatter
entry: scripts/check-horizontal-rules.sh
language: script
types: [markdown]创建后必须运行,上述rev版本可能已过时。pre-commit autoupdate
scripts/check-horizontal-rules.sh从 scripts/check-horizontal-rules.sh 复制脚本,然后执行赋予执行权限。
chmod +xWindows 用户:脚本需要在Git Bash或WSL环境中运行。.sh
.gitignoretext
node_modules/
.mypy_cache/
__pycache__/3. 移除现有 ---
分隔线
---3. 移除现有---
分隔线
---保留 YAML frontmatter 的 ,删除其余所有水平线:
---bash
undefined保留YAML前置元数据中的,删除其余所有水平线:
---bash
undefined找出违规文件
找出违规文件
bash scripts/check-horizontal-rules.sh $(find . -name '.md' -not -path './node_modules/')
bash scripts/check-horizontal-rules.sh $(find . -name '.md' -not -path './node_modules/')
提取违规文件列表(仅匹配 .md: 格式的行,避免捕获错误消息)
提取违规文件列表(仅匹配.md:格式的行,避免捕获错误消息)
files=$(bash scripts/check-horizontal-rules.sh
$(find . -name '.md' -not -path './node_modules/') 2>&1
| grep -E '.md:[0-9]+:' | grep -oE '^[^:]+' | sort -u)
$(find . -name '.md' -not -path './node_modules/') 2>&1
| grep -E '.md:[0-9]+:' | grep -oE '^[^:]+' | sort -u)
files=$(bash scripts/check-horizontal-rules.sh
$(find . -name '.md' -not -path './node_modules/') 2>&1
| grep -E '.md:[0-9]+:' | grep -oE '^[^:]+' | sort -u)
$(find . -name '.md' -not -path './node_modules/') 2>&1
| grep -E '.md:[0-9]+:' | grep -oE '^[^:]+' | sort -u)
批量移除(保留 frontmatter)
批量移除(保留前置元数据)
for file in $files; do
awk 'NR == 1 && /^---[[:space:]]$/ { print; fm = 1; next } fm && /^---[[:space:]]$/ { print; fm = 0; next } !fm && /^[[:space:]][-][[:space:]][-][[:space:]][-][-* ]*$/ { next } { print }' "$file" > "$file.tmp" && mv "$file.tmp" "$file"
echo "Fixed: $file"
done
> **注意**:grep 必须用 `\.md:[0-9]+:` 过滤,否则脚本末尾的 "Error: ..." 错误消息会被当成文件名。for file in $files; do
awk 'NR == 1 && /^---[[:space:]]$/ { print; fm = 1; next } fm && /^---[[:space:]]$/ { print; fm = 0; next } !fm && /^[[:space:]][-][[:space:]][-][[:space:]][-][-* ]*$/ { next } { print }' "$file" > "$file.tmp" && mv "$file.tmp" "$file"
echo "Fixed: $file"
done
> **注意**:必须使用`grep -E '\.md:[0-9]+:'`过滤,否则脚本末尾的"Error: ..."错误消息会被当作文件名处理。4. 格式化全部文件
4. 格式化全部文件
bash
undefinedbash
undefined单仓库
单仓库
npx markdownlint-cli2 --fix "/*.md"
npx markdownlint-cli2 "/*.md"
npx markdownlint-cli2 --fix "/*.md"
npx markdownlint-cli2 "/*.md"
monorepo(排除子仓库和依赖目录)
单体仓库(排除子仓库和依赖目录)
npx markdownlint-cli2 --fix "/*.md" "#repos" "#node_modules"
npx markdownlint-cli2 "/*.md" "#repos" "#node_modules"
undefinednpx markdownlint-cli2 --fix "/*.md" "#repos" "#node_modules"
npx markdownlint-cli2 "/*.md" "#repos" "#node_modules"
undefined4a. 处理无法自动修复的错误
4a. 处理无法自动修复的错误
--fix- MD040(代码块缺少语言):给 加上语言标识(
```、python、bash、yaml等)text - MD025/MD045/MD051/MD056:如果大量出现在 vendor 文档中,在 中禁用对应规则
.markdownlint.json
先分析错误分布再决定策略:
bash
undefined--fix- MD040(代码块缺少语言标识):为添加语言标识(如
```、python、bash、yaml等)text - MD025/MD045/MD051/MD056:如果大量出现在第三方文档中,在中禁用对应规则
.markdownlint.json
先分析错误分布再决定处理策略:
bash
undefined按规则统计
按规则统计错误数量
npx markdownlint-cli2 "**/*.md" "#repos" "#node_modules" 2>&1
| grep -oE 'MD[0-9]+' | sort | uniq -c | sort -rn
| grep -oE 'MD[0-9]+' | sort | uniq -c | sort -rn
npx markdownlint-cli2 "**/*.md" "#repos" "#node_modules" 2>&1
| grep -oE 'MD[0-9]+' | sort | uniq -c | sort -rn
| grep -oE 'MD[0-9]+' | sort | uniq -c | sort -rn
按文件统计
按文件统计错误数量
npx markdownlint-cli2 "**/*.md" "#repos" "#node_modules" 2>&1
| grep -oE '^[^:]+' | sort | uniq -c | sort -rn | head -10
| grep -oE '^[^:]+' | sort | uniq -c | sort -rn | head -10
undefinednpx markdownlint-cli2 "**/*.md" "#repos" "#node_modules" 2>&1
| grep -oE '^[^:]+' | sort | uniq -c | sort -rn | head -10
| grep -oE '^[^:]+' | sort | uniq -c | sort -rn | head -10
undefined5. 安装 hook
5. 安装钩子
bash
pre-commit installbash
pre-commit install6. 验证
6. 验证
bash
undefinedbash
undefined两项检查全部通过(monorepo 加 "#repos" 排除子仓库)
两项检查全部通过(单体仓库添加"#repos"排除子仓库)
npx markdownlint-cli2 "**/.md" "#repos" "#node_modules"
bash scripts/check-horizontal-rules.sh $(find . -name '.md' -not -path './repos/' -not -path './node_modules/')
npx markdownlint-cli2 "**/.md" "#repos" "#node_modules"
bash scripts/check-horizontal-rules.sh $(find . -name '.md' -not -path './repos/' -not -path './node_modules/')
测试 hook: 故意加 --- 到某 md 文件,git add + commit,应被拦截
测试钩子:故意在某个md文件中添加---,执行git add + commit,应被拦截
undefinedundefined检查/修复(已有配置的仓库)
检查/修复(已有配置的仓库)
bash
npx markdownlint-cli2 "**/*.md" "#repos" "#node_modules" # 检查
npx markdownlint-cli2 --fix "**/*.md" "#repos" "#node_modules" # 自动修复
bash scripts/check-horizontal-rules.sh $(find . -name '*.md' -not -path './repos/*' -not -path './node_modules/*')单仓库项目去掉即可。"#repos"
bash
npx markdownlint-cli2 "**/*.md" "#repos" "#node_modules" # 检查
npx markdownlint-cli2 --fix "**/*.md" "#repos" "#node_modules" # 自动修复
bash scripts/check-horizontal-rules.sh $(find . -name '*.md' -not -path './repos/*' -not -path './node_modules/*')单仓库项目去掉即可。"#repos"
常见问题
常见问题
| 问题 | 原因 | 修复 |
|---|---|---|
| 未安装 | |
| markdownlint 大量 MD060 错误 | 表格管道符间距 | |
| 大量 MD056 错误 | vendor 文档复杂表格 | |
| 大量 MD051 错误 | CJK 锚点链接 | |
| Windows 不原生支持 Bash | 使用 Git Bash 或 WSL |
frontmatter | awk 脚本问题 | 确认文件第 1 行是 |
批量删除 HR 产生垃圾 | grep 捕获了脚本错误消息作为文件名 | 用 |
| 部分规则无法自动修复 | 手动修复(通常是 MD040 缺少代码块语言,加 |
| 问题 | 原因 | 修复方案 |
|---|---|---|
| 未安装pre-commit | |
| markdownlint出现大量MD060错误 | 表格管道符间距不符合规则 | 在 |
| 出现大量MD056错误 | 第三方文档包含复杂表格 | 在 |
| 出现大量MD051错误 | 使用了CJK锚点链接 | 在 |
| Windows不原生支持Bash | 使用Git Bash或WSL环境 |
前置元数据 | awk脚本逻辑问题 | 确认文件第1行是 |
批量删除水平线产生垃圾 | grep捕获了脚本错误消息作为文件名 | 使用 |
| 部分规则无法自动修复 | 手动修复(通常是MD040缺少代码块语言标识,添加 |