mirror of
https://github.com/basicmachines-co/basic-memory
synced 2026-06-21 13:47:35 +00:00
fix(core): require line-anchored frontmatter fences in file_utils
has_frontmatter(), parse_frontmatter(), and remove_frontmatter() detected
frontmatter by substring/split (`content.startswith("---")` plus
`content.split("---", 2)`) rather than by line-anchored fences. A single-line
string that merely starts with `---` — e.g. `---\nstatus: active\n---\nBody`
where `\n` are literal backslash-n characters, a common CLI/agent input shape —
was misread as frontmatter: yaml parsed `\nstatus` as a key that got merged into
the note's YAML on disk, and the body was silently transformed.
Introduce a shared `_split_frontmatter()` helper that anchors both fences to
their own lines (`^---[ \t]*$`), tolerating leading blank lines so dedented
heredoc-style content still parses. All three public helpers now delegate to it,
preserving existing behavior for valid frontmatter (BOM stripping, empty -> {},
non-dict -> ParseError, and the existing ParseError messages callers assert on).
Closes #972
Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Drew Cain <groksrc@gmail.com>
This commit is contained in:
@@ -302,26 +302,76 @@ async def format_file(
|
||||
return None
|
||||
|
||||
|
||||
# A frontmatter fence is a line containing exactly `---`, optionally followed by
|
||||
# trailing horizontal whitespace. Anchoring to a full line (rather than a bare
|
||||
# substring/`startswith`) prevents single-line content like
|
||||
# `---\nstatus: active\n---\nBody` — where `\n` is a literal backslash-n, not a
|
||||
# newline — from being misread as frontmatter. See issue #972.
|
||||
_FENCE_RE = re.compile(r"^---[ \t]*$")
|
||||
|
||||
|
||||
def _split_frontmatter(content: str) -> Optional[tuple[str, str]]:
|
||||
"""Split content into (yaml_block, body) when it opens with a line-anchored fence.
|
||||
|
||||
The opening fence must be the very first line and the closing fence must be a
|
||||
later line, each matching exactly `---` (with optional trailing whitespace).
|
||||
|
||||
Returns:
|
||||
A `(yaml_block, body)` tuple when a complete fenced block is present, or
|
||||
``None`` when the content does not open with a frontmatter fence.
|
||||
|
||||
Raises:
|
||||
ParseError: If the content opens with a fence but has no closing fence.
|
||||
"""
|
||||
lines = content.splitlines(keepends=True)
|
||||
|
||||
# Skip leading blank lines: a document may begin with whitespace before the
|
||||
# opening fence (e.g. a heredoc/dedented string starting with a newline). This
|
||||
# does NOT relax line-anchoring — the opening fence must still be the first
|
||||
# non-blank line, all on its own, so a single-line `---\\nstatus...` (literal
|
||||
# backslash-n) is still rejected. See issue #972.
|
||||
start = 0
|
||||
while start < len(lines) and lines[start].strip() == "":
|
||||
start += 1
|
||||
|
||||
if start >= len(lines) or not _FENCE_RE.match(lines[start].rstrip("\r\n")):
|
||||
return None
|
||||
|
||||
# Find the closing fence on its own line somewhere after the opening fence.
|
||||
for index in range(start + 1, len(lines)):
|
||||
if _FENCE_RE.match(lines[index].rstrip("\r\n")):
|
||||
yaml_block = "".join(lines[start + 1 : index])
|
||||
body = "".join(lines[index + 1 :])
|
||||
return yaml_block, body
|
||||
|
||||
raise ParseError("Invalid frontmatter format")
|
||||
|
||||
|
||||
def has_frontmatter(content: str) -> bool:
|
||||
"""
|
||||
Check if content contains valid YAML frontmatter.
|
||||
|
||||
Frontmatter requires `---` fences on their own lines; an inline `---` (such as
|
||||
a single-line string that merely starts with the characters `---`) is not
|
||||
frontmatter.
|
||||
|
||||
Args:
|
||||
content: Content to check
|
||||
|
||||
Returns:
|
||||
True if content has valid frontmatter markers (---), False otherwise
|
||||
True if content has line-anchored frontmatter fences, False otherwise
|
||||
"""
|
||||
if not content:
|
||||
return False
|
||||
|
||||
# Strip BOM before checking for frontmatter markers
|
||||
content = strip_bom(content).strip()
|
||||
if not content.startswith("---"):
|
||||
content = strip_bom(content)
|
||||
try:
|
||||
return _split_frontmatter(content) is not None
|
||||
except ParseError:
|
||||
# An opening fence with no closing fence is not usable frontmatter.
|
||||
return False
|
||||
|
||||
return "---" in content[3:]
|
||||
|
||||
|
||||
def parse_frontmatter(content: str) -> Dict[str, Any]:
|
||||
"""
|
||||
@@ -339,17 +389,14 @@ def parse_frontmatter(content: str) -> Dict[str, Any]:
|
||||
try:
|
||||
# Strip BOM before parsing frontmatter
|
||||
content = strip_bom(content)
|
||||
if not content.strip().startswith("---"):
|
||||
split = _split_frontmatter(content)
|
||||
if split is None:
|
||||
raise ParseError("Content has no frontmatter")
|
||||
|
||||
# Split on first two occurrences of ---
|
||||
parts = content.split("---", 2)
|
||||
if len(parts) < 3:
|
||||
raise ParseError("Invalid frontmatter format")
|
||||
yaml_block, _ = split
|
||||
|
||||
# Parse YAML
|
||||
try:
|
||||
frontmatter = yaml.safe_load(parts[1])
|
||||
frontmatter = yaml.safe_load(yaml_block)
|
||||
# Handle empty frontmatter (None from yaml.safe_load)
|
||||
if frontmatter is None:
|
||||
return {}
|
||||
@@ -381,18 +428,17 @@ def remove_frontmatter(content: str) -> str:
|
||||
ParseError: If content starts with frontmatter marker but is malformed
|
||||
"""
|
||||
# Strip BOM before processing
|
||||
content = strip_bom(content).strip()
|
||||
content = strip_bom(content)
|
||||
|
||||
# Return as-is if no frontmatter marker
|
||||
if not content.startswith("---"):
|
||||
return content
|
||||
split = _split_frontmatter(content)
|
||||
# Trigger: content does not open with a line-anchored fence
|
||||
# Why: inline `---` is ordinary content, not frontmatter (issue #972)
|
||||
# Outcome: return the content untouched (stripped to preserve prior behavior)
|
||||
if split is None:
|
||||
return content.strip()
|
||||
|
||||
# Split on first two occurrences of ---
|
||||
parts = content.split("---", 2)
|
||||
if len(parts) < 3:
|
||||
raise ParseError("Invalid frontmatter format")
|
||||
|
||||
return parts[2].strip()
|
||||
_, body = split
|
||||
return body.strip()
|
||||
|
||||
|
||||
def dump_frontmatter(post: frontmatter.Post) -> str:
|
||||
|
||||
Reference in New Issue
Block a user