Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the no-default-alt-text rule #40

Merged
merged 5 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v3
with:
node-version: 14
node-version: 19
- run: npm install
- run: npm test
63 changes: 24 additions & 39 deletions src/rules/no-default-alt-text.js
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++;
}
},
};
12 changes: 4 additions & 8 deletions test/no-default-alt-text.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,13 @@ describe("GH001: No Default Alt Text", () => {
const results = await runTest(strings, altTextRule);

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