Skip to content

Commit

Permalink
Flag images without alt text (#26)
Browse files Browse the repository at this point in the history
* Flag images without alt text

* add exit on failure

* add todo;

* uncomment

* flag images without alt
  • Loading branch information
kendallgassner committed Jul 5, 2023
1 parent 5d9b99c commit b5828a5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 45 deletions.
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ runs:
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)."
flag="$(flagAltText "$content")"
echo $flag
echo $type
echo "Detected bad alt text: ${flag}"
echo "Event type: $type"
if [[ $flag = true ]]; then
if [[ $type = pr_comment ]] || [[ $type = pr_description ]]; then
Expand Down
23 changes: 14 additions & 9 deletions flag-alt-text.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#!/bin/bash

flagAltText() {
markdownMacOsScreenshotRegex="^.*!\[(Clean|Screen) ?[S|s]hot [0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-9][0-9].*\].*$"
semanticMacOsScreenshotRegex="^.*<img.*(Clean|Screen) ?[S|s]hot [0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-9][0-9].*$"
markdownImageRegex="^.*!\[(i|I)mage\].*$"
semanticImageRegex="^.*<img.*alt=\"(i|I)mage\".*$"
if [[ $1 =~ $semanticMacOsScreenshotRegex ]] || [[ $1 =~ $markdownMacOsScreenshotRegex ]] || [[ $1 =~ $semanticImageRegex ]] || [[ $1 =~ $markdownImageRegex ]]; then
echo true
else
echo false
fi
markdownMacOsScreenshotRegex="^.*!\[(Clean|Screen) ?[S|s]hot [0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-9][0-9].*\].*$"
semanticMacOsScreenshotRegex="^.*<img.*(Clean|Screen) ?[S|s]hot [0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-9][0-9].*$"
markdownImageRegex="^.*!\[(i|I)mage\].*$"
semanticImageRegexStartingWithImage="^.*<img.*alt=\"(i|I)mage\".*$"
emptySemanticRegex="^.*<img.*alt=(\"|')(\"|').*$"
emptyMarkdownRegex="^.*!\[\].*$"
semanticImageRegex="^.*<img.*$"
semanticImageWithAltRegex="^.*<img.*alt=.*$"

if [[ ($1 =~ $semanticImageRegex) && ! ($1 =~ $semanticImageWithAltRegex) ]] || [[ $1 =~ $semanticMacOsScreenshotRegex ]] || [[ $1 =~ $markdownMacOsScreenshotRegex ]] || [[ $1 =~ $semanticImageRegexStartingWithImage ]] || [[ $1 =~ $markdownImageRegex ]] || [[ $1 =~ $emptySemanticRegex ]] || [[ $1 =~ $emptyMarkdownRegex ]]; then
echo true
else
echo false
fi
}
78 changes: 44 additions & 34 deletions test-flag-alt-text.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,57 @@ source assert.sh
# true

declare -a should_be_true=(
# markdown
'![Cleanshot 2020-01-01 at 12.00.00.png]'
'![Clean shot 2020-12-01 @12x]'
"![Clean shot 2020-12-01 @12x]"
"![Screen Shot 2020-01-01 at 12.00.00.png]"
"![Screenshot 2020-01-01 at 12.00.00.png]"
"![image]"
"![Image]"
"Check this: ![Image]"
"My awesome ![image]"
'Check this out: <img alt="image" src="cat.png">'
# html
'<img alt="image" src="cat.png">'
'<img alt="Screen shot 2020-01-01 at 12.00.00.png" src="cat.png">'
'<img alt="Screen Shot 2020-01-01 at 12.00.00.png" src="cat.png">'
'<img alt="Screenshot 2020-01-01 at 12.00.00.png" src="cat.png">'
'<img alt="CleanShot 2020-01-01 @12x" src="cat.png">'
# markdown
'![Cleanshot 2020-01-01 at 12.00.00.png]'
'![Clean shot 2020-12-01 @12x]'
"![Clean shot 2020-12-01 @12x]"
"![Screen Shot 2020-01-01 at 12.00.00.png]"
"![Screenshot 2020-01-01 at 12.00.00.png]"
"![image]"
"![Image]"
"![]"
"Check this: ![Image]"
"My awesome ![image]"
'Check this out: <img alt="image" src="cat.png">'
# html
'<img alt="image" src="cat.png">'
'<img alt="" src="cat.png">'
"<img alt='' src='cat.png'>"
"<img src="cat.png">"
'<img alt src="cat.png">'
'<img src="cat.png" width="10px">'
'<img alt="Screen shot 2020-01-01 at 12.00.00.png" src="cat.png">'
'<img alt="Screen Shot 2020-01-01 at 12.00.00.png" src="cat.png">'
'<img alt="Screenshot 2020-01-01 at 12.00.00.png" src="cat.png">'
'<img alt="CleanShot 2020-01-01 @12x" src="cat.png">'
)

declare -a should_be_false=(
# markdown
"![Screenshot of the new GitHub home page]"
"![Screen shot of Submit button with updated color contrast.]"
"![Image of a cat]"
# html
'<img alt="Mona Lisa, the Octocat" src="cat.png">'
'<img alt="Screenshot of the new danger button with a dark red shade" src="test.png">'
'<img alt="Clean shot of the scenery" src="test.png">'
# markdown
"![Screenshot of the new GitHub home page]"
"![Screen shot of Submit button with updated color contrast.]"
"![Image of a cat]"
# html
'<img src="cat.png" alt="Mona Lisa, the Octocat" >'
'<img alt="Mona Lisa, the Octocat" src="cat.png">'
'<img alt="Screenshot of the new danger button with a dark red shade" src="test.png">'
'<img alt="Clean shot of the scenery" src="test.png">'
)

echo "******Expecting true:*******"
for i in "${should_be_true[@]}"
do
echo "Testing: $i"
assert_true "$(flagAltText "$i")" "$i must be true"
for i in "${should_be_true[@]}"; do
echo "Testing: $i"
assert_true "$(flagAltText "$i")" "$i must be true"
if [ $? == 1 ]; then
exit 1
fi
done

echo "******Expecting false:*******"
for i in "${should_be_false[@]}"
do
echo "Testing: $i"
assert_false "$(flagAltText "$i")" "$i must be false"
for i in "${should_be_false[@]}"; do
echo "Testing: $i"
assert_false "$(flagAltText "$i")" "$i must be false"
if [ $? == 1 ]; then
exit 1
fi
done

0 comments on commit b5828a5

Please sign in to comment.