Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/scribae/io_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ def load_note(note_path: Path, *, max_chars: int) -> NoteDetails:
body = post.content.strip()
truncated_body, truncated = truncate(body, max_chars)

note_title = (
metadata.get("title") or metadata.get("name") or note_path.stem.replace("_", " ").replace("-", " ").title()
# Front matter is arbitrary YAML, so a `title:` may parse as a number, list, or blank.
note_title = _first_text(metadata.get("title"), metadata.get("name")) or (
note_path.stem.replace("_", " ").replace("-", " ").title()
)

return NoteDetails(
Expand Down Expand Up @@ -74,4 +75,12 @@ def truncate(value: str, max_chars: int) -> tuple[str, bool]:
return value[: max_chars - 1].rstrip() + " …", True


def _first_text(*candidates: object) -> str | None:
"""Return the first candidate that is a non-blank string."""
for candidate in candidates:
if isinstance(candidate, str) and candidate.strip():
return candidate
return None


__all__ = ["NoteDetails", "Reporter", "load_note", "truncate"]
32 changes: 32 additions & 0 deletions tests/unit/io_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,35 @@ def _boom(_path: Path) -> None:

with pytest.raises(RuntimeError, match="unexpected parser failure"):
load_note(Path("note.md"), max_chars=100)


class TestLoadNoteTitle:
def test_title_comes_from_frontmatter(self, tmp_path: Path) -> None:
note = tmp_path / "my-note.md"
note.write_text("---\ntitle: Real Title\n---\n\nBody\n", encoding="utf-8")

assert load_note(note, max_chars=1000).title == "Real Title"

def test_name_is_used_when_title_is_absent(self, tmp_path: Path) -> None:
note = tmp_path / "my-note.md"
note.write_text("---\nname: From Name\n---\n\nBody\n", encoding="utf-8")

assert load_note(note, max_chars=1000).title == "From Name"

def test_filename_is_used_when_frontmatter_has_no_title(self, tmp_path: Path) -> None:
note = tmp_path / "my_great-note.md"
note.write_text("Body\n", encoding="utf-8")

assert load_note(note, max_chars=1000).title == "My Great Note"

def test_non_string_title_falls_back_to_the_filename(self, tmp_path: Path) -> None:
note = tmp_path / "my-note.md"
note.write_text("---\ntitle: 2026\n---\n\nBody\n", encoding="utf-8")

assert load_note(note, max_chars=1000).title == "My Note"

def test_blank_title_falls_back_to_the_filename(self, tmp_path: Path) -> None:
note = tmp_path / "my-note.md"
note.write_text("---\ntitle: ' '\n---\n\nBody\n", encoding="utf-8")

assert load_note(note, max_chars=1000).title == "My Note"
6 changes: 3 additions & 3 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.