You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

markdownlint通过pre-commit误删快捷链接,求定位对应规则

问题诊断:markdownlint自动移除快捷链接定义的原因

背景

使用MkDocs信息框语法配合快捷链接编写内容,原始test.md内容如下:

??? infobox "example"

    example shortcut link --> [Buuntu/fastapi-react]

[Buuntu/fastapi-react]: https://github.com/Buuntu/fastapi-react

通过pre-commit执行带--fix参数的markdownlint后,底部的链接定义被自动删除,仅保留信息框内的链接标记。修改MD053规则后无效果,需明确导致该问题的规则。

问题根源

导致链接定义被移除的是**MD042(no-unused-definitions)**规则。

markdownlint默认会扫描文档中未被引用的链接定义,在--fix模式下自动删除这类定义。但它的默认解析器不识别MkDocs特有的信息框(??? infobox)语法,会将信息框内的内容视为未解析文本,因此判定[Buuntu/fastapi-react]这个链接定义未被实际引用,触发规则将其删除。

你之前修改MD053规则无效,是因为MD053负责的是链接定义的样式(比如是否允许快捷、折叠式链接),和未引用定义的清理逻辑完全无关。

解决方案

在.markdownlint.jsonc配置文件中,针对MD042规则做以下调整之一即可:

  1. 直接禁用MD042规则:
{
  "default": true,
  "MD003": { "style": "atx" },
  "MD007": { "indent": 4 },
  "MD013": {
    "line_length": 80,
    "heading_line_length": 80,
    "code_block_line_length": 80,
    "code_blocks": true,
    "tables": true,
    "headings": true,
    "headers": true,
    "strict": false,
    "stern": false
  },
  "no-hard-tabs": false,
  "whitespace": false,
  "MD042": false,
  "MD053": {
    "ignored_definitions": ["full", "collapsed", "shortcut"]
  }
}
  1. 忽略特定的链接定义(适合仅需保留个别未被识别的链接场景):
{
  "default": true,
  "MD003": { "style": "atx" },
  "MD007": { "indent": 4 },
  "MD013": {
    "line_length": 80,
    "heading_line_length": 80,
    "code_block_line_length": 80,
    "code_blocks": true,
    "tables": true,
    "headings": true,
    "headers": true,
    "strict": false,
    "stern": false
  },
  "no-hard-tabs": false,
  "whitespace": false,
  "MD042": {
    "ignored_definitions": ["Buuntu/fastapi-react"]
  },
  "MD053": {
    "ignored_definitions": ["full", "collapsed", "shortcut"]
  }
}

内容的提问来源于stack exchange,提问作者willwrighteng

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 05:05:37