43 lines
1.4 KiB
Python
43 lines
1.4 KiB
Python
import os
|
|
import re
|
|
import json
|
|
|
|
base_dir = r"e:\Wiki\2nd"
|
|
wiki_dir = os.path.join(base_dir, "10_Wiki")
|
|
graph_path = os.path.join(base_dir, "20_Meta", "Graph.json")
|
|
|
|
def rebuild_graph():
|
|
nodes = []
|
|
links = []
|
|
|
|
# 1. Scan for all files and create nodes
|
|
all_files = []
|
|
for root, dirs, files in os.walk(wiki_dir):
|
|
for file in files:
|
|
if file.endswith(".md"):
|
|
rel_path = os.path.relpath(os.path.join(root, file), base_dir)
|
|
title = file.replace(".md", "")
|
|
all_files.append((title, rel_path))
|
|
nodes.append({"id": title, "path": rel_path})
|
|
|
|
# 2. Scan content for links [[...]]
|
|
titles = [n["id"] for n in nodes]
|
|
for title, rel_path in all_files:
|
|
full_path = os.path.join(base_dir, rel_path)
|
|
with open(full_path, "r", encoding="utf-8") as f:
|
|
content = f.read()
|
|
|
|
found_links = re.findall(r'\[\[(.*?)\]\]', content)
|
|
for link in found_links:
|
|
if link in titles and link != title:
|
|
links.append({"source": title, "target": link})
|
|
|
|
graph = {"nodes": nodes, "links": links}
|
|
with open(graph_path, "w", encoding="utf-8") as f:
|
|
json.dump(graph, f, indent=2, ensure_ascii=False)
|
|
|
|
print(f"Graph rebuilt: {len(nodes)} nodes, {len(links)} links.")
|
|
|
|
if __name__ == "__main__":
|
|
rebuild_graph()
|