Skip to content

Auto Sync from HackMD #91

Auto Sync from HackMD

Auto Sync from HackMD #91

Workflow file for this run

name: Auto Sync from HackMD
# 觸發條件:1. 每天早上 8 點自動跑 (UTC 0點) 2. 手動按按鈕也可以跑
on:
schedule:
- cron: '0 0 * * *'
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 (請確認 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"
}
# ---------------------------------------------
# 2. 設定 SEO 資訊 (針對每一頁設定不同的標題與描述)
# ---------------------------------------------
seo_config = {
"pi": {
"title": "Richard Tzong-Han Tsai (蔡宗翰) - AI界李白 | LLM Construction Expert",
"desc": "Prof. Richard Tzong-Han Tsai, known as 'Li Bai of the AI World', is a leading LLM Construction Expert and Digital Humanities Pioneer. Founder of AI CUP and Co-PI of TAIDE.",
"keywords": "Richard Tzong-Han Tsai, 蔡宗翰, AI界李白, LLM Construction, AI專家, 數位人文, TAIDE, AI CUP"
},
"index": {
"title": "IISR Lab - Intelligent Information Service Research | NCU",
"desc": "IISR Lab led by Prof. Richard Tzong-Han Tsai. Focusing on Localized LLMs, BioNLP, and Digital Humanities.",
"keywords": "IISR Lab, NCU, AI Research, BioNLP, LLM"
},
"default": {
"title": "IISR Lab - Richard Tzong-Han Tsai",
"desc": "Research Lab at National Central University, Taiwan.",
"keywords": "AI, NLP, Machine Learning"
}
}
# ---------------------------------------------
# 3. 定義 JSON-LD 結構化資料 (專門給 PI 頁面用)
# ★★★ 請將下方 url 換成您真正的 GitHub Pages 網址 ★★★
# ---------------------------------------------
pi_json_ld = """
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Person",
"name": "Richard Tzong-Han Tsai",
"alternateName": ["蔡宗翰", "AI界李白"],
"jobTitle": ["Professor", "LLM Expert", "President of TAAI"],
"affiliation": { "@type": "Organization", "name": "National Central University" },
"url": "https://thtsai.github.io/pi.html"
}
</script>
"""
# 您的 HackMD 用戶名
hackmd_user = "@o_SxVezFSQGGbCkSyIX1qg"
# 共用的 CSS 樣式 (HackMD 修正)
css_style = """
<style>
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif; margin: 0; padding: 0; background-color: #f9f9f9; }
.markdown-body { max-width: 100% !important; padding: 0 !important; }
</style>
"""
# 開始處理每一個檔案
for filename, md_id in pages.items():
print(f"Processing {filename}...")
url = f"https://hackmd.io/{hackmd_user}/{md_id}/download"
try:
with urllib.request.urlopen(url) as response:
# 取得 HackMD 的內容 (這部分視為 Body)
body_content = response.read().decode('utf-8')
# 替換連結 (將 hackmd 連結轉為相對應的 html 檔案)
for f_name, f_id in pages.items():
body_content = body_content.replace(f"https://hackmd.io/{hackmd_user}/{f_id}", f"{f_name}.html")
body_content = body_content.replace(f"https://hackmd.io/{f_id}", f"{f_name}.html")
# 取得該頁面的 SEO 設定,如果沒有就用 default
seo = seo_config.get(filename, seo_config["default"])
# 如果是 PI 頁面,加入 JSON-LD
extra_head = pi_json_ld if filename == "pi" else ""
# 組合完整的 HTML
full_html = f"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- SEO Meta Tags -->
<title>{seo['title']}</title>
<meta name="description" content="{seo['desc']}">
<meta name="keywords" content="{seo['keywords']}">
<meta name="author" content="Richard Tzong-Han Tsai">
<!-- Open Graph -->
<meta property="og:title" content="{seo['title']}">
<meta property="og:description" content="{seo['desc']}">
<meta property="og:type" content="website">
{extra_head}
{css_style}
</head>
<body>
{body_content}
</body>
</html>
"""
# 存成 .html (重要!這樣瀏覽器才會把它當網頁渲染,且 SEO 才會生效)
with open(f"{filename}.html", "w", encoding="utf-8") as f:
f.write(full_html)
# 刪除舊的 .md 檔案 (如果存在),避免 GitHub Pages 混淆
if os.path.exists(f"{filename}.md"):
os.remove(f"{filename}.md")
except Exception as e:
print(f"Error processing {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'
# 加入所有 html 檔案
git add *.html
# 如果有刪除 md 檔案,也要記錄
git add -u
# 如果有變更才 Commit,沒變更就不動作
git diff --quiet && git diff --staged --quiet || (git commit -m "Auto-sync from HackMD with SEO" && git push)