Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 114f037

Browse filesBrowse files
authored
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
1 parent c71b2d6 commit 114f037
Copy full SHA for 114f037

File tree

Expand file treeCollapse file tree

3 files changed

+29
-48
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+29
-48
lines changed

‎.github/workflows/ci.yml

Copy file name to clipboardExpand all lines: .github/workflows/ci.yml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ jobs:
1111
- uses: actions/checkout@v2
1212
- uses: actions/setup-node@v3
1313
with:
14-
node-version: 14
14+
node-version: 19
1515
- run: npm install
1616
- run: npm test

‎src/rules/no-default-alt-text.js

Copy file name to clipboard
+24-39Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,40 @@
1-
// Regex to match alt text that is the same as the default image filename
2-
// e.g. "Screen Shot 2020-10-20 at 2 52 27 PM"
3-
// e.g. "Screenshot 2020-10-20 at 2 52 27 PM"
4-
// e.g. "Clean Shot 2020-10-20 @45x"
5-
// e.g. "image"
6-
const defaultMacOsScreenshotMarkdownRegex =
7-
/^(Screen|Clean) ?[S|s]hot \d{4}-\d{2}-\d{2}/gi;
8-
const imageMarkdownRegex = /^image$/i;
1+
/**
2+
* Examples:
3+
* * "Screen Shot 2020-10-20 at 2 52 27 PM"
4+
* * "Screenshot 2020-10-20 at 2 52 27 PM"
5+
* * "Clean Shot 2020-10-20 @45x"
6+
*/
7+
const defaultScreenshotRegex =
8+
"(?:screen|clean) ?shot \\d{4}-\\d{2}-\\d{2}[^'\"\\]]*";
99

10-
const defaultMacOsScreenshotHtmlRegex =
11-
/alt="(Screen|Clean) ?[S|s]hot \d{4}-\d{2}-\d{2}/gi;
12-
const imageHtmlRegex = /alt="image"/i;
10+
const imageRegex = "image";
11+
const combinedRegex = `(${[defaultScreenshotRegex, imageRegex].join("|")})`;
12+
13+
const markdownAltRegex = new RegExp(`!\\[${combinedRegex}\\]\\(.*\\)`, "gid");
14+
const htmlAltRegex = new RegExp(`alt=["']${combinedRegex}["']`, "gid");
1315

1416
module.exports = {
1517
names: ["GH001", "no-default-alt-text"],
16-
description:
17-
"Images should set meaningful alternative text (alt text), and not use the macOS default screenshot filename or `Image`.",
18+
description: "Images should have meaningful alternative text (alt text)",
1819
information: new URL(
1920
"https://github.com/github/markdownlint-github/blob/main/docs/rules/GH001-no-default-alt-text.md"
2021
),
2122
tags: ["accessibility", "images"],
2223
function: function GH001(params, onError) {
23-
// markdown syntax
24-
const inlineTokens = params.tokens.filter((t) => t.type === "inline");
25-
for (const token of inlineTokens) {
26-
const imageTokens = token.children.filter((t) => t.type === "image");
27-
for (const image of imageTokens) {
28-
if (
29-
image.content.match(defaultMacOsScreenshotMarkdownRegex) ||
30-
image.content.match(imageMarkdownRegex)
31-
) {
32-
onError({
33-
lineNumber: image.lineNumber,
34-
detail: `For image: ${image.content}`,
35-
});
36-
}
37-
}
38-
}
24+
for (const [lineIndex, line] of params.lines.entries()) {
25+
for (const match of [
26+
...line.matchAll(markdownAltRegex),
27+
...line.matchAll(htmlAltRegex),
28+
]) {
29+
// The alt text is contained in the first capture group
30+
const altText = match[1];
31+
const [startIndex] = match.indices[1];
3932

40-
// html syntax
41-
let lineNumber = 1;
42-
for (const line of params.lines) {
43-
if (
44-
line.match(defaultMacOsScreenshotHtmlRegex) ||
45-
line.match(imageHtmlRegex)
46-
) {
4733
onError({
48-
lineNumber,
49-
detail: `For image: ${line}`,
34+
lineNumber: lineIndex + 1,
35+
range: [startIndex + 1, altText.length],
5036
});
5137
}
52-
lineNumber++;
5338
}
5439
},
5540
};

‎test/no-default-alt-text.test.js

Copy file name to clipboardExpand all lines: test/no-default-alt-text.test.js
+4-8Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,13 @@ describe("GH001: No Default Alt Text", () => {
8484
const results = await runTest(strings, altTextRule);
8585

8686
expect(results[0].ruleDescription).toMatch(
87-
"Images should set meaningful alternative text (alt text), and not use the macOS default screenshot filename or `Image`."
88-
);
89-
expect(results[0].errorDetail).toBe(
90-
"For image: Screen Shot 2022-06-26 at 7 41 30 PM"
87+
"Images should have meaningful alternative text (alt text)"
9188
);
89+
expect(results[0].errorRange).toEqual([3, 36]);
9290
expect(results[1].ruleDescription).toMatch(
93-
"Images should set meaningful alternative text (alt text), and not use the macOS default screenshot filename or `Image`."
94-
);
95-
expect(results[1].errorDetail).toBe(
96-
'For image: <img alt="Screen Shot 2022-06-26 at 7 41 30 PM" src="https://user-images.githubusercontent.com/abcdef.png">'
91+
"Images should have meaningful alternative text (alt text)"
9792
);
93+
expect(results[1].errorRange).toEqual([11, 36]);
9894
});
9995
});
10096
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.