Skip to content

fix(frontmatter): recognise empty frontmatter blocks#592

Open
MFA-G wants to merge 1 commit into
Nano-Collective:mainfrom
MFA-G:fix/empty-frontmatter-block
Open

fix(frontmatter): recognise empty frontmatter blocks#592
MFA-G wants to merge 1 commit into
Nano-Collective:mainfrom
MFA-G:fix/empty-frontmatter-block

Conversation

@MFA-G

@MFA-G MFA-G commented Jun 21, 2026

Copy link
Copy Markdown

Summary

splitFrontmatter failed to recognise an empty frontmatter block — a file that opens with --- and closes with --- on the next line, with no lines in between:

---
---
Body content

The existing regex required at least one captured line between the delimiters (([\s\S]*?)\r?\n---), so an empty block fell through to the no-frontmatter branch. Two consequences:

  • hasFrontmatter was reported as false and the literal ---\n--- markers leaked into body.
  • For subagent markdown files, extractRawFrontmatter then threw No YAML frontmatter found in file (see source/subagents/markdown-parser.ts), even though the file does have a (valid, empty) frontmatter block.

Fix

Make the inner frontmatter capture group optional so an empty block is matched, returning an empty frontmatter string and a clean body:

-const frontmatterRegex = /^---\s*\r?\n([\s\S]*?)\r?\n---\s*\r?\n?([\s\S]*)$/;
+const frontmatterRegex = /^---\s*\r?\n(?:([\s\S]*?)\r?\n)?---\s*\r?\n?([\s\S]*)$/;

I verified against a matrix of inputs that the only behavioural change is the previously-unhandled empty-block case — every other input (standard, multi-line, CRLF, no trailing newline, embedded --- in the body, lone delimiter, no frontmatter) splits identically to before.

Tests

Added source/utils/frontmatter.spec.ts (the module had no spec). It covers splitFrontmatter across all the cases above — including a regression test for the empty block — plus parseYamlObject (valid object, nested/array values, blank input, scalar, top-level array, invalid YAML).

Validation

  • pnpm exec ava source/utils/frontmatter.spec.ts — 15 passed
  • pnpm run test:ava — full suite, 5775 passed
  • pnpm run test:types — clean
  • pnpm exec biome lint/check (changed files) — clean
  • pnpm run test:knip — clean

An empty frontmatter block (`---\n---\n` with no content between the
delimiters) was not matched by the splitFrontmatter regex. This caused
the literal `---` markers to leak into the body and hasFrontmatter to
be reported as false. For subagent markdown files this even raised
"No YAML frontmatter found in file".

Make the inner frontmatter capture group optional so an empty block is
recognised, returning an empty frontmatter string and a clean body. The
only behavioural change is for the previously-unhandled empty-block case;
all other inputs split identically.

Add frontmatter.spec.ts covering splitFrontmatter (standard, multi-line,
CRLF, no-trailing-newline, empty block, blank-line block, embedded ---,
no-frontmatter, lone-delimiter) and parseYamlObject.
Copilot AI review requested due to automatic review settings June 21, 2026 13:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes splitFrontmatter so it correctly recognizes empty YAML frontmatter blocks (---\n---\n...) at the start of markdown files, preventing delimiter leakage into the body and ensuring hasFrontmatter is reported correctly.

Changes:

  • Updated the frontmatter regex to allow an empty frontmatter section between delimiters.
  • Adjusted splitFrontmatter extraction to return frontmatter: '' when the optional capture group is absent.
  • Added a new AVA spec file covering splitFrontmatter and parseYamlObject across a range of inputs, including the empty-frontmatter regression.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
source/utils/frontmatter.ts Makes the frontmatter capture optional so ---\n---\n is treated as valid frontmatter with an empty frontmatter string.
source/utils/frontmatter.spec.ts Adds coverage for empty/blank-line frontmatter handling and parseYamlObject behaviors (valid mapping, blank, scalar/array, invalid YAML).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

2 participants