Skip to content

Warn on YAML comment loss in frontmatter write commands#52

Open
DandyLyons wants to merge 1 commit into
mainfrom
warn-yaml-comment-loss
Open

Warn on YAML comment loss in frontmatter write commands#52
DandyLyons wants to merge 1 commit into
mainfrom
warn-yaml-comment-loss

Conversation

@DandyLyons

Copy link
Copy Markdown
Owner

Summary

  • YAML comments (# ...) in frontmatter are silently destroyed by any write operation — this is a fundamental limitation of Yams/libYAML, which discards comment tokens before they reach the AST
  • Adds a naive pre-parse comment detector using swift-parsing that catches the most common case (standalone comment lines) before the raw YAML string is handed to Yams
  • All 9 frontmatter write commands now emit a warning: to stderr when comments are detected, so users know data loss is about to occur
  • Documents the limitation in docs/architecture.md

Changes

Layer What changed
FrontMatterParser Added static func containsYAMLComments(_:) — scans raw YAML line-by-line using Prefix(while:) from swift-parsing
MarkdownDocument Added public let containsYAMLComments: Bool, set at init(content:) time
9 write CLI commands Emit warning: <path>: frontmatter contains YAML comments which will be lost to stderr before mutating
docs/architecture.md New "Known Limitations" section documenting root cause, detection scope, and possible future fix
Tests 16 new tests in YAMLCommentDetectionTests.swift

Scope of detection

The check is intentionally naive: it detects standalone comment lines (where the first non-whitespace character is #). It does not detect inline comments (key: value # comment). This avoids false positives on YAML string values that legitimately contain # (URLs, hex color codes, hashtags).

Test plan

  • swift test --filter YAMLComment — all 16 tests pass
  • swift test — full suite passes
  • Manual: run md-utils fm set on a file with # comment in frontmatter and confirm warning appears on stderr

🤖 Generated with Claude Code

YAML comments in frontmatter are silently discarded whenever any write
operation is performed. This is a fundamental limitation of Yams/libYAML:
comment tokens are dropped before they reach the parsed AST and cannot
be recovered.

- Add `FrontMatterParser.containsYAMLComments(_:)` — a naive check using
  swift-parsing's `Prefix(while:)` that detects standalone comment lines
  (first non-whitespace character is `#`) in the raw YAML string before
  Yams parses it
- Add `MarkdownDocument.containsYAMLComments: Bool` property, set at
  `init(content:)` time; always `false` for programmatic init
- Emit `warning: <path>: frontmatter contains YAML comments which will
  be lost` to stderr in all 9 frontmatter write commands (set, remove,
  rename, replace, sort-keys, touch, array append/prepend/remove)
- Document the limitation in docs/architecture.md under Known Limitations
- Add 16 tests covering detection true/false cases and the document property

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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