Skip to content

Normalize images

Normalize images #3

name: Normalize GitBook assets for Git it Write
on:
push:
branches: [ main ]
workflow_dispatch:
permissions:
contents: write
concurrency:
group: normalize-assets-${{ github.ref }}
cancel-in-progress: true
jobs:
normalize:
if: ${{ github.actor != 'github-actions[bot]' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Configure author
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Update to latest main (rebase)
run: |
git fetch origin main
git checkout main
git rebase origin/main
- name: Ensure _images exists
run: mkdir -p _images/gitbook
- name: Copy from .gitbook/assets
run: |
if [ -d ".gitbook/assets" ]; then
rsync -a --delete ".gitbook/assets/" "_images/gitbook/"
fi
- name: Rewrite image paths in Markdown (depth-aware)
shell: bash
run: |
shopt -s globstar nullglob
for f in **/*.md; do
dir="${f%/*}"
if [[ "$dir" == "$f" ]]; then depth=0; else depth=$(awk -F'/' '{print NF}' <<< "$dir"); fi
prefix=""
for ((i=0; i<depth; i++)); do prefix+="../"; done
sed -i -e "s#](\\.gitbook/assets/#](${prefix}_images/gitbook/#g" \
-e "s#](/\\.gitbook/assets/#](${prefix}_images/gitbook/#g" "$f"
sed -i -e "s#src=\"\\./\\.gitbook/assets/#src=\"${prefix}_images/gitbook/#g" \
-e "s#src=\"/\\.gitbook/assets/#src=\"${prefix}_images/gitbook/#g" "$f"
done
- name: Commit changes (if any)
run: |
if [ -n "$(git status --porcelain)" ]; then
git add -A
git commit -m "Normalize GitBook assets for Git it Write [ci skip]"
else
echo "No changes to commit."
fi
- name: Rebase on latest and push (retry with --force-with-lease)
shell: bash
run: |
if [ -z "$(git log origin/main..HEAD)" ]; then
echo "No new commits to push."
exit 0
fi
for attempt in 1 2 3; do
echo "Push attempt $attempt..."
if git push --force-with-lease; then
echo "Pushed successfully."
exit 0
fi
echo "Push rejected. Fetching and rebasing..."
git fetch origin main
git rebase origin/main || exit 1
sleep 2
done
echo "Giving up after 3 attempts."
exit 1