Skip to content

Commit d749cb8

Browse files
committed
feat(site): auto-discover top-level domains instead of hardcoding
1 parent 9a9a27c commit d749cb8

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

‎scripts/build_site.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,49 @@
3636
("Server", "服务端", "Server", "Java / 数据库 / 中间件 / 运维(规划中)", "🗄️"),
3737
]
3838

39+
# 自动发现额外技术域:根目录下未被上面声明、但确实含有 .md 的目录,
40+
# 会自动补充为一个技术域,这样以后新增 Python/、Rust/ 等顶层目录无需改脚本。
41+
_ROOT_SKIP = {"scripts", "docs", "node_modules", "assets"}
42+
_MANUAL_DOMAINS = list(DOMAINS)
43+
44+
45+
def _discover_domains():
46+
known = {d[0] for d in _MANUAL_DOMAINS}
47+
try:
48+
entries = sorted(os.listdir(ROOT))
49+
except OSError:
50+
return list(_MANUAL_DOMAINS)
51+
52+
found = []
53+
for name in entries:
54+
if name in known or name in _ROOT_SKIP or name.startswith("."):
55+
continue
56+
path = os.path.join(ROOT, name)
57+
if not os.path.isdir(path):
58+
continue
59+
if not any(f.endswith(".md") for _, _, fs in os.walk(path) for f in fs):
60+
continue
61+
# 用该目录 README.md 的一级标题作为显示名,取不到就退化为目录名
62+
label = name
63+
readme = os.path.join(path, "README.md")
64+
if os.path.isfile(readme):
65+
try:
66+
with open(readme, encoding="utf-8", errors="ignore") as fh:
67+
for line in fh:
68+
s = line.strip()
69+
if s.startswith("# "):
70+
label = s[2:].strip() or name
71+
break
72+
if s and not s.startswith("#"):
73+
break
74+
except OSError:
75+
pass
76+
found.append((name, label, name, f"{label} 相关笔记", "📄"))
77+
return list(_MANUAL_DOMAINS) + found
78+
79+
80+
DOMAINS = _discover_domains()
81+
3982
ANDROID_ORDER = [
4083
"BasicKnowledge", "AdavancedPart", "SourceAnalysis", "VideoDevelopment",
4184
"Jetpack", "KotlinCourse", "Dagger2", "ImageLoaderLibrary", "RxJavaPart",

0 commit comments

Comments
 (0)