Skip to content

Commit c4d6365

Browse files
fix: Markdown.ToMd preserves YAML frontmatter
When Markdown.ToMd serialises a parsed MarkdownDocument back to Markdown text, YamlFrontmatter paragraphs were silently dropped. Documents containing a YAML front-matter block (--- ... ---) would lose that block on round-trip. Fix: emit the --- delimiters and frontmatter lines in formatParagraph so round-trips are lossless. Add two new tests: one with populated frontmatter and one with an empty block. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 97b8697 commit c4d6365

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

RELEASE_NOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## [Unreleased]
44

5+
### Fixed
6+
* Fix `Markdown.ToMd` silently dropping YAML frontmatter when serialising a parsed `MarkdownDocument` back to Markdown text. Frontmatter is now preserved with its `---` delimiters.
7+
58
## [22.0.0] - 2026-04-03
69

710
### Fixed

src/FSharp.Formatting.Markdown/MarkdownUtils.fs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,14 @@ module internal MarkdownUtils =
235235
let lines = code.Replace("\r\n", "\n").Split('\n') |> Array.toList
236236
yield! lines
237237
//yield ""
238-
| YamlFrontmatter _ -> ()
238+
| YamlFrontmatter(lines, _) ->
239+
yield "---"
240+
241+
for line in lines do
242+
yield line
243+
244+
yield "---"
245+
yield ""
239246
| Span(body = body) -> yield formatSpans ctx body
240247
| QuotedBlock(paragraphs = paragraphs) ->
241248
for paragraph in paragraphs do

tests/FSharp.Markdown.Tests/Markdown.fs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,3 +1371,19 @@ let ``ToMd round-trip: indirect image with unresolved reference`` () =
13711371
let result = Markdown.ToMd(doc)
13721372
// When key is not resolved, should preserve the indirect form
13731373
result |> should contain "![alt text][unknown-ref]"
1374+
1375+
[<Test>]
1376+
let ``ToMd preserves YAML frontmatter`` () =
1377+
let input = "---\ntitle: My Page\ndate: 2024-01-01\n---\n\nHello world.\n"
1378+
let result = input |> toMd
1379+
result |> should contain "---"
1380+
result |> should contain "title: My Page"
1381+
result |> should contain "date: 2024-01-01"
1382+
result |> should contain "Hello world."
1383+
1384+
[<Test>]
1385+
let ``ToMd preserves empty YAML frontmatter`` () =
1386+
let input = "---\n---\n\nHello.\n"
1387+
let result = input |> toMd
1388+
result |> should contain "---"
1389+
result |> should contain "Hello."

0 commit comments

Comments
 (0)