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

Flag images without alt text #26

Merged
merged 5 commits into from
Jul 5, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"
Comment on lines +62 to +63
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice and clear!


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