Skip to content

Commit 66cd6b2

Browse files
committed
refactor(tree_renderer, lint): use shared frontmatter module
1 parent bfc0542 commit 66cd6b2

2 files changed

Lines changed: 6 additions & 19 deletions

File tree

openkb/lint.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import yaml
1717

18+
from openkb import frontmatter
1819
from openkb.schema import PAGE_CONTENT_DIRS
1920

2021
# Matches [[wikilink]] or [[subdir/link]]
@@ -500,16 +501,7 @@ def find_missing_okf_fields(wiki: Path) -> list[str]:
500501
if not subdir_path.exists():
501502
continue
502503
for path in sorted(subdir_path.glob("*.md")):
503-
text = _read_md(path)
504-
fm = None
505-
if text.startswith("---"):
506-
end = text.find("\n---", 3)
507-
if end != -1:
508-
try:
509-
fm = yaml.safe_load(text[3:end].strip("\n"))
510-
except yaml.YAMLError:
511-
fm = None
512-
fm = fm if isinstance(fm, dict) else {}
504+
fm = frontmatter.parse(_read_md(path))
513505
rel = path.relative_to(wiki)
514506
type_val = fm.get("type")
515507
if not (isinstance(type_val, str) and type_val.strip()):

openkb/tree_renderer.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
"""Markdown renderers for PageIndex tree structures."""
22
from __future__ import annotations
33

4-
import json
5-
6-
7-
def _yaml_kv_line(key: str, value: str) -> str:
8-
"""Return a single YAML key-value line with the value JSON-quoted."""
9-
return f"{key}: {json.dumps(value, ensure_ascii=False)}"
4+
from openkb import frontmatter
105

116

127
def _yaml_frontmatter(source_name: str, doc_id: str, description: str = "") -> str:
138
"""Return a YAML frontmatter block for a PageIndex wiki page."""
14-
lines = [_yaml_kv_line("type", "Summary")]
9+
lines = [frontmatter.kv_line("type", "Summary")]
1510
if description:
16-
lines.append(_yaml_kv_line("description", description))
11+
lines.append(frontmatter.kv_line("description", description))
1712
lines.append("doc_type: pageindex")
18-
lines.append(_yaml_kv_line("full_text", f"sources/{source_name}.json"))
13+
lines.append(frontmatter.kv_line("full_text", f"sources/{source_name}.json"))
1914
return "---\n" + "\n".join(lines) + "\n---\n"
2015

2116

0 commit comments

Comments
 (0)