|
1 |
| -// Regex to match alt text that is the same as the default image filename |
| 1 | +// Regex to match alt text that is the same as the default image filename |
2 | 2 | // e.g. "Screen Shot 2020-10-20 at 2 52 27 PM"
|
3 | 3 | // e.g. "Screenshot 2020-10-20 at 2 52 27 PM"
|
4 |
| -const altTextRegex = /^Screen ?[S|s]hot \d{4}-\d{2}-\d{2} at \d \d{2} \d{2} [A|P]M$/gi |
5 |
| -const altTextTagRegex = /alt=\"Screen ?[S|s]hot \d{4}-\d{2}-\d{2} at \d \d{2} \d{2} [A|P]M\"/gi |
| 4 | +const altTextRegex = |
| 5 | + /^Screen ?[S|s]hot \d{4}-\d{2}-\d{2} at \d \d{2} \d{2} [A|P]M$/gi; |
| 6 | +const altTextTagRegex = |
| 7 | + /alt="Screen ?[S|s]hot \d{4}-\d{2}-\d{2} at \d \d{2} \d{2} [A|P]M"/gi; |
6 | 8 |
|
7 | 9 | 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). 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", |
10 |
| - "information": new URL("https://primer.style/design/accessibility/alternative-text-for-images"), |
11 |
| - "tags": [ "accessibility", "images" ], |
12 |
| - "function": function GH001(params, onError) { |
13 |
| - // markdown syntax |
14 |
| - params.tokens.filter(t => t.type === "inline").forEach(token => { |
15 |
| - token.children.filter(t => t.type === "image").forEach(image => { |
16 |
| - if (image.content.match(altTextRegex)) { |
17 |
| - onError({ |
18 |
| - lineNumber: image.lineNumber, |
19 |
| - details: `For image: ${image.content}` |
20 |
| - }) |
21 |
| - } |
22 |
| - }) |
23 |
| - }) |
| 10 | + names: ["GH001", "no-default-alt-text"], |
| 11 | + 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", |
| 13 | + information: new URL( |
| 14 | + "https://primer.style/design/accessibility/alternative-text-for-images" |
| 15 | + ), |
| 16 | + tags: ["accessibility", "images"], |
| 17 | + function: function GH001(params, onError) { |
| 18 | + // markdown syntax |
| 19 | + const inlineTokens = params.tokens.filter((t) => t.type === "inline"); |
| 20 | + for (const token of inlineTokens) { |
| 21 | + const imageTokens = token.children.filter((t) => t.type === "image"); |
| 22 | + for (const image of imageTokens) { |
| 23 | + if (image.content.match(altTextRegex)) { |
| 24 | + onError({ |
| 25 | + lineNumber: image.lineNumber, |
| 26 | + details: `For image: ${image.content}`, |
| 27 | + }); |
| 28 | + } |
| 29 | + } |
| 30 | + } |
24 | 31 |
|
25 |
| - // html syntax |
26 |
| - let lineNumber = 1 |
27 |
| - params.lines.forEach(line => { |
28 |
| - if (line.match(altTextTagRegex)) { |
29 |
| - onError({ |
30 |
| - lineNumber: lineNumber, |
31 |
| - details: `For line: ${line}` |
32 |
| - }) |
33 |
| - } |
34 |
| - lineNumber++ |
35 |
| - }) |
| 32 | + // html syntax |
| 33 | + let lineNumber = 1; |
| 34 | + for (const line of params.lines) { |
| 35 | + if (line.match(altTextTagRegex)) { |
| 36 | + onError({ |
| 37 | + lineNumber, |
| 38 | + details: `For image: ${line}`, |
| 39 | + }); |
| 40 | + } |
| 41 | + lineNumber++; |
36 | 42 | }
|
37 |
| -} |
| 43 | + }, |
| 44 | +}; |
0 commit comments