-
-
Notifications
You must be signed in to change notification settings - Fork 748
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rule MD059/descriptive-link-text "Link text should be descriptive" (
fixes #681).
- Loading branch information
1 parent
a4c553a
commit 27d39b7
Showing
42 changed files
with
1,107 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
This rule is triggered when a link is set with generic text like | ||
"Click here", "here", or "learn more", giving it a generic accessible name. | ||
|
||
Rationale: Screen reader users may navigate through a list of links | ||
to quickly find content on a page. When the link name is something ambiguous | ||
like "Learn more", there isn't sufficient context to help the user determine | ||
whether to follow the link. | ||
|
||
Link names should be descriptive and describe the purpose of the link, like: | ||
`[Download the budget document]`, `[About markdownlint]`,`[View registration]`, | ||
etc. | ||
|
||
To override the default list and configure your own list of banned accessible | ||
names, set `link_texts` in the config. | ||
|
||
Note: This rule checks Markdown-style links and ignores HTML-style links. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# `MD059` - Link text should be descriptive | ||
|
||
Tags: `accessibility`, `links` | ||
|
||
Aliases: `descriptive-link-text` | ||
|
||
Parameters: | ||
|
||
- `link_texts`: List of restricted link texts (`string[]`, default `[]`) | ||
|
||
This rule is triggered when a link is set with generic text like | ||
"Click here", "here", or "learn more", giving it a generic accessible name. | ||
|
||
Rationale: Screen reader users may navigate through a list of links | ||
to quickly find content on a page. When the link name is something ambiguous | ||
like "Learn more", there isn't sufficient context to help the user determine | ||
whether to follow the link. | ||
|
||
Link names should be descriptive and describe the purpose of the link, like: | ||
`[Download the budget document]`, `[About markdownlint]`,`[View registration]`, | ||
etc. | ||
|
||
To override the default list and configure your own list of banned accessible | ||
names, set `link_texts` in the config. | ||
|
||
Note: This rule checks Markdown-style links and ignores HTML-style links. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// @ts-check | ||
|
||
import { addErrorContext } from "../helpers/helpers.cjs"; | ||
import { filterByTypesCached } from "./cache.mjs"; | ||
|
||
const defaultBannedText = [ | ||
"click here", | ||
"here", | ||
"learn more", | ||
"link", | ||
"more", | ||
"read more" | ||
]; | ||
|
||
/** | ||
* Normalizes a string and removes extra whitespaces and punctuations. | ||
* | ||
* @param {string} text String to transform. | ||
* @returns {string} Normalized string with no punctuations or extra whitespaces. | ||
*/ | ||
function normalizeText(text) { | ||
return text | ||
.toLowerCase() | ||
.replace(/\W+/g, " ") | ||
.replace(/\s+/g, " ") | ||
.trim(); | ||
} | ||
|
||
/** @type {import("markdownlint").Rule} */ | ||
export default { | ||
"names": [ "MD059", "descriptive-link-text" ], | ||
"description": "Link text should be descriptive", | ||
"tags": [ "links", "accessibility" ], | ||
"parser": "micromark", | ||
"function": function MD059(params, onError) { | ||
const bannedNames = new Set(params.config.link_texts || defaultBannedText); | ||
const labels = filterByTypesCached([ "label" ]) | ||
.filter((label) => label.parent?.type === "link"); | ||
|
||
for (const label of labels) { | ||
const labelTexts = label.children.filter((child) => child.type === "labelText"); | ||
for (const labelText of labelTexts) { | ||
const { text } = label; | ||
if (bannedNames.has(normalizeText(text))) { | ||
const range = labelText.startLine === labelText.endLine ? | ||
[ labelText.startColumn, text.length ] : | ||
undefined; | ||
addErrorContext( | ||
onError, | ||
labelText.startLine, | ||
text, | ||
undefined, | ||
undefined, | ||
range | ||
); | ||
} | ||
} | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.