Skip to content

MD051 - link-fragments

Description

This rule checks that link fragments (internal links starting with #) reference valid anchors in the document. Valid anchors include headings (converted to fragments using when Markdown is displayed on GitHub), HTML elements with id attributes, and <a> elements with name attributes.

Rationale

GitHub section links are created automatically for every heading when Markdown content is displayed. However, section links break if headings are renamed or removed. This rule helps identify broken section links within a document before they cause problems for readers.

Configuration

ignore_case

Ignore case when comparing fragments with headings.

  • Default: false
ignored_pattern

Regular expression pattern for fragments to ignore.

  • Default: "" (empty string)

Examples

Invalid

# Heading One

This [link](#non-existent) references a non-existent heading.

## Heading Two

Another [bad link](#missing-section) here.

Valid

# Heading One

This [link](#heading-one) is valid.

## Heading Two

This [link](#heading-two) is also valid.

A [link to top](#top) is always valid.

<a id="custom-anchor"></a>

This [link](#custom-anchor) references an HTML anchor.