Skip to content

Commit 23e7f7f

Browse files
authored
Create main.yml
0 parents  commit 23e7f7f

1 file changed

Lines changed: 91 additions & 0 deletions

File tree

.github/workflows/main.yml

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
name: Auto Sync from HackMD
2+
3+
# 觸發條件:1. 每天早上 8 點自動跑 2. 手動按按鈕也可以跑
4+
on:
5+
schedule:
6+
- cron: '0 0 * * *' # UTC 0點 = 台灣早上8點
7+
workflow_dispatch:
8+
9+
jobs:
10+
build-and-deploy:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout Code
14+
uses: actions/checkout@v3
15+
16+
- name: Set up Python
17+
uses: actions/setup-python@v4
18+
with:
19+
python-version: '3.9'
20+
21+
- name: Run Sync Script
22+
run: |
23+
import urllib.request
24+
import os
25+
26+
# ---------------------------------------------
27+
# 1. 設定你的 HackMD 頁面 ID 對照表
28+
# 格式:"GitHub上的檔名": "HackMD的ID"
29+
# 請把下面的 ID 換成你真正的 ID!
30+
# ---------------------------------------------
31+
pages = {
32+
"index": "HJHiobaNZl",
33+
"people": "rJ_g3zp4We",
34+
"research": "rybtWmp4We",
35+
"impact": "S1V_vE1S-g",
36+
"publications": "ByoaZXaVZe",
37+
"news": "rJ7SMmpEWe",
38+
"joinus": "ByPcG7aEWg",
39+
"contact": "HyK0M7pNZl"
40+
}
41+
42+
# 你的 HackMD 用戶名 (網址 @ 後面那一串)
43+
hackmd_user = "@o_SxVezFSQGGbCkSyIX1qg"
44+
45+
# 這是我們要自動插入的 CSS 樣式
46+
css_style = """
47+
<style>
48+
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif; max-width: 1000px; margin: 0 auto; padding: 20px; line-height: 1.6; color: #333; }
49+
a { color: #0366d6; text-decoration: none; }
50+
a:hover { text-decoration: underline; }
51+
table { width: 100%; border-collapse: collapse; }
52+
img { max-width: 100%; }
53+
</style>
54+
"""
55+
56+
# 開始處理每一個檔案
57+
for filename, md_id in pages.items():
58+
print(f"Downloading {filename} from {md_id}...")
59+
60+
# 下載 HackMD 內容
61+
url = f"https://hackmd.io/{hackmd_user}/{md_id}/download"
62+
try:
63+
with urllib.request.urlopen(url) as response:
64+
content = response.read().decode('utf-8')
65+
66+
# 自動替換連結:把 HackMD 的網址換成 GitHub 的 .html
67+
for f_name, f_id in pages.items():
68+
# 替換完整網址
69+
content = content.replace(f"https://hackmd.io/{hackmd_user}/{f_id}", f"{f_name}.html")
70+
# 替換短網址 (有些 HackMD 連結是短的)
71+
content = content.replace(f"https://hackmd.io/{f_id}", f"{f_name}.html")
72+
73+
# 在最上面插入 CSS
74+
final_content = css_style + "\n\n" + content
75+
76+
# 存檔
77+
with open(f"{filename}.md", "w", encoding="utf-8") as f:
78+
f.write(final_content)
79+
80+
except Exception as e:
81+
print(f"Error downloading {filename}: {e}")
82+
83+
shell: python
84+
85+
- name: Commit and Push
86+
run: |
87+
git config --global user.name 'GitHub Actions Bot'
88+
git config --global user.email 'actions@github.com'
89+
git add *.md
90+
# 如果有變更才 Commit,沒變更就不動作
91+
git diff --quiet && git diff --staged --quiet || (git commit -m "Auto-sync from HackMD" && git push)

0 commit comments

Comments
 (0)