Skip to content

Commit 1b875e9

Browse files
committed
Use pyyaml for YAML frontmatter generation in note.py
Replace manual YAML construction with yaml.dump()
1 parent b1b72d1 commit 1b875e9

1 file changed

Lines changed: 12 additions & 13 deletions

File tree

.opencode/bin/note.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,37 @@
1212
uv run note --title "My Note" [--slug my-note] [--tags "tag1,tag2"] < content.txt
1313
uv run note --title "My Note" --save < content.txt
1414
"""
15+
import re
1516
import sys
1617
from datetime import datetime
1718
from pathlib import Path
1819

1920
import typer
21+
import yaml
2022

2123
app = typer.Typer(help="Create structured notes")
2224
NOTES_DIR = Path(".knowledge/notes")
2325

2426

2527
def slugify(title: str) -> str:
2628
"""Convert title to URL-safe slug."""
27-
import re
2829
return re.sub(r"[^a-z0-9]+", "-", title.lower()).strip("-")
2930

3031

3132
def format_note(title: str, slug: str, tags: list[str], content: str) -> str:
3233
"""Format note with YAML frontmatter."""
3334
date = datetime.now().strftime("%Y-%m-%d")
3435

35-
lines = ["---"]
36-
lines.append(f'title: "{title}"')
37-
lines.append(f"slug: {slug}")
38-
lines.append(f"date: {date}")
39-
lines.append("tags:")
40-
for tag in tags:
41-
lines.append(f" - {tag}")
42-
lines.append("---")
43-
lines.append("")
44-
lines.append(f"# {title}")
45-
lines.append("")
46-
lines.append(content)
36+
frontmatter = {
37+
"title": title,
38+
"slug": slug,
39+
"date": date,
40+
"tags": tags,
41+
}
42+
43+
yaml_header = yaml.dump(frontmatter, sort_keys=False, allow_unicode=True, default_flow_style=False)
44+
45+
lines = ["---", yaml_header.rstrip(), "---", "", f"# {title}", "", content]
4746

4847
return "\n".join(lines)
4948

0 commit comments

Comments
 (0)