-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_linkset.py
More file actions
37 lines (27 loc) · 1 KB
/
gen_linkset.py
File metadata and controls
37 lines (27 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import json
import mkdocs_gen_files
def to_dir_url(site: str, path: str) -> str:
if path.endswith(".markdown"):
stem = path[: -len(".markdown")]
else:
stem = path[: -len(".md")]
stem = stem.rstrip("/")
if stem.endswith("/index") or stem == "index":
stem = stem[: -len("/index")] if stem.endswith("/index") else ""
url_path = f"{stem}/" if stem else ""
return f"{site}/{url_path}"
def main() -> None:
cfg = mkdocs_gen_files.config
site = cfg["site_url"].rstrip("/")
links = []
for page in mkdocs_gen_files.files:
if not (page.endswith(".md") or page.endswith(".markdown")):
continue
url = to_dir_url(site, page)
links.append({"rel": "item", "href": url})
data = {"linkset": [{"anchor": f"{site}/", "link": links}]}
for name in [".well-known/linkset", ".well-known/linkset.json"]:
with mkdocs_gen_files.open(name, "w") as f:
f.write(json.dumps(data))
if __name__ == "__main__":
main()