[GH-ISSUE #1771] False positive for MD051 when header contains a colon #744

Closed
opened 2026-03-03 01:29:31 +03:00 by kerem · 3 comments
Owner

Originally created by @cdwilson on GitHub (Sep 23, 2025).
Original GitHub issue: https://github.com/DavidAnson/markdownlint/issues/1771

test.md produces a false positive for MD051:

# Overview

- [Overview](#overview)
  - [A heading with:a colon](#a-heading-witha-colon)

## A heading with:a colon

Some more text

❯ markdownlint-cli2 test.md
markdownlint-cli2 v0.18.1 (markdownlint v0.38.0)
Finding: test.md
Linting: 1 file(s)
Summary: 1 error(s)
test.md:4:5 MD051/link-fragments Link fragments should be valid [Context: "[A heading with:a colon](#a-heading-witha-colon)"]

Removing the colon from the header in test2.md:

# Overview

- [Overview](#overview)
  - [A heading without a colon](#a-heading-without-a-colon)

## A heading without a colon

Some more text
❯ markdownlint-cli2 test2.md
markdownlint-cli2 v0.18.1 (markdownlint v0.38.0)
Finding: test.md
Linting: 1 file(s)
Summary: 0 error(s)
Originally created by @cdwilson on GitHub (Sep 23, 2025). Original GitHub issue: https://github.com/DavidAnson/markdownlint/issues/1771 `test.md` produces a false positive for MD051: ```md # Overview - [Overview](#overview) - [A heading with:a colon](#a-heading-witha-colon) ## A heading with:a colon Some more text ``` ``` ❯ markdownlint-cli2 test.md markdownlint-cli2 v0.18.1 (markdownlint v0.38.0) Finding: test.md Linting: 1 file(s) Summary: 1 error(s) test.md:4:5 MD051/link-fragments Link fragments should be valid [Context: "[A heading with:a colon](#a-heading-witha-colon)"] ``` Removing the colon from the header in `test2.md`: ```md # Overview - [Overview](#overview) - [A heading without a colon](#a-heading-without-a-colon) ## A heading without a colon Some more text ``` ``` ❯ markdownlint-cli2 test2.md markdownlint-cli2 v0.18.1 (markdownlint v0.38.0) Finding: test.md Linting: 1 file(s) Summary: 0 error(s) ```
kerem 2026-03-03 01:29:31 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@DavidAnson commented on GitHub (Sep 23, 2025):

In your example, the text ":a" is recognized by the Markdown parser as a directive. You can see that in the output from the demo app:

https://dlaa.me/markdownlint/#%25m%23%20Overview%0A%0A%23%23%20A%20heading%20with%3Aa%20colon%0A%0A%5BA%20heading%20with%3Aa%20colon%5D(%23a-heading-witha-colon)%0A%0A%23%23%20A%20heading%20with%5C%3Aa%20escaped%20colon%0A%0A%5BA%20heading%20with%3Aa%20escaped%20colon%5D(%23a-heading-witha-escaped-colon)%0A

You can read more about directive syntax here: https://github.com/micromark/micromark-extension-directive

While your environment may not support directives, they're enabled and recognized by default by markdownlint. Per my example above, you can backslash-escape the colon character to avoid the unwanted warning. Or you can disable the rule for that line, file, or project.

<!-- gh-comment-id:3322402953 --> @DavidAnson commented on GitHub (Sep 23, 2025): In your example, the text ":a" is recognized by the Markdown parser as a directive. You can see that in the output from the demo app: <https://dlaa.me/markdownlint/#%25m%23%20Overview%0A%0A%23%23%20A%20heading%20with%3Aa%20colon%0A%0A%5BA%20heading%20with%3Aa%20colon%5D(%23a-heading-witha-colon)%0A%0A%23%23%20A%20heading%20with%5C%3Aa%20escaped%20colon%0A%0A%5BA%20heading%20with%3Aa%20escaped%20colon%5D(%23a-heading-witha-escaped-colon)%0A> You can read more about directive syntax here: https://github.com/micromark/micromark-extension-directive While your environment may not support directives, they're enabled and recognized by default by `markdownlint`. Per my example above, you can backslash-escape the colon character to avoid the unwanted warning. Or you can disable the rule for that line, file, or project.
Author
Owner

@cdwilson commented on GitHub (Sep 23, 2025):

Thanks for the context and the example!

Per my example above, you can backslash-escape the colon character to avoid the unwanted warning.

I ran across this today when I was trying to lint a table-of-contents generated by https://github.com/hukkin/mdformat-toc

For example, running the following doc through mdformat:

# Overview

<!-- mdformat-toc start --slug=github --no-anchors --maxlevel=6 --minlevel=1 -->

## A heading with\:a colon

Some more text

Produces this output:

# Overview

<!-- mdformat-toc start --slug=github --no-anchors --maxlevel=6 --minlevel=1 -->

- [Overview](#overview)
  - [A heading with:a colon](#a-heading-witha-colon)

<!-- mdformat-toc end -->

## A heading with:a colon

Some more text

From your comment, it sounds like mdformat should not remove the escape in the heading when it formats the doc. I'll open an issue in the mdformat-toc repo.

<!-- gh-comment-id:3322429408 --> @cdwilson commented on GitHub (Sep 23, 2025): Thanks for the context and the example! > Per my example above, you can backslash-escape the colon character to avoid the unwanted warning. I ran across this today when I was trying to lint a table-of-contents generated by https://github.com/hukkin/mdformat-toc For example, running the following doc through `mdformat`: ```md # Overview <!-- mdformat-toc start --slug=github --no-anchors --maxlevel=6 --minlevel=1 --> ## A heading with\:a colon Some more text ``` Produces this output: ```md # Overview <!-- mdformat-toc start --slug=github --no-anchors --maxlevel=6 --minlevel=1 --> - [Overview](#overview) - [A heading with:a colon](#a-heading-witha-colon) <!-- mdformat-toc end --> ## A heading with:a colon Some more text ``` From your comment, it sounds like `mdformat` should not remove the escape in the heading when it formats the doc. I'll open an issue in the `mdformat-toc` repo.
Author
Owner

@DavidAnson commented on GitHub (Sep 23, 2025):

I'm not sure I'd say they are WRONG to do so because directives are not part of the official CommonMark specification, but it IS inconvenient here and they may not need to be doing so. :)

<!-- gh-comment-id:3322567784 --> @DavidAnson commented on GitHub (Sep 23, 2025): I'm not sure I'd say they are WRONG to do so because directives are not part of the official CommonMark specification, but it IS inconvenient here and they may not need to be doing so. :)
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/markdownlint#744
No description provided.