Skip to content

Commit b5828a5

Browse files
Flag images without alt text (#26)
* Flag images without alt text * add exit on failure * add todo; * uncomment * flag images without alt
1 parent 5d9b99c commit b5828a5

File tree

3 files changed

+60
-45
lines changed

3 files changed

+60
-45
lines changed

action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ runs:
5959
Learn more about alt text at [Basic writing and formatting syntax: images on GitHub Docs](https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#images)."
6060
flag="$(flagAltText "$content")"
6161
62-
echo $flag
63-
echo $type
62+
echo "Detected bad alt text: ${flag}"
63+
echo "Event type: $type"
6464
6565
if [[ $flag = true ]]; then
6666
if [[ $type = pr_comment ]] || [[ $type = pr_description ]]; then

flag-alt-text.sh

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
#!/bin/bash
22

33
flagAltText() {
4-
markdownMacOsScreenshotRegex="^.*!\[(Clean|Screen) ?[S|s]hot [0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-9][0-9].*\].*$"
5-
semanticMacOsScreenshotRegex="^.*<img.*(Clean|Screen) ?[S|s]hot [0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-9][0-9].*$"
6-
markdownImageRegex="^.*!\[(i|I)mage\].*$"
7-
semanticImageRegex="^.*<img.*alt=\"(i|I)mage\".*$"
8-
if [[ $1 =~ $semanticMacOsScreenshotRegex ]] || [[ $1 =~ $markdownMacOsScreenshotRegex ]] || [[ $1 =~ $semanticImageRegex ]] || [[ $1 =~ $markdownImageRegex ]]; then
9-
echo true
10-
else
11-
echo false
12-
fi
4+
markdownMacOsScreenshotRegex="^.*!\[(Clean|Screen) ?[S|s]hot [0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-9][0-9].*\].*$"
5+
semanticMacOsScreenshotRegex="^.*<img.*(Clean|Screen) ?[S|s]hot [0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-9][0-9].*$"
6+
markdownImageRegex="^.*!\[(i|I)mage\].*$"
7+
semanticImageRegexStartingWithImage="^.*<img.*alt=\"(i|I)mage\".*$"
8+
emptySemanticRegex="^.*<img.*alt=(\"|')(\"|').*$"
9+
emptyMarkdownRegex="^.*!\[\].*$"
10+
semanticImageRegex="^.*<img.*$"
11+
semanticImageWithAltRegex="^.*<img.*alt=.*$"
12+
13+
if [[ ($1 =~ $semanticImageRegex) && ! ($1 =~ $semanticImageWithAltRegex) ]] || [[ $1 =~ $semanticMacOsScreenshotRegex ]] || [[ $1 =~ $markdownMacOsScreenshotRegex ]] || [[ $1 =~ $semanticImageRegexStartingWithImage ]] || [[ $1 =~ $markdownImageRegex ]] || [[ $1 =~ $emptySemanticRegex ]] || [[ $1 =~ $emptyMarkdownRegex ]]; then
14+
echo true
15+
else
16+
echo false
17+
fi
1318
}

test-flag-alt-text.sh

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,57 @@ source assert.sh
66
# true
77

88
declare -a should_be_true=(
9-
# markdown
10-
'![Cleanshot 2020-01-01 at 12.00.00.png]'
11-
'![Clean shot 2020-12-01 @12x]'
12-
"![Clean shot 2020-12-01 @12x]"
13-
"![Screen Shot 2020-01-01 at 12.00.00.png]"
14-
"![Screenshot 2020-01-01 at 12.00.00.png]"
15-
"![image]"
16-
"![Image]"
17-
"Check this: ![Image]"
18-
"My awesome ![image]"
19-
'Check this out: <img alt="image" src="cat.png">'
20-
# html
21-
'<img alt="image" src="cat.png">'
22-
'<img alt="Screen shot 2020-01-01 at 12.00.00.png" src="cat.png">'
23-
'<img alt="Screen Shot 2020-01-01 at 12.00.00.png" src="cat.png">'
24-
'<img alt="Screenshot 2020-01-01 at 12.00.00.png" src="cat.png">'
25-
'<img alt="CleanShot 2020-01-01 @12x" src="cat.png">'
9+
# markdown
10+
'![Cleanshot 2020-01-01 at 12.00.00.png]'
11+
'![Clean shot 2020-12-01 @12x]'
12+
"![Clean shot 2020-12-01 @12x]"
13+
"![Screen Shot 2020-01-01 at 12.00.00.png]"
14+
"![Screenshot 2020-01-01 at 12.00.00.png]"
15+
"![image]"
16+
"![Image]"
17+
"![]"
18+
"Check this: ![Image]"
19+
"My awesome ![image]"
20+
'Check this out: <img alt="image" src="cat.png">'
21+
# html
22+
'<img alt="image" src="cat.png">'
23+
'<img alt="" src="cat.png">'
24+
"<img alt='' src='cat.png'>"
25+
"<img src="cat.png">"
26+
'<img alt src="cat.png">'
27+
'<img src="cat.png" width="10px">'
28+
'<img alt="Screen shot 2020-01-01 at 12.00.00.png" src="cat.png">'
29+
'<img alt="Screen Shot 2020-01-01 at 12.00.00.png" src="cat.png">'
30+
'<img alt="Screenshot 2020-01-01 at 12.00.00.png" src="cat.png">'
31+
'<img alt="CleanShot 2020-01-01 @12x" src="cat.png">'
2632
)
2733

2834
declare -a should_be_false=(
29-
# markdown
30-
"![Screenshot of the new GitHub home page]"
31-
"![Screen shot of Submit button with updated color contrast.]"
32-
"![Image of a cat]"
33-
# html
34-
'<img alt="Mona Lisa, the Octocat" src="cat.png">'
35-
'<img alt="Screenshot of the new danger button with a dark red shade" src="test.png">'
36-
'<img alt="Clean shot of the scenery" src="test.png">'
35+
# markdown
36+
"![Screenshot of the new GitHub home page]"
37+
"![Screen shot of Submit button with updated color contrast.]"
38+
"![Image of a cat]"
39+
# html
40+
'<img src="cat.png" alt="Mona Lisa, the Octocat" >'
41+
'<img alt="Mona Lisa, the Octocat" src="cat.png">'
42+
'<img alt="Screenshot of the new danger button with a dark red shade" src="test.png">'
43+
'<img alt="Clean shot of the scenery" src="test.png">'
3744
)
3845

3946
echo "******Expecting true:*******"
40-
for i in "${should_be_true[@]}"
41-
do
42-
echo "Testing: $i"
43-
assert_true "$(flagAltText "$i")" "$i must be true"
47+
for i in "${should_be_true[@]}"; do
48+
echo "Testing: $i"
49+
assert_true "$(flagAltText "$i")" "$i must be true"
50+
if [ $? == 1 ]; then
51+
exit 1
52+
fi
4453
done
4554

4655
echo "******Expecting false:*******"
47-
for i in "${should_be_false[@]}"
48-
do
49-
echo "Testing: $i"
50-
assert_false "$(flagAltText "$i")" "$i must be false"
56+
for i in "${should_be_false[@]}"; do
57+
echo "Testing: $i"
58+
assert_false "$(flagAltText "$i")" "$i must be false"
59+
if [ $? == 1 ]; then
60+
exit 1
61+
fi
5162
done
52-

0 commit comments

Comments
 (0)