summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEltrac <hi@guhub.cn>2025-05-23 16:09:01 +0800
committerEltrac <hi@guhub.cn>2025-05-23 16:09:01 +0800
commit60d68be1b4ea81b235b73924d2a98be67d1ebfc9 (patch)
tree8314a7be5c57367bed28ef13b33339b64ce72b14
parent40a4d57dd91c1d9464f4d944860553b2cdf74986 (diff)
feat: list external links ranking
also move python scripts to one folder
-rw-r--r--layouts/_default/external-links.html8
-rw-r--r--scripts/extractLinks.py (renamed from extractLinks.py)0
-rw-r--r--scripts/listExternalLinks.py53
3 files changed, 61 insertions, 0 deletions
diff --git a/layouts/_default/external-links.html b/layouts/_default/external-links.html
new file mode 100644
index 00000000..c07fdcb9
--- /dev/null
+++ b/layouts/_default/external-links.html
@@ -0,0 +1,8 @@
+{{ define "main" }}
+ <h2>Top External Links</h2>
+ <ol>
+ {{ range .Site.Data.externallinks }}
+ <li>{{ index . 0 }} — {{ index . 1 }} links</li>
+ {{ end }}
+ </ol>
+{{ end }}
diff --git a/extractLinks.py b/scripts/extractLinks.py
index eb172353..eb172353 100644
--- a/extractLinks.py
+++ b/scripts/extractLinks.py
diff --git a/scripts/listExternalLinks.py b/scripts/listExternalLinks.py
new file mode 100644
index 00000000..6b52ab76
--- /dev/null
+++ b/scripts/listExternalLinks.py
@@ -0,0 +1,53 @@
+import os
+from urllib.parse import urlparse
+from collections import Counter
+import json
+import re
+
+content_dir = "content"
+external_links = Counter()
+
+# 匹配 Markdown 中的 URL 链接 [text](url)
+markdown_link_re = re.compile(r'\[.*?\]\((http.*?)\)')
+
+def extract_front_matter_and_content(path):
+ with open(path, 'r', encoding='utf-8') as f:
+ lines = f.readlines()
+
+ if not lines or lines[0].strip() != '---':
+ return {}, ''.join(lines)
+
+ front_matter = {}
+ end_index = None
+ for i in range(1, len(lines)):
+ if lines[i].strip() == '---':
+ end_index = i
+ break
+ key_value = lines[i].split(':', 1)
+ if len(key_value) == 2:
+ key, value = key_value
+ front_matter[key.strip()] = value.strip()
+
+ content = ''.join(lines[end_index+1:]) if end_index else ''.join(lines)
+ return front_matter, content
+
+def extract_links_from_file(path):
+ _, content = extract_front_matter_and_content(path)
+
+ # 提取 Markdown 格式链接
+ for match in markdown_link_re.findall(content):
+ domain = urlparse(match).netloc
+ if domain:
+ external_links[domain] += 1
+
+for root, dirs, files in os.walk(content_dir):
+ for file in files:
+ if file.endswith(".md"):
+ extract_links_from_file(os.path.join(root, file))
+
+# 写入 JSON
+os.makedirs("data", exist_ok=True)
+with open("data/externallinks.json", "w", encoding="utf-8") as f:
+ json.dump(external_links.most_common(), f, indent=2, ensure_ascii=False)
+
+print("✅ External link data written to data/externallinks.json")