Skip to content

Count only a closed block as frontmatter, so AddFrontmatter writes a header above an opening rule [patch] - #186

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/157-unclosed-opening-rule
Oct 6, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/157-unclosed-opening-rule

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #157

Problem

HasFrontmatter returned true whenever the first line was a delimiter, but TrySplitFrontmatterBlocks also needs a closing delimiter. When a document opens with a thematic break that never closes ("---\n\n# Heading\n\nText\n"), the two disagree. AddFrontmatter then took the "present but unreadable, leave it alone" branch and returned the input unchanged, so the new properties were silently lost.

Change

  • HasFrontmatter now returns TrySplitFrontmatterBlocks(...), so only a closed block counts.
  • The first-line check (BOM skip, trailing whitespace allowed) moves to a private OpensWithDelimiter helper. The splitter uses that helper as its precondition.
  • ExtractFrontmatterObjects no longer reports a never-closed opening rule as an unreadable block. A document like that has no frontmatter, so AddFrontmatter prepends a header to it the same way it does for any other document without one.
  • RemoveFrontmatter now returns such a document unchanged, like any document without frontmatter. Before, it was trimmed and given a trailing newline.

Tests

New UnclosedOpeningRuleTests, covering HasFrontmatter, ExtractFrontmatter, AddFrontmatter (header goes in front, the original --- rule stays in the body, and the result round-trips) and RemoveFrontmatter.

Checked locally:

  • The HasFrontmatter and AddFrontmatter tests fail with the Frontmatter.cs change reverted.
  • The full suite passes with it (241/241), including the existing BOM and trailing-whitespace opening-delimiter tests.

🤖 Generated with Claude Code

https://claude.ai/code/session_01QiAEodt8KsqJyMYzdfoAfo


Generated by Claude Code

…header above an opening rule [patch]

HasFrontmatter returned true as soon as the first line was a delimiter, while
the block splitter needs a closing one. A document opening with a thematic
break that never closes made AddFrontmatter take the "present but unreadable"
branch and return the input unchanged, silently dropping the new properties.
HasFrontmatter now asks the splitter, and the first-line check moves to a
private OpensWithDelimiter helper that keeps the BOM and trailing-whitespace
rules.

Fixes #157

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QiAEodt8KsqJyMYzdfoAfo
@sonarqubecloud

sonarqubecloud Bot commented Oct 6, 2026

Copy link
Copy Markdown

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.

AddFrontmatter silently drops the new properties when a document opens with a --- rule that never closes

2 participants