-
Notifications
You must be signed in to change notification settings - Fork 55
80 lines (67 loc) · 2.25 KB
/
update-pages.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
# .github/workflows/update-pages.yml
name: Update Pages List
on:
push:
branches: [ main ]
paths:
- '**.md'
workflow_dispatch:
permissions:
contents: write
pages: write
jobs:
update-pages:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
ref: main
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Update index.html
run: |
python3 - <<EOF
import os
import re
# Get all markdown files
md_files = [f for f in os.listdir('.') if f.endswith('.md')]
# Sort files alphabetically, but ensure README.md is first
md_files.sort()
if 'README.md' in md_files:
md_files.remove('README.md')
md_files.insert(0, 'README.md')
# Read the current index.html
with open('index.html', 'r') as f:
content = f.read()
# Create the new pages array string
pages_array = "const pages = [\n '" + "',\n '".join(md_files) + "'\n ];"
# Replace the existing pages array in index.html
new_content = re.sub(
r'const pages = \[[\s\S]*?\];',
pages_array,
content
)
# Write the updated content back to index.html
with open('index.html', 'w') as f:
f.write(new_content)
EOF
- name: Commit and push if changed
run: |
git config --global user.name 'GitHub Action'
git config --global user.email '[email protected]'
git add index.html
git diff --quiet && git diff --staged --quiet || (git commit -m "Update pages list" && git push)
- name: Checkout gh-pages
uses: actions/checkout@v3
with:
ref: gh-pages
clean: false
- name: Copy updated index.html to gh-pages
run: |
git checkout main -- index.html
git config --global user.name 'GitHub Action'
git config --global user.email '[email protected]'
git add index.html
git diff --quiet && git diff --staged --quiet || (git commit -m "Update pages list" && git push)