Skip to content

Commit 21f9bad

Browse files
feat: adds no-default-alt-text from The Hub; scaffolds tests
0 parents  commit 21f9bad

11 files changed

Lines changed: 3135 additions & 0 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

accessibility-rules.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const noDefaultAltText = require('./no-default-alt-text')
2+
3+
module.exports.noDefaultAltText = noDefaultAltText
4+
5+
module.exports.all = [
6+
noDefaultAltText
7+
]

no-default-alt-text.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
const altTextRegex = /^Screen\s?shot \d{4}-\d{2}-\d{2} at \d \d{2} \d{2} [A|P]M$/gi
5+
const altTextTagRegex = /alt=\"Screen\s?shot \d{4}-\d{2}-\d{2} at \d \d{2} \d{2} [A|P]M\"/gi
6+
7+
module.exports = {
8+
"names": ["GH001", "no-default-alt-text"],
9+
"description": "Images should not use the MacOS default screenshot filename as alternate text (alt text). For more information see: https://primer.style/design/accessibility/alternative-text-for-images",
10+
"information": new URL("https://primer.style/design/accessibility/alternative-text-for-images"),
11+
"tags": ["accessibility", "images"],
12+
"function": function GH001(params, onError) {
13+
params.tokens.filter(t => t.type === "inline").forEach(token => {
14+
token.children.filter(t => t.type === "image").forEach(image => {
15+
if (image.content.match(altTextRegex)) {
16+
onError({
17+
lineNumber: image.lineNumber,
18+
details: `For image: ${image.content}`
19+
})
20+
}
21+
})
22+
})
23+
24+
let lineNumber = 1
25+
params.lines.forEach(line => {
26+
if (line.match(altTextTagRegex)) {
27+
onError({
28+
lineNumber: lineNumber,
29+
details: `For line: ${line}`
30+
})
31+
}
32+
lineNumber++
33+
})
34+
}
35+
}

0 commit comments

Comments
 (0)