AgentSkillsCN

markdownlint

使用 markdownlint-cli2 对 Markdown 文件进行语法检查与修复。适用于创建、编辑或审阅 Markdown (.md) 文件时,或当用户要求对 Markdown 进行语法检查或修复时,亦或是需要核查 Markdown 的质量和一致性时使用。该工具可自动运行 linter、解读错误信息、应用自动修复、配置规则,并支持内联指令的使用。

SKILL.md
--- frontmatter
name: markdownlint
description: Lint and fix Markdown files using markdownlint-cli2. Use when creating, editing, or reviewing Markdown (.md) files, when the user asks to lint or fix markdown, or when checking markdown quality and consistency. Handles running the linter, interpreting errors, applying auto-fixes, configuring rules, and using inline directives.

Markdownlint

Lint Markdown files for style and consistency using markdownlint via the markdownlint-cli2 CLI.

Running the linter

Lint specific files or directories:

bash
npx markdownlint-cli2 "docs/**/*.md"

Lint and auto-fix fixable issues:

bash
npx markdownlint-cli2 --fix "docs/**/*.md"

Lint multiple paths:

bash
npx markdownlint-cli2 "docs/**/*.md" "README.md" "CHANGELOG.md"

Exclude paths with # (negation):

bash
npx markdownlint-cli2 "**/*.md" "#node_modules"

Exit codes

  • 0: No errors (warnings may exist)
  • 1: Linting errors found
  • 2: Linting failed (e.g., bad config)

Interpreting output

Output format: filepath:line rule/alias description

Example:

text
docs/guide.md:15 MD022/blanks-around-headings [Expected: 1; Actual: 0; Below]
docs/guide.md:42 MD034/no-bare-urls Bare URL used [Context: "..."]

Configuration

Project config file .markdownlint.json

json
{
  "default": true,
  "MD013": false,
  "MD033": { "allowed_elements": ["details", "summary", "br"] },
  "MD024": { "siblings_only": true }
}

Rules can be set to:

  • true or "error" — enable as error
  • "warning" — enable as warning
  • false — disable
  • { ... } — enable with parameters

CLI config file .markdownlint-cli2.jsonc

Controls both rules and CLI behavior:

jsonc
{
  "config": {
    "default": true,
    "MD013": false
  },
  "globs": ["**/*.md"],
  "ignores": ["node_modules/**", "build/**"],
  "fix": false
}

Configuration precedence

  1. .markdownlint-cli2.jsonc / .markdownlint-cli2.yaml (full CLI config)
  2. .markdownlint.jsonc / .markdownlint.json / .markdownlint.yaml (rules only)
  3. Inline HTML comment directives in the file itself

Inline directives

Control rules within a file using HTML comments:

markdown
<!-- markdownlint-disable MD013 -->
This long line won't trigger a warning because MD013 is disabled for this section of the file.
<!-- markdownlint-enable MD013 -->

<!-- markdownlint-disable-line MD034 -->
https://bare-url-allowed-on-this-line.com

<!-- markdownlint-disable-next-line MD041 -->
Not a heading on line 1

<!-- markdownlint-configure-file { "MD013": { "line_length": 120 } } -->

State capture/restore:

markdown
<!-- markdownlint-capture -->
<!-- markdownlint-disable MD013 -->
Long content here...
<!-- markdownlint-restore -->

Common rules quick reference

RuleAliasWhat it checksFixable
MD001heading-incrementHeading levels increment by oneNo
MD003heading-styleConsistent heading style (atx vs setext)No
MD004ul-styleConsistent unordered list markersYes
MD005list-indentConsistent list indentationYes
MD007ul-indentUnordered list indentation depthYes
MD009no-trailing-spacesNo trailing whitespaceYes
MD010no-hard-tabsNo hard tab charactersYes
MD012no-multiple-blanksNo consecutive blank linesYes
MD013line-lengthLine length limit (default 80)No
MD022blanks-around-headingsBlank lines around headingsYes
MD023heading-start-leftHeadings must start at line beginningYes
MD025single-h1Only one top-level heading per fileNo
MD031blanks-around-fencesBlank lines around fenced code blocksYes
MD032blanks-around-listsBlank lines around listsYes
MD033no-inline-htmlNo inline HTMLNo
MD034no-bare-urlsNo bare URLsNo
MD040fenced-code-languageFenced code blocks need a languageNo
MD041first-line-h1First line should be a top-level headingNo
MD047single-trailing-newlineFiles end with a single newlineYes

For a complete reference of all 47 rules with parameters, see references/rules.md.

Workflow

When creating or editing markdown files:

  1. Write or edit the markdown content
  2. Run the linter: npx markdownlint-cli2 "path/to/file.md"
  3. If errors are found:
    • Try auto-fix first: npx markdownlint-cli2 --fix "path/to/file.md"
    • Manually fix any remaining non-fixable issues
    • Re-run the linter to confirm zero errors
  4. If a rule should be permanently disabled, update .markdownlint.json
  5. For one-off exceptions, use inline directives

Tips

  • Run --fix before manual fixes to save effort — about half the rules are auto-fixable
  • Use rule aliases (e.g., no-trailing-spaces) instead of codes (e.g., MD009) in inline directives for readability
  • The MD013 line-length rule is commonly disabled in projects (this project disables it in .markdownlint.json)
  • Use <!-- markdownlint-disable-next-line --> for single-line exceptions rather than disable/enable blocks
  • Configuration in .markdownlint.json applies to the directory and all subdirectories