Render YAML front matter as a yaml code block#58
Merged
Conversation
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.
This was referenced May 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 comrakfront_matter_delimiteroption wasn't enabled and there was no match arm forNodeValue::FrontMatter.Changes:
options.extension.front_matter_delimiter = Some(\"---\")increate_options().emit_code_block()out of the existingNodeValue::CodeBlockarm so both fenced code blocks and front matter share the framed-and-highlighted rendering path.NodeValue::FrontMatterarm that strips the---delimiters (viastrip_front_matter_delimiters) and emits ayamlcode block via the shared helper. Lazy syntax highlighting picks up theyamllabel automatically.Detection contract
Front matter is detected positionally by comrak: the document must start with
---\non line 1, and a matching\n---\nmust 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 labeledyaml, body heading still renderstest_horizontal_rule_in_body_still_renders_as_thematic_break— a---mid-document is still a horizontal ruletest_dashes_after_blank_line_are_not_front_matter— a blank line before---disqualifies ittest_unclosed_front_matter_falls_back_to_normal_parsing— missing closing delimiter falls back to default parsingtest_strip_front_matter_delimiters_handles_unix_and_windows_endings— unit test for the delimiter strip helper across\nand\r\nNote 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 --checkcargo clippy -- -D warnings(pinned Rust 1.95.0)cargo test— 632 tests pass/tmp/test_frontmatter.md(jekyll-style file with title/author/date/tags)