Skip to content

Commit 602efc3

Browse files
authored
Update alt text rule to flag image or Image (#28)
Update alt text rule to flag image or Image
1 parent 23e2eb6 commit 602efc3

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
// Regex to match alt text that is the same as the default image filename
22
// e.g. "Screen Shot 2020-10-20 at 2 52 27 PM"
33
// e.g. "Screenshot 2020-10-20 at 2 52 27 PM"
4-
const altTextRegex =
4+
const defaultMacOsScreenshotMarkdownRegex =
55
/^Screen ?[S|s]hot \d{4}-\d{2}-\d{2} at \d \d{2} \d{2} [A|P]M$/gi;
6-
const altTextTagRegex =
6+
const imageMarkdownRegex = /^image$/i;
7+
8+
const defaultMacOsScreenshotHtmlRegex =
79
/alt="Screen ?[S|s]hot \d{4}-\d{2}-\d{2} at \d \d{2} \d{2} [A|P]M"/gi;
10+
const imageHtmlRegex = /alt="image"/i;
811

912
module.exports = {
1013
names: ["GH001", "no-default-alt-text"],
1114
description:
12-
"Images should not use the MacOS default screenshot filename as alternate text (alt text). If you have not changed this file, try merging main with your branch. For more information see: https://primer.style/design/accessibility/alternative-text-for-images",
15+
"Images should set meaningful alternative text (alt text), and not use the macOS default screenshot filename or `Image`.",
1316
information: new URL(
1417
"https://github.com/github/markdownlint-github/blob/main/docs/rules/GH001-no-default-alt-text.md"
1518
),
@@ -20,7 +23,10 @@ module.exports = {
2023
for (const token of inlineTokens) {
2124
const imageTokens = token.children.filter((t) => t.type === "image");
2225
for (const image of imageTokens) {
23-
if (image.content.match(altTextRegex)) {
26+
if (
27+
image.content.match(defaultMacOsScreenshotMarkdownRegex) ||
28+
image.content.match(imageMarkdownRegex)
29+
) {
2430
onError({
2531
lineNumber: image.lineNumber,
2632
detail: `For image: ${image.content}`,
@@ -32,7 +38,10 @@ module.exports = {
3238
// html syntax
3339
let lineNumber = 1;
3440
for (const line of params.lines) {
35-
if (line.match(altTextTagRegex)) {
41+
if (
42+
line.match(defaultMacOsScreenshotHtmlRegex) ||
43+
line.match(imageHtmlRegex)
44+
) {
3645
onError({
3746
lineNumber,
3847
detail: `For image: ${line}`,

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ describe("GH001: No Default Alt Text", () => {
3333
"![ScreenShot 2022-06-26 at 7 41 30 PM](https://user-images.githubusercontent.com/abcdef.png)",
3434
"![Screen shot 2022-06-26 at 7 41 30 PM](https://user-images.githubusercontent.com/abcdef.png)",
3535
"![Screenshot 2022-06-26 at 7 41 30 PM](https://user-images.githubusercontent.com/abcdef.png)",
36+
"![image](https://user-images.githubusercontent.com/abcdef.png)",
37+
"![Image](https://user-images.githubusercontent.com/abcdef.png)",
3638
];
3739

3840
const results = await runTest(strings, altTextRule);
@@ -42,7 +44,7 @@ describe("GH001: No Default Alt Text", () => {
4244
.flat()
4345
.filter((name) => !name.includes("GH"));
4446

45-
expect(failedRules).toHaveLength(4);
47+
expect(failedRules).toHaveLength(6);
4648
for (const rule of failedRules) {
4749
expect(rule).toBe("no-default-alt-text");
4850
}
@@ -54,6 +56,8 @@ describe("GH001: No Default Alt Text", () => {
5456
'<img alt="ScreenShot 2022-06-26 at 7 41 30 PM" src="https://user-images.githubusercontent.com/abcdef.png">',
5557
'<img alt="Screen shot 2022-06-26 at 7 41 30 PM" src="https://user-images.githubusercontent.com/abcdef.png">',
5658
'<img alt="Screenshot 2022-06-26 at 7 41 30 PM" src="https://user-images.githubusercontent.com/abcdef.png">',
59+
'<img alt="Image" src="https://user-images.githubusercontent.com/abcdef.png">',
60+
'<img alt="image" src="https://user-images.githubusercontent.com/abcdef.png">',
5761
];
5862

5963
const results = await runTest(strings, altTextRule);
@@ -63,7 +67,7 @@ describe("GH001: No Default Alt Text", () => {
6367
.flat()
6468
.filter((name) => !name.includes("GH"));
6569

66-
expect(failedRules).toHaveLength(4);
70+
expect(failedRules).toHaveLength(6);
6771
for (const rule of failedRules) {
6872
expect(rule).toBe("no-default-alt-text");
6973
}
@@ -78,13 +82,13 @@ describe("GH001: No Default Alt Text", () => {
7882
const results = await runTest(strings, altTextRule);
7983

8084
expect(results[0].ruleDescription).toMatch(
81-
/Images should not use the MacOS default screenshot filename as alternate text/
85+
"Images should set meaningful alternative text (alt text), and not use the macOS default screenshot filename or `Image`."
8286
);
8387
expect(results[0].errorDetail).toBe(
8488
"For image: Screen Shot 2022-06-26 at 7 41 30 PM"
8589
);
8690
expect(results[1].ruleDescription).toMatch(
87-
/Images should not use the MacOS default screenshot filename as alternate text/
91+
"Images should set meaningful alternative text (alt text), and not use the macOS default screenshot filename or `Image`."
8892
);
8993
expect(results[1].errorDetail).toBe(
9094
'For image: <img alt="Screen Shot 2022-06-26 at 7 41 30 PM" src="https://user-images.githubusercontent.com/abcdef.png">'

0 commit comments

Comments
 (0)