Skip to content

Commit d43ed71

Browse files
authored
Enhance workflow for generating notes index
Updated the workflow to generate a grouped notes index and commit changes if the README is modified.
1 parent 1ef0758 commit d43ed71

1 file changed

Lines changed: 57 additions & 23 deletions

File tree

.github/workflows/main.yml

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,60 +17,94 @@ jobs:
1717

1818
- name: Generate grouped notes index
1919
run: |
20+
# 查找所有 .md 文件,排除不需要的文件
2021
find . -type f -name "*.md" \
2122
! -path "./README.md" \
22-
! -path "./INDEX.md" \
23+
! -name "index.md" \ # 排除所有 index.md(包括子目录)
24+
! -name "INDEX.md" \ # 排除临时文件
2325
! -path "./.github/*" \
26+
! -path "./node_modules/*" \ # 常见排除项
2427
| sort \
2528
| awk '
26-
function url_encode(str, i, c, out) {
27-
out = ""
29+
# URL 编码函数(处理常见特殊字符)
30+
function urlencode(str, result, i, c, ord) {
31+
result = ""
2832
for (i = 1; i <= length(str); i++) {
2933
c = substr(str, i, 1)
30-
31-
if (c ~ /[A-Za-z0-9._~-]/) {
32-
out = out c
34+
ord = index("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~", c)
35+
if (ord > 0) {
36+
result = result c
37+
} else if (c == " ") {
38+
result = result "%20"
3339
} else {
34-
out = out "%" sprintf("%02X", ord_byte(c))
40+
# 对其他字符进行十六进制编码
41+
result = result "%" sprintf("%02X", ord(c))
3542
}
3643
}
37-
return out
38-
}
39-
40-
function ord_byte(c) {
41-
return and(255, ord = sprintf("%d", c))
44+
return result
4245
}
43-
4446
{
47+
# 移除开头的 "./"
4548
path = substr($0, 3)
4649
n = split(path, parts, "/")
47-
group = parts[1]
48-
50+
51+
# 确定分组:根目录文件归为 "Root" 组
52+
if (n == 1) {
53+
group = "Root"
54+
} else {
55+
group = parts[1]
56+
}
57+
58+
# 首次遇到该分组时输出标题
4959
if (!(group in seen)) {
50-
if (NR > 1) print ""
60+
if (length(seen) > 0) print "" # 分组间空行
5161
print "### " group
5262
seen[group] = 1
5363
}
54-
64+
65+
# 生成缩进(子目录层级)
5566
indent = ""
5667
for (i = 2; i < n; i++) indent = indent " "
57-
68+
69+
# 获取文件名(不含路径)
5870
file = parts[n]
59-
url = url_encode(path)
60-
61-
print indent "- [" file "](" url ")"
71+
72+
# 对完整路径进行 URL 编码
73+
encoded_path = urlencode(path)
74+
75+
# 输出 Markdown 链接
76+
print indent "- [" file "](" encoded_path ")"
6277
}' > INDEX.md
78+
79+
# 将生成的索引插入 README.md 的标记之间
80+
awk '
81+
BEGIN { in_block=0 }
82+
/<!-- AUTO-GENERATED-START -->/ {
83+
print
84+
while ((getline line < "INDEX.md") > 0) print line
85+
close("INDEX.md")
86+
in_block=1
87+
next
88+
}
89+
/<!-- AUTO-GENERATED-END -->/ {
90+
in_block=0
91+
print
92+
next
93+
}
94+
!in_block { print }
95+
' README.md > README.new.md
96+
97+
mv README.new.md README.md
98+
rm -f INDEX.md # 确保清理临时文件
6399
64100
- name: Commit and push if changed
65101
run: |
66102
if git diff --quiet; then
67103
echo "README unchanged"
68104
exit 0
69105
fi
70-
71106
git config user.name "github-actions[bot]"
72107
git config user.email "github-actions[bot]@users.noreply.github.com"
73-
74108
git add README.md
75109
git commit -m "docs: auto update notes index (grouped)"
76110
git push

0 commit comments

Comments
 (0)