Loading...
Loading...
Use when setting up or running markdown formatting and linting in a repository, or when encountering markdownlint errors (MD013, MD040, MD060) or horizontal rule violations. Triggers on「格式化 markdown」「设置 markdown lint」「markdown 检查」「设置 pre-commit」「检查 md 格式」「markdownlint 报错」
npx skill4agent add niracler/skill markdown-lint| 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.
npx markdownlint-cli2 file.md# 已有配置?跳到「检查/修复」
ls .markdownlint.json .pre-commit-config.yaml 2>/dev/null.markdownlint.json{
"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.yamlrepos:
- 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.shchmod +xWindows 用户:脚本需要在 Git Bash 或 WSL 中运行。.sh
.gitignorenode_modules/
.mypy_cache/
__pycache__/------# 找出违规文件
bash scripts/check-horizontal-rules.sh $(find . -name '*.md' -not -path './node_modules/*')
# 提取违规文件列表(仅匹配 .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)
# 批量移除(保留 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 必须用过滤,否则脚本末尾的 "Error: ..." 错误消息会被当成文件名。\.md:[0-9]+:
# 单仓库
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"--fix```pythonbashyamltext.markdownlint.json# 按规则统计
npx markdownlint-cli2 "**/*.md" "#repos" "#node_modules" 2>&1 \
| 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 -10pre-commit install# 两项检查全部通过(monorepo 加 "#repos" 排除子仓库)
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,应被拦截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 缺少代码块语言,加 |