-
Notifications
You must be signed in to change notification settings - Fork 2
186 lines (158 loc) · 5.56 KB
/
Hugo-PaperMod.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
name: Deploy Hugo site to Pages
on:
push:
branches: [ main ]
schedule:
- cron: '0 2 * * *' # UTC 每天2点
workflow_dispatch:
permissions:
contents: read
pages: write
id-token: write
concurrency:
group: "pages"
cancel-in-progress: false
jobs:
build-deploy:
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
env:
CUSTOM_DOMAIN: ${{ github.event.repository.pages.cname }}
steps:
# 1. 检出代码
- name: Checkout
uses: actions/checkout@v4
with:
submodules: 'recursive'
# 2. 设置 Hugo
- name: Setup Hugo
uses: peaceiris/actions-hugo@v2
with:
hugo-version: 'latest'
extended: true
# 3. 配置 GitHub Pages
- name: Setup Pages
uses: actions/configure-pages@v4
# 4. 下载 PaperMod 主题 & 处理文档
- name: Download PaperMod theme
run: |
mkdir -p themes
git clone https://github.com/adityatelange/hugo-PaperMod.git themes/PaperMod
# 这里的重点:把所有 .md 文件都写为 type = "post"
generate_front_matter() {
local file="$1"
local title=$(basename "$file" .md)
local date=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
local category=$(dirname "$file" | sed 's|^./||')
# 如果已有 front matter,就直接复制
if sed -n '1{/^---$/h}; /^---$/{x;p;q}; H' "$file" | grep -q "^---$"; then
cp "$file" "content/$category/$title.md"
return
fi
# 若没有 front matter,自动生成
{
echo "---"
echo "title: \"$title\""
echo "date: $date"
# 想按目录来区分分类,可保留这一行
echo "categories: [\"$category\"]"
# ★★ 修复点:统一指定所有文章的 type = "post" ★★
echo "type: \"post\""
echo "---"
cat "$file"
} > "content/$category/$title.md"
}
# 遍历所有 md 文件(排除 README.md)
find . -type f -name "*.md" ! -name "README.md" | while read file; do
dir=$(dirname "$file" | sed 's|^./||')
mkdir -p "content/$dir"
generate_front_matter "$file"
done
# 5. 获取自定义域名
- name: Retrieve custom domain
id: fetch-domain
run: |
customDomain=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
https://api.github.com/repos/${{ github.repository_owner }}/${{ github.event.repository.name }}/pages \
| jq -r '.cname // ""')
echo "CUSTOM_DOMAIN=$customDomain" >> $GITHUB_ENV
# 6. 创建并写入 Hugo 配置
- name: Configure Hugo
run: |
if [ -n "$CUSTOM_DOMAIN" ]; then
domain="$CUSTOM_DOMAIN"
else
owner="${GITHUB_REPOSITORY_OWNER}"
repo="$(echo $GITHUB_REPOSITORY | cut -d'/' -f2)"
domain="$owner.github.io/$repo"
fi
cat > config.toml << EOL
baseURL = "https://$domain"
languageCode = "zh-cn"
title = "${{ github.repository_owner }}'s Space"
theme = "PaperMod"
[params]
# 指定 mainSections 只有一个: "post"
# 因为我们在上面把所有文件都设为 type = "post"
mainSections = ["post"]
defaultTheme = "auto"
ShowReadingTime = true
ShowShareButtons = false
ShowPostNavLinks = true
ShowBreadCrumbs = true
ShowCodeCopyButtons = true
ShowToc = true
[params.homeInfoParams]
Title = "欢迎来到 ${{ github.repository_owner }} 的文档站"
Content = "在喧嚣时代中静心思考,用文字传递理性与希望。"
[[params.socialIcons]]
name = "github"
url = "https://github.com/${{ github.repository_owner }}"
[outputs]
home = ["HTML", "RSS", "JSON"]
# 想自定义分类和标签名称,可在这里改
[taxonomies]
category = "categories"
tag = "tags"
[menu]
main = [
{identifier = "archives", name = "Archives", url = "/archives/", weight = 10},
{identifier = "categories", name = "Categories", url = "/categories/", weight = 20},
{identifier = "search", name = "Search", url = "/search/", weight = 30},
{identifier = "tags", name = "Tags", url = "/tags/", weight = 40}
]
EOL
# 至少留个 content 目录
mkdir -p content
# 搜索页面
cat > content/search.md << EOL
---
title: "Search"
layout: "search"
summary: "search"
placeholder: "搜索文章..."
---
EOL
# 归档页面
cat > content/archives.md << EOL
---
title: "Archives"
layout: "archives"
url: "/archives/"
summary: "archives"
---
EOL
# 7. Hugo 构建
- name: Build site
run: hugo --minify
# 8. 上传构建产物
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: ./public
# 9. 部署到 GitHub Pages
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4