"""Frontmatter parser for checklist files.
Parses YAML frontmatter from markdown files to extract metadata.
"""
from __future__ import annotations
from typing import TYPE_CHECKING, Any
import frontmatter
from sso_mcp_server import get_logger
if TYPE_CHECKING:
from pathlib import Path
_logger = get_logger("frontmatter_parser")
def parse_checklist_file(file_path: Path) -> dict[str, Any] | None:
"""Parse a checklist markdown file with YAML frontmatter.
Extracts name, description, and content from the file.
Args:
file_path: Path to the markdown file.
Returns:
Dictionary with name, description, content, and path.
Returns None if file doesn't exist or can't be parsed.
"""
if not file_path.exists():
_logger.debug("file_not_found", path=str(file_path))
return None
try:
post = frontmatter.load(file_path)
# Extract metadata from frontmatter
name = post.metadata.get("name", file_path.stem)
description = post.metadata.get("description", "")
# Content is the body without frontmatter
content = post.content.strip()
result = {
"name": name,
"description": description,
"content": content,
"path": file_path,
}
_logger.debug(
"file_parsed",
path=str(file_path),
name=name,
has_description=bool(description),
)
return result
except Exception as e:
_logger.warning("parse_error", path=str(file_path), error=str(e))
# Return basic info using filename
return {
"name": file_path.stem,
"description": "",
"content": "",
"path": file_path,
}