diff options
| author | Eltrac <hi@guhub.cn> | 2025-08-17 23:29:47 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-08-17 23:29:47 +0800 |
| commit | 40881cb3a641503c9dc1bba30685b3af99fe4e63 (patch) | |
| tree | 20e0915d176da56f8be433d25d6318b0e30d030e | |
| parent | 67f539543e7e3feeb352982b7f328c1031fc53f6 (diff) | |
feat: add a page listing all highlights
Former-commit-id: 621f7e411f31e8c584209d7f0a37195c3c7f1e8c
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | config/_default/params.zh.yaml | 2 | ||||
| -rw-r--r-- | content/highlights.md | 8 | ||||
| -rw-r--r-- | layouts/highlights.html | 23 | ||||
| -rw-r--r-- | scripts/highlights.py | 61 | ||||
| -rw-r--r-- | scripts/run_scripts.py | 12 |
6 files changed, 107 insertions, 0 deletions
@@ -14,3 +14,4 @@ content/posts/_template.md static/link-graph.json data/externallinks.json assets/css/uno.css +data/highlights.json diff --git a/config/_default/params.zh.yaml b/config/_default/params.zh.yaml index a5f4e8dd..9b5e0f26 100644 --- a/config/_default/params.zh.yaml +++ b/config/_default/params.zh.yaml @@ -17,5 +17,7 @@ nav: footerInternalLinks: - name: 林卷 link: /linkroll/ + - name: 标记 + link: /highlights/ - name: 赞助 link: /sponsor/ diff --git a/content/highlights.md b/content/highlights.md new file mode 100644 index 00000000..9d5e5792 --- /dev/null +++ b/content/highlights.md @@ -0,0 +1,8 @@ +--- +title: 标记 +layout: highlights +--- + +一些文字从语境中抽离出来依然能保留其价值。 + +--- diff --git a/layouts/highlights.html b/layouts/highlights.html new file mode 100644 index 00000000..eecd06df --- /dev/null +++ b/layouts/highlights.html @@ -0,0 +1,23 @@ +{{ define "main" }} + <article> + {{ partial "header" . }} + <h1 class="term-title">{{ .Title }}</h1> + <div class="post-content markdown-body"> + <p>{{ .Content }}</p> + </div> + <!-- Highlights --> + {{ range shuffle site.Data.highlights }} + <section class="post-content"> + <h3><a href="{{ .url }}">《{{ .title }}》</a></h3> + <ul class="list-none"> + {{ range .highlights }} + <li class="my-2 text-lg"> + <mark class="bg-amber-2"><a href="{{ .link }}">{{ .text | plainify }}</a></mark> + </li> + {{ end }} + </ul> + </section> + {{ end }} + </article> +{{ partial "footer.html" . }} +{{ end }} diff --git a/scripts/highlights.py b/scripts/highlights.py new file mode 100644 index 00000000..102ff02b --- /dev/null +++ b/scripts/highlights.py @@ -0,0 +1,61 @@ +import os +import re +import json +import urllib.parse + +CONTENT_DIR = "content" +OUTPUT_FILE = "data/highlights.json" + +# 匹配 Hugo 文章中的 ==高亮== +highlight_pattern = re.compile(r"==(.+?)==") + +# 匹配 front matter(支持 yaml 格式) +title_pattern = re.compile(r'^title:\s*["\']?(.+?)["\']?\s*$', re.MULTILINE) +slug_pattern = re.compile(r'^slug:\s*["\']?(.+?)["\']?\s*$', re.MULTILINE) + +results = [] + +for root, _, files in os.walk(CONTENT_DIR): + for f in files: + if f.endswith(".md"): + path = os.path.join(root, f) + + with open(path, "r", encoding="utf-8") as file: + content = file.read() + + # 提取 title + title_match = title_pattern.search(content) + title = title_match.group(1) if title_match else os.path.basename(path) + + # 提取 slug(如果有就替换默认 URL) + slug_match = slug_pattern.search(content) + if slug_match: + slug = slug_match.group(1).strip("/") + # 用目录推断 section,比如 content/post/abc.md => /post/slug/ + rel_dir = os.path.relpath(root, CONTENT_DIR).replace("\\", "/") + url = f"/{rel_dir}/{slug}/" if rel_dir != "." else f"/{slug}/" + else: + # 默认 Hugo URL 规则:相对路径去掉 .md + url = "/" + os.path.relpath(path, CONTENT_DIR).replace("\\", "/").replace(".md", "/") + + # 提取高亮 + highlights = highlight_pattern.findall(content) + if highlights: + highlight_links = [] + for h in highlights: + encoded = urllib.parse.quote(h, safe="") + link = f"{url}#:~:text={encoded}" + highlight_links.append({"text": h, "link": link}) + + results.append({ + "title": title, + "url": url, + "highlights": highlight_links + }) + +# 写入 data 文件夹 +os.makedirs(os.path.dirname(OUTPUT_FILE), exist_ok=True) +with open(OUTPUT_FILE, "w", encoding="utf-8") as f: + json.dump(results, f, ensure_ascii=False, indent=2) + +print(f"✔ 提取完成,共 {len(results)} 篇文章有高亮,已写入 {OUTPUT_FILE}") diff --git a/scripts/run_scripts.py b/scripts/run_scripts.py new file mode 100644 index 00000000..71764a11 --- /dev/null +++ b/scripts/run_scripts.py @@ -0,0 +1,12 @@ +import os +import subprocess +import sys + +SCRIPTS_DIR = "scripts" +SELF = os.path.basename(__file__) + +for filename in sorted(os.listdir(SCRIPTS_DIR)): + if filename.endswith(".py") and filename != SELF: + path = os.path.join(SCRIPTS_DIR, filename) + print(f"▶ Running {path} ...") + subprocess.run([sys.executable, path], check=True) |
