Skip to content

Handle empty frontmatter in local helpers#59

Open
fa1k3 wants to merge 2 commits into
cyanheads:mainfrom
fa1k3:fix/empty-frontmatter-splice
Open

Handle empty frontmatter in local helpers#59
fa1k3 wants to merge 2 commits into
cyanheads:mainfrom
fa1k3:fix/empty-frontmatter-splice

Conversation

@fa1k3
Copy link
Copy Markdown

@fa1k3 fa1k3 commented May 20, 2026

Summary

  • recognize empty YAML frontmatter blocks (---\n---\n) in the local frontmatter helpers
  • anchor the matcher to the start of the document (no /m flag) so --- lines in the body (thematic breaks) are never mistaken for a frontmatter block
  • avoid duplicating ---\n--- when adding frontmatter tags to a note with an empty block
  • LF/CRLF empty-block coverage plus a regression test for body-only --- lines

Why

The composed tag/frontmatter helpers read and rewrite raw note content for operations that have no single Local REST API equivalent. The matcher missed valid empty frontmatter blocks.

A multiline-anchored fix would over-match: with /m, the opening ^--- can match a --- line mid-body, and because splice() slices via m[0].length (assuming the match is at index 0), a note with no frontmatter but --- lines in the body would be silently corrupted. The matcher is therefore anchored to string start with an optional content group — this handles the empty block without the false positive.

Tests

  • bun test tests/services/frontmatter-ops.test.ts
  • bun run test (full suite green)
  • bun run test:types

fa1k3 and others added 2 commits May 20, 2026 11:22
The empty-frontmatter regex used the /m flag, so the opening `^---` could
match a `---` line mid-body. splice() slices via m[0].length assuming the
match starts at index 0, so a note with no frontmatter but `---` lines in
the body (thematic breaks) was mis-parsed and its content silently corrupted.

Anchor the opening `---` to string start (drop /m) and make the content
group optional so an empty block (`---\n---\n`) still matches. Add a
regression test for the no-frontmatter / body-`---` case.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@fa1k3
Copy link
Copy Markdown
Author

fa1k3 commented Jun 2, 2026

Updated: rebased onto main. The matcher is now anchored to the start of the document rather than using /m — a multiline anchor lets the opening ^--- match a --- line mid-body, and since splice() slices via m[0].length (assuming a match at index 0), that silently corrupts notes that have no frontmatter but contain --- lines. Added a regression test for that case alongside the empty-block coverage. Ready for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant