Skip to content

Render YAML front matter as a yaml code block#58

Merged
jvanderberg merged 1 commit into
mainfrom
fix/yaml-front-matter
May 14, 2026
Merged

Render YAML front matter as a yaml code block#58
jvanderberg merged 1 commit into
mainfrom
fix/yaml-front-matter

Conversation

@jvanderberg
Copy link
Copy Markdown
Owner

Summary

Closes #51. Markdown files with --- delimited front matter were rendered as two horizontal rules with the keys/values bleeding through as paragraph text — the comrak front_matter_delimiter option wasn't enabled and there was no match arm for NodeValue::FrontMatter.

Changes:

  • Enable options.extension.front_matter_delimiter = Some(\"---\") in create_options().
  • Extract emit_code_block() out of the existing NodeValue::CodeBlock arm so both fenced code blocks and front matter share the framed-and-highlighted rendering path.
  • Add a NodeValue::FrontMatter arm that strips the --- delimiters (via strip_front_matter_delimiters) and emits a yaml code block via the shared helper. Lazy syntax highlighting picks up the yaml label automatically.

Detection contract

Front matter is detected positionally by comrak: the document must start with ---\n on line 1, and a matching \n---\n must appear later. Otherwise the --- falls back to a thematic break / horizontal rule. Locked in by these tests:

  • test_front_matter_renders_as_yaml_code_block — happy path: keys land in the code block, no horizontal rules, frame is labeled yaml, body heading still renders
  • test_horizontal_rule_in_body_still_renders_as_thematic_break — a --- mid-document is still a horizontal rule
  • test_dashes_after_blank_line_are_not_front_matter — a blank line before --- disqualifies it
  • test_unclosed_front_matter_falls_back_to_normal_parsing — missing closing delimiter falls back to default parsing
  • test_strip_front_matter_delimiters_handles_unix_and_windows_endings — unit test for the delimiter strip helper across \n and \r\n

Note on PR #52

The vivainio draft PR #52 proposed essentially the same approach a month ago. I wrote this independently after the user asked for a fix in #51; we can close #52 once this lands.

Test plan

  • cargo fmt --check
  • cargo clippy -- -D warnings (pinned Rust 1.95.0)
  • cargo test — 632 tests pass
  • Manual verification with /tmp/test_frontmatter.md (jekyll-style file with title/author/date/tags)

Markdown files with `---` delimited front matter were rendered as two
horizontal rules with the keys/values bleeding through as paragraph text,
because comrak's front_matter_delimiter option wasn't enabled and there
was no match arm for NodeValue::FrontMatter.

- Enable options.extension.front_matter_delimiter = Some("---")
- Extract emit_code_block() out of the CodeBlock arm so both fenced code
  blocks and front matter share the framed/highlighted rendering path
- Add a NodeValue::FrontMatter arm that strips the `---` delimiters and
  emits a yaml code block via the shared helper

Front matter is detected positionally (must start at line 1, must have a
matching `\n---\n` closing delimiter), so a `---` elsewhere in a document
still renders as a horizontal rule. Locked in by tests:

- front_matter_renders_as_yaml_code_block (happy path)
- horizontal_rule_in_body_still_renders_as_thematic_break
- dashes_after_blank_line_are_not_front_matter
- unclosed_front_matter_falls_back_to_normal_parsing
- strip_front_matter_delimiters_handles_unix_and_windows_endings

Closes #51.
@jvanderberg jvanderberg merged commit ec7272c into main May 14, 2026
3 checks passed
@jvanderberg jvanderberg deleted the fix/yaml-front-matter branch May 14, 2026 02:11
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.

YAML front matter rendered as horizontal rules + plain text

1 participant