Skip to content

fix: crlf frontmatter detection - #372

Open
kumaa-n wants to merge 1 commit into
RivoLink:mainfrom
kumaa-n:fix/crlf-frontmatter-detection
Open

kumaa-n wants to merge 1 commit into
RivoLink:mainfrom
kumaa-n:fix/crlf-frontmatter-detection

Conversation

@kumaa-n

@kumaa-n kumaa-n commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

extract_frontmatter only matched fence lines ending in a bare \n (or at the end of the file), so frontmatter in a file with CRLF line endings was never detected. The opening --- rendered as a horizontal rule, and the key/value lines and the closing --- became a setext H2 heading. The body was fine; only the fence check missed CRLF.

This accepts ---\r\n as the opening fence and trims trailing \r/\n before comparing the closing fence. A lone CR is also a line ending in CommonMark/YAML, so "---\r\r\n" still closes the block, and a short comment notes this. LF files behave exactly as before.

Reproduce

printf -- '---\r\ntitle: Demo\r\nowner: me\r\n---\r\n# Visible\r\nBody\r\n' > repro.md
leaf --inline=plain --width 40 repro.md

Before:

────────────────────────────────────────

title: Demo
owner: me
─────────
Visible
═══════
Body

After:

┌───────────────────┬──────────────────┐
│ title             │ owner            │
╞═══════════════════╪══════════════════╡
│ Demo              │ me               │
└───────────────────┴──────────────────┘

Visible
═══════
Body

Testing

  • Added frontmatter_with_crlf_line_endings_is_ignored_in_preview, source_line_map_crlf_frontmatter_lines_point_to_line_one, frontmatter_closing_fence_accepts_lone_cr and frontmatter_closing_fence_variants_are_detected. All four fail on main and pass with this change.
  • frontmatter_closing_fence_variants_are_detected also covers the LF fences that had no test before (..., and a closing fence at the end of the file), since this change rewrites that check.
  • cargo test (1017 passed), cargo clippy --all-targets --all-features -- -D warnings, cargo fmt --all -- --check.
  • Rendered every *.md in this repo with the old and new code: output and source line map are identical, so normal documents are unaffected.

One visible change for CRLF files: a file whose first line is --- used as a horizontal rule, with another --- line later, is now treated as frontmatter, the same as an LF file. Lines in between that are not key: value pairs are no longer rendered, and lines that look like one show up in the frontmatter table. This is the existing LF behavior, not something new in this change.

A file using bare CR line endings (classic Mac OS) is still not supported. Leaf counts lines by \n throughout, so handling that would mean normalizing line endings on load, which felt out of scope here.

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