forked from basalt-org/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
88 lines (70 loc) · 3.25 KB
/
fetch-readme.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
name: Fetch README and Images
on:
push:
branches: [ main ]
schedule:
- cron: '0 0 * * *' # Daily at midnight UTC
workflow_dispatch:
jobs:
fetch-readme:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '18'
- name: Fetch README and Images
run: |
REPO_URL=$(node -p "require('./docs.config.json').repositoryURL")
OWNER=$(echo $REPO_URL | sed 's/https:\/\/github.com\///' | cut -d'/' -f1)
REPO=$(echo $REPO_URL | sed 's/https:\/\/github.com\///' | cut -d'/' -f2)
mkdir -p public/readme-assets
README_CONTENT=$(curl -H "Accept: application/vnd.github.v3.raw" \
"https://api.github.com/repos/$OWNER/$REPO/contents/README.md")
echo "$README_CONTENT" > public/readme.md
TEMP_README=$(mktemp)
echo "$README_CONTENT" > "$TEMP_README"
# Process markdown image syntax ![alt](path)
while IFS= read -r line; do
if [[ $line =~ !\[.*\]\((.*?)\) ]]; then
IMG_PATH="${BASH_REMATCH[1]}"
if [[ $IMG_PATH != http* ]]; then
# Remove leading slash if present
IMG_PATH=${IMG_PATH#/}
mkdir -p "public/readme-assets/$(dirname "$IMG_PATH")"
curl -H "Accept: application/vnd.github.v3.raw" \
"https://raw.githubusercontent.com/$OWNER/$REPO/main/$IMG_PATH" \
--create-dirs -o "public/readme-assets/$IMG_PATH"
# Don't modify the path in the markdown
line=${line//$IMG_PATH/$IMG_PATH}
fi
fi
echo "$line" >> "$TEMP_README.new"
done < "$TEMP_README"
mv "$TEMP_README.new" "$TEMP_README"
# Process HTML image syntax <img src="path" />
while IFS= read -r line; do
if [[ $line =~ \<img.*src=\"([^\"]+)\" ]]; then
IMG_PATH="${BASH_REMATCH[1]}"
if [[ $IMG_PATH != http* ]]; then
# Remove leading slash if present
IMG_PATH=${IMG_PATH#/}
mkdir -p "public/readme-assets/$(dirname "$IMG_PATH")"
curl -H "Accept: application/vnd.github.v3.raw" \
"https://raw.githubusercontent.com/$OWNER/$REPO/main/$IMG_PATH" \
--create-dirs -o "public/readme-assets/$IMG_PATH"
# Don't modify the path in the markdown
line=${line//$IMG_PATH/$IMG_PATH}
fi
fi
echo "$line" >> "$TEMP_README.new"
done < "$TEMP_README"
mv "$TEMP_README.new" public/readme.md
rm "$TEMP_README"
- name: Commit and push if changed
run: |
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
git add public/readme.md public/readme-assets
git diff --quiet && git diff --staged --quiet || (git commit -m "Update README content and assets" && git push)