Skip to content

Commit 979ce57

Browse files
committed
feat: encode data in url instead of json, use for-the-badge style format
1 parent 423ca00 commit 979ce57

File tree

3 files changed

+26
-43
lines changed

3 files changed

+26
-43
lines changed

.github/workflows/update-badge.yml

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ jobs:
9797
echo "visitors=$visitors" >> $GITHUB_OUTPUT
9898
echo "formatted_visitors=$formatted_visitors" >> $GITHUB_OUTPUT
9999
100-
- name: Create Badge Data
100+
- name: Update README Badges
101101
run: |
102-
echo "Creating badge files..."
102+
echo "Updating README with dynamic badge URLs..."
103103
104104
# Get values from previous step
105105
pageviews_value="${{ steps.analytics.outputs.formatted_pageviews }}"
@@ -108,40 +108,37 @@ jobs:
108108
echo "Using pageviews: $pageviews_value"
109109
echo "Using visitors: $visitors_value"
110110
111-
# Create pageviews badge data JSON file (dark purple)
112-
cat > badge-data-pageviews.json << EOF
113-
{
114-
"schemaVersion": 1,
115-
"label": "Total Views",
116-
"message": "$pageviews_value",
117-
"color": "2d0e2d",
118-
"cacheSeconds": 86400
119-
}
120-
EOF
111+
# URL encode the values (replace spaces and special chars)
112+
pageviews_encoded=$(echo "$pageviews_value" | sed 's/ /%20/g')
113+
visitors_encoded=$(echo "$visitors_value" | sed 's/ /%20/g')
121114
122-
# Create unique visitors badge data JSON file (light purple)
123-
cat > badge-data-visitors.json << EOF
124-
{
125-
"schemaVersion": 1,
126-
"label": "Unique Visitors",
127-
"message": "$visitors_value",
128-
"color": "c8a3c8",
129-
"cacheSeconds": 86400
130-
}
131-
EOF
115+
# Update the README.md with new badge URLs (for-the-badge style)
116+
# Look for existing badge lines and replace them, or add them if not found
117+
if grep -q "Total%20Views" README.md; then
118+
# Replace existing pageviews badge
119+
sed -i "s|!\[Total Views\].*|![Total Views](https://img.shields.io/badge/Total%20Views-${pageviews_encoded}-2d0e2d?style=for-the-badge)|" README.md
120+
else
121+
# Add pageviews badge if not found (after # OpenWashData Website line)
122+
sed -i '/# OpenWashData Website/a\\n![Total Views](https://img.shields.io/badge/Total%20Views-'"${pageviews_encoded}"'-2d0e2d?style=for-the-badge)' README.md
123+
fi
124+
125+
if grep -q "Unique%20Visitors" README.md; then
126+
# Replace existing visitors badge
127+
sed -i "s|!\[Unique Visitors\].*|![Unique Visitors](https://img.shields.io/badge/Unique%20Visitors-${visitors_encoded}-c8a3c8?style=for-the-badge)|" README.md
128+
else
129+
# Add visitors badge if not found (after pageviews badge)
130+
sed -i '/Total%20Views.*-2d0e2d/a![Unique Visitors](https://img.shields.io/badge/Unique%20Visitors-'"${visitors_encoded}"'-c8a3c8?style=for-the-badge)' README.md
131+
fi
132132
133-
echo "Badge files created successfully:"
134-
ls -la badge-data-*.json
135-
echo "Content of pageviews badge:"
136-
cat badge-data-pageviews.json
137-
echo "Content of visitors badge:"
138-
cat badge-data-visitors.json
133+
echo "README updated with new badge URLs"
134+
echo "Updated badges:"
135+
grep -E "!\[(Total Views|Unique Visitors)\]" README.md || echo "No badges found in README"
139136
140137
- name: Commit and Push Changes
141138
run: |
142139
git config --local user.email "[email protected]"
143140
git config --local user.name "GitHub Action"
144-
git add badge-data-pageviews.json badge-data-visitors.json
141+
git add README.md
145142
146143
# Use custom commit message if provided, otherwise use default
147144
if [ -n "${{ github.event.inputs.custom_message }}" ]; then

badge-data-pageviews.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

badge-data-visitors.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)