Skip to content

Auto Sync from HackMD #31

Auto Sync from HackMD

Auto Sync from HackMD #31

Workflow file for this run

name: Auto Sync from HackMD
# 觸發條件:1. 每天早上 8 點自動跑 2. 手動按按鈕也可以跑
on:
schedule:
- cron: '0 0 * * *' # UTC 0點 = 台灣早上8點
workflow_dispatch:
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Run Sync Script
run: |
import urllib.request
import os
# ---------------------------------------------
# 1. 設定你的 HackMD 頁面 ID 對照表
# 格式:"GitHub上的檔名": "HackMD的ID"
# 請把下面的 ID 換成你真正的 ID!
# ---------------------------------------------
pages = {
"index": "HJHiobaNZl",
"people": "rJ_g3zp4We",
"research": "rybtWmp4We",
"impact": "S1V_vE1S-g",
"publications": "ByoaZXaVZe",
"news": "rJ7SMmpEWe",
"opportunities": "ByPcG7aEWg",
"ai-mandarin-partner": "S157R4Jrbg",
"pi": "ByDTXybrbg",
"course_dhta": "B1zA7BZrWx",
"tap": "SylDrAbBZx",
"collaboration": "BJzFlYGrWl"
}
# 你的 HackMD 用戶名 (網址 @ 後面那一串)
hackmd_user = "@o_SxVezFSQGGbCkSyIX1qg"
# 這是我們要自動插入的 CSS 樣式
css_style = """
<style>
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; }
a { color: #0366d6; text-decoration: none; }
a:hover { text-decoration: underline; }
table { width: 100%; border-collapse: collapse; }
img { max-width: 100%; }
</style>
"""
# 開始處理每一個檔案
for filename, md_id in pages.items():
print(f"Downloading {filename} from {md_id}...")
# 下載 HackMD 內容
url = f"https://hackmd.io/{hackmd_user}/{md_id}/download"
try:
with urllib.request.urlopen(url) as response:
content = response.read().decode('utf-8')
# 自動替換連結:把 HackMD 的網址換成 GitHub 的 .html
for f_name, f_id in pages.items():
# 替換完整網址
content = content.replace(f"https://hackmd.io/{hackmd_user}/{f_id}", f"{f_name}.html")
# 替換短網址 (有些 HackMD 連結是短的)
content = content.replace(f"https://hackmd.io/{f_id}", f"{f_name}.html")
# 在最上面插入 CSS
final_content = css_style + "\n\n" + content
# 存檔
with open(f"{filename}.md", "w", encoding="utf-8") as f:
f.write(final_content)
except Exception as e:
print(f"Error downloading {filename}: {e}")
shell: python
- name: Commit and Push
run: |
git config --global user.name 'GitHub Actions Bot'
git config --global user.email 'actions@github.com'
git add *.md
# 如果有變更才 Commit,沒變更就不動作
git diff --quiet && git diff --staged --quiet || (git commit -m "Auto-sync from HackMD" && git push)