Skip to content

Commit

Permalink
Bash script generates URLs for pa11y-ci from git diff
Browse files Browse the repository at this point in the history
  • Loading branch information
jcarstairs-scottlogic committed Nov 29, 2023
1 parent cf4da3e commit f00988b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ Gemfile.lock

/container_gem_cache/
/dist/
/scripts/generate-related/data/
/scripts/generate-related/data/
/pa11y-ci-urls.js
41 changes: 41 additions & 0 deletions generate_pa11y_ci_urls_from_git_diff.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash
# Take in a base ref. Gets the files that have changed from the base to the HEAD.
# Export the list as a CommonJS module for the pa11y-ci configuration to use.
# Typical usage: generate_pa11y_ci_urls_from_git_diff.sh gh-pages > .pa11yci.js

function generate_pa11y_ci_urls_from_git_diff {
base_ref=$1
if [ -z "$base_ref" ]; then
echo "No Git base ref provided. Usage: $0 <base_ref>"
exit 1
fi

changed_files=$(git diff --name-only $base_ref)

echo -n "module.exports=[";
for file in $changed_files; do
if [[ $file == _posts/* ]]; then
echo -n "'/$(url_of_post $file)',";
elif [[ $file == *.html && ! $file == _includes/* && ! $file == _layouts/* ]]; then
echo -n "'/$(url_of_page $file)',";
fi
done
echo -n "];";
}

function url_of_page {
echo $1 | sed "s/\(\/\?index\)\?\.html$//";
}

function url_of_post {
year="\([0-9][0-9][0-9][0-9]\)"
month="\([0-9][0-9]\)"
day="\([0-9][0-9]\)"
slug="\(.*\)"
file_ext="\(html\|markdown\|md\)"

echo $1 | sed "s/^_posts\/$year-$month-$day-$slug\.$file_ext$/\1\/\2\/\3\/\4.html/";
}

generate_pa11y_ci_urls_from_git_diff $1

0 comments on commit f00988b

Please sign in to comment.