[PR #1117] [MERGED] feat: support line number in fragment in MD051/link-fragments (e.g: '#L7') #1234

Closed
opened 2026-03-03 02:02:33 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/DavidAnson/markdownlint/pull/1117
Author: @theoludwig
Created: 1/31/2024
Status: Merged
Merged: 2/10/2024
Merged by: @DavidAnson

Base: nextHead: feat/support-line-number-in-fragment


📝 Commits (2)

  • 4307342 Update MD051/link-fragments to support GitHub-style line/column range syntax in URLs (refs #1117).
  • d6d091e Refine previous commit for MD051/link-fragments to tweak behavior, tests, and documentation.

📊 Changes

8 files changed (+440 additions, -26 deletions)

View changed files

📝 demo/markdownlint-browser.js (+3 -1)
📝 doc-build/md051.md (+26 -0)
📝 doc/Rules.md (+26 -0)
📝 doc/md051.md (+26 -0)
📝 lib/md051.js (+4 -2)
📝 test/link-fragments.md (+44 -0)
📝 test/snapshots/markdownlint-test-scenarios.js.md (+311 -23)
📝 test/snapshots/markdownlint-test-scenarios.js.snap (+0 -0)

📄 Description

Hello! 😄
I'm the original author of the rule MD051, work started in this PR: https://github.com/DavidAnson/markdownlint/pull/495.
I'm also the author of https://github.com/theoludwig/markdownlint-rule-relative-links custom rule.
This custom rule, verify if relative links exists in the file system using Node.js node:fs, it also checks for links fragments referencing other files (similar to the rule MD051, by trying to have as much as possible the same behavior as the MD051 built-in rule in this regard).

GitHub supports links fragments for referring to a specific line, e.g:

[Link](#L7)

#L7 means "Go To Line 7`.

More context: https://github.com/theoludwig/markdownlint-rule-relative-links/issues/6

Currently MD051 reports an error for that kind of fragments, but it should not, as they are valid on GitHub.

This PR fixes the issue, similarly to how it is done in markdownlint-rule-relative-links.
In the custom rule, we can go even further and check if the file has enough lines to be a valid line number reference (e.g: #L7 in a empty file, is invalid, as line 7 doesn't exist), but that needs access to Node.js node:fs, which as far as I understand, you don't want to use for built-in rule for compatibility with the browser. So for now we're just considering it's valid without extra checks.

Feel free to suggest ideas, suggest better implementation, or maybe you have ideas of more test cases to add?

Thanks!


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/DavidAnson/markdownlint/pull/1117 **Author:** [@theoludwig](https://github.com/theoludwig) **Created:** 1/31/2024 **Status:** ✅ Merged **Merged:** 2/10/2024 **Merged by:** [@DavidAnson](https://github.com/DavidAnson) **Base:** `next` ← **Head:** `feat/support-line-number-in-fragment` --- ### 📝 Commits (2) - [`4307342`](https://github.com/DavidAnson/markdownlint/commit/4307342873dfca9f4885fe82959d613e399cfec2) Update MD051/link-fragments to support GitHub-style line/column range syntax in URLs (refs #1117). - [`d6d091e`](https://github.com/DavidAnson/markdownlint/commit/d6d091e2a727154c733008f9649e81538cac3b87) Refine previous commit for MD051/link-fragments to tweak behavior, tests, and documentation. ### 📊 Changes **8 files changed** (+440 additions, -26 deletions) <details> <summary>View changed files</summary> 📝 `demo/markdownlint-browser.js` (+3 -1) 📝 `doc-build/md051.md` (+26 -0) 📝 `doc/Rules.md` (+26 -0) 📝 `doc/md051.md` (+26 -0) 📝 `lib/md051.js` (+4 -2) 📝 `test/link-fragments.md` (+44 -0) 📝 `test/snapshots/markdownlint-test-scenarios.js.md` (+311 -23) 📝 `test/snapshots/markdownlint-test-scenarios.js.snap` (+0 -0) </details> ### 📄 Description Hello! :smile: I'm the original author of the rule MD051, work started in this PR: https://github.com/DavidAnson/markdownlint/pull/495. I'm also the author of https://github.com/theoludwig/markdownlint-rule-relative-links custom rule. This custom rule, verify if relative links exists in the file system using Node.js `node:fs`, it also checks for links fragments referencing other files (similar to the rule MD051, by trying to have as much as possible the same behavior as the MD051 built-in rule in this regard). GitHub supports links fragments for referring to a specific line, e.g: ```md [Link](#L7) ``` `#L7` means "Go To Line 7`. More context: https://github.com/theoludwig/markdownlint-rule-relative-links/issues/6 Currently MD051 reports an error for that kind of fragments, but it should not, as they are valid on GitHub. This PR fixes the issue, similarly to how it is done in `markdownlint-rule-relative-links`. In the custom rule, we can go even further and check if the file has enough lines to be a valid line number reference (e.g: `#L7` in a empty file, is invalid, as line 7 doesn't exist), but that needs access to Node.js `node:fs`, which as far as I understand, you don't want to use for built-in rule for compatibility with the browser. So for now we're just considering it's valid without extra checks. Feel free to suggest ideas, suggest better implementation, or maybe you have ideas of more test cases to add? Thanks! --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-03-03 02:02:33 +03:00
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#1234
No description provided.