test: lqip in GitHub Action #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Generate LQIP for Images | |
on: | |
push: | |
branches: | |
- main | |
workflow_dispatch: | |
jobs: | |
generateLqip: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout main branch | |
uses: actions/checkout@v3 | |
with: | |
ref: main | |
- name: Set up Node.js | |
uses: actions/setup-node@v3 | |
with: | |
node-version: '22.7' | |
- name: Install dependencies | |
run: | | |
cd tools/lqip | |
npm install | |
- name: Generate and insert LQIP data | |
run: | | |
img_dirs=("assets/img/post" "assets/img/site") | |
for img_dir in "${img_dirs[@]}"; do | |
node tools/lqip/index.js "$img_dir" > lqip_results.txt | |
while IFS= read -r line; do | |
image_name=$(echo "$line" | awk '{print $1}') | |
lqip_data=$(echo "$line" | awk '{print $2}') | |
markdown_files=$(find _posts -name "*${image_name}*") | |
for markdown_file in $markdown_files; do | |
if [[ -f "$markdown_file" ]]; then | |
if grep -q "!\[.*\](${image_name})" "$markdown_file"; then | |
existing_lqip=$(grep -oP "(?<=lqip:\s)[^\s]+" "$markdown_file" || echo "") | |
if ! grep -q "lqip:" "$markdown_file"; then | |
sed -i "/^image:/a\ lqip: $lqip_data" "$markdown_file" | |
elif [[ "$existing_lqip" != "$lqip_data" ]]; then | |
sed -i "s|lqip: .*|lqip: $lqip_data|" "$markdown_file" | |
fi | |
if grep -q "!\[.*\](${image_name}){: lqip=" "$markdown_file"; then | |
sed -i "s|!\[.*\](${image_name}){: lqip=\"[^\"]*\"|![${image_name}](${image_name}){: lqip=\"${lqip_data}\"|g" "$markdown_file" | |
else | |
sed -i "s|!\[.*\](${image_name})|![${image_name}](${image_name}){: lqip=\"${lqip_data}\"}|g" "$markdown_file" | |
fi | |
fi | |
fi | |
done | |
done < lqip_results.txt | |
done | |
- name: Commit changes | |
run: | | |
git config --global user.name "GitHub Action" | |
git config --global user.email "[email protected]" | |
git add _posts/* | |
git commit -m "Generated and inserted LQIP data" || echo "No changes to commit" | |
- name: Push changes | |
run: git push origin main | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |