-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-frontmatter.py
45 lines (32 loc) · 1.22 KB
/
generate-frontmatter.py
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
38
39
40
41
42
43
44
45
import os
import re
from pathlib import Path
FRONTMATTER_PATTERN = re.compile(r"^---\n(.*?)\n---\n", re.DOTALL)
TITLE_PATTERN = re.compile(r"^# (.+)", re.MULTILINE)
SUBTITLE_PATTERN = re.compile(r"<div class='subtitle'>(.*?)</div>", re.DOTALL)
def extract_or_insert_frontmatter(filepath):
with open(filepath, "r", encoding="utf-8") as f:
content = f.read()
if FRONTMATTER_PATTERN.match(content):
return # Already has frontmatter
title_match = TITLE_PATTERN.search(content)
subtitle_match = SUBTITLE_PATTERN.search(content)
if not title_match or not subtitle_match:
print(f"Skipping {filepath}, missing title or subtitle")
return
title = title_match.group(1).strip()
subtitle = subtitle_match.group(1).strip()
frontmatter = f"""---
title: {title}
description: {subtitle}
---\n\n"""
new_content = frontmatter + content
with open(filepath, "w", encoding="utf-8") as f:
f.write(new_content)
print(f"Inserted frontmatter into {filepath}")
def traverse_docs_and_process():
docs_path = Path("./docs")
for md_file in docs_path.rglob("*.md"):
extract_or_insert_frontmatter(md_file)
if __name__ == "__main__":
traverse_docs_and_process()