-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve the no-default-alt-text rule (#40)
* Improve the no-default-alt-text rule * Fix lint errors * Update node version for tests * Change example comment to JSDoc * Format
- Loading branch information
1 parent
c71b2d6
commit 114f037
Showing
3 changed files
with
29 additions
and
48 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 |
---|---|---|
@@ -1,55 +1,40 @@ | ||
// Regex to match alt text that is the same as the default image filename | ||
// e.g. "Screen Shot 2020-10-20 at 2 52 27 PM" | ||
// e.g. "Screenshot 2020-10-20 at 2 52 27 PM" | ||
// e.g. "Clean Shot 2020-10-20 @45x" | ||
// e.g. "image" | ||
const defaultMacOsScreenshotMarkdownRegex = | ||
/^(Screen|Clean) ?[S|s]hot \d{4}-\d{2}-\d{2}/gi; | ||
const imageMarkdownRegex = /^image$/i; | ||
/** | ||
* Examples: | ||
* * "Screen Shot 2020-10-20 at 2 52 27 PM" | ||
* * "Screenshot 2020-10-20 at 2 52 27 PM" | ||
* * "Clean Shot 2020-10-20 @45x" | ||
*/ | ||
const defaultScreenshotRegex = | ||
"(?:screen|clean) ?shot \\d{4}-\\d{2}-\\d{2}[^'\"\\]]*"; | ||
|
||
const defaultMacOsScreenshotHtmlRegex = | ||
/alt="(Screen|Clean) ?[S|s]hot \d{4}-\d{2}-\d{2}/gi; | ||
const imageHtmlRegex = /alt="image"/i; | ||
const imageRegex = "image"; | ||
const combinedRegex = `(${[defaultScreenshotRegex, imageRegex].join("|")})`; | ||
|
||
const markdownAltRegex = new RegExp(`!\\[${combinedRegex}\\]\\(.*\\)`, "gid"); | ||
const htmlAltRegex = new RegExp(`alt=["']${combinedRegex}["']`, "gid"); | ||
|
||
module.exports = { | ||
names: ["GH001", "no-default-alt-text"], | ||
description: | ||
"Images should set meaningful alternative text (alt text), and not use the macOS default screenshot filename or `Image`.", | ||
description: "Images should have meaningful alternative text (alt text)", | ||
information: new URL( | ||
"https://github.com/github/markdownlint-github/blob/main/docs/rules/GH001-no-default-alt-text.md" | ||
), | ||
tags: ["accessibility", "images"], | ||
function: function GH001(params, onError) { | ||
// markdown syntax | ||
const inlineTokens = params.tokens.filter((t) => t.type === "inline"); | ||
for (const token of inlineTokens) { | ||
const imageTokens = token.children.filter((t) => t.type === "image"); | ||
for (const image of imageTokens) { | ||
if ( | ||
image.content.match(defaultMacOsScreenshotMarkdownRegex) || | ||
image.content.match(imageMarkdownRegex) | ||
) { | ||
onError({ | ||
lineNumber: image.lineNumber, | ||
detail: `For image: ${image.content}`, | ||
}); | ||
} | ||
} | ||
} | ||
for (const [lineIndex, line] of params.lines.entries()) { | ||
for (const match of [ | ||
...line.matchAll(markdownAltRegex), | ||
...line.matchAll(htmlAltRegex), | ||
]) { | ||
// The alt text is contained in the first capture group | ||
const altText = match[1]; | ||
const [startIndex] = match.indices[1]; | ||
|
||
// html syntax | ||
let lineNumber = 1; | ||
for (const line of params.lines) { | ||
if ( | ||
line.match(defaultMacOsScreenshotHtmlRegex) || | ||
line.match(imageHtmlRegex) | ||
) { | ||
onError({ | ||
lineNumber, | ||
detail: `For image: ${line}`, | ||
lineNumber: lineIndex + 1, | ||
range: [startIndex + 1, altText.length], | ||
}); | ||
} | ||
lineNumber++; | ||
} | ||
}, | ||
}; |
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