ingestion: strip repeated front-matter blocks from notes

This commit is contained in:
Paperclip Builder
2026-10-26 22:33:57 +11:00
parent 4896e18a41
commit 720feddfcc

View File

@@ -692,14 +692,26 @@ def _make_document(**values) -> loader.Document:
def _strip_front_matter(text: str) -> str:
"""Drop a leading YAML block. The Note path parses it; files only need it gone."""
lines = text.splitlines()
if not lines or lines[0].strip() != "---":
return text
for index in range(1, len(lines)):
if lines[index].strip() in ("---", "..."):
return "\n".join(lines[index + 1 :]).strip()
return text
"""Drop leading YAML blocks. The Note path parses them; files only need them gone.
Some notes carry two blocks in a row (a stale export, or a block followed by
a horizontal rule that closes), so this repeats. It never strips a body that
does not have a closing delimiter.
"""
body = text
for _ in range(3):
lines = body.splitlines()
if not lines or lines[0].strip() != "---":
return body
closing = None
for index in range(1, len(lines)):
if lines[index].strip() in ("---", "..."):
closing = index
break
if closing is None:
return body
body = "\n".join(lines[closing + 1 :]).strip()
return body
def _split_note(text: str, validator) -> tuple:
@@ -724,7 +736,9 @@ def _split_note(text: str, validator) -> tuple:
fields = parsed
except validator.YamlError:
fields = {}
return fields, "\n".join(lines[end + 1 :]).strip()
# Strip any further leading block as well, so a duplicate front-matter block
# never reaches the chunker.
return fields, _strip_front_matter("\n".join(lines[end + 1 :]).strip())
def _source_row(name: str, enabled: bool, documents: list, extra: dict = None) -> dict: