diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 772e4712..288ca475 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -9,6 +9,7 @@ * Bump `System.Memory` transitive-dependency pin from 4.5.5 to 4.6.3.0 ### Fixed +* Fix `Markdown.ToMd` silently dropping YAML frontmatter when serialising a parsed `MarkdownDocument` back to Markdown text. Frontmatter is now preserved with its `---` delimiters. * Fix `Markdown.ToMd` converting tight lists (no blank lines between items) into loose lists by emitting a blank line after every item. Tight lists now round-trip correctly without inter-item blank lines. * Fix `Markdown.ToMd` serialising `HardLineBreak` as a bare newline instead of two trailing spaces + newline. The correct CommonMark representation `" \n"` is now emitted, so hard line breaks survive a round-trip through `ToMd`. * Fix `Markdown.ToMd` serialising `HorizontalRule` as 23 hyphens regardless of the character used in the source. It now emits exactly three characters matching the parsed character (`---`, `***`, or `___`), giving faithful round-trips. diff --git a/src/FSharp.Formatting.Markdown/MarkdownUtils.fs b/src/FSharp.Formatting.Markdown/MarkdownUtils.fs index 20a1479b..6ea8f9ad 100644 --- a/src/FSharp.Formatting.Markdown/MarkdownUtils.fs +++ b/src/FSharp.Formatting.Markdown/MarkdownUtils.fs @@ -298,7 +298,14 @@ module internal MarkdownUtils = | InlineHtmlBlock(code, _, _) -> let lines = code.Replace("\r\n", "\n").Split('\n') |> Array.toList yield! lines - | YamlFrontmatter _ -> () + | YamlFrontmatter(lines, _) -> + yield "---" + + for line in lines do + yield line + + yield "---" + yield "" | Span(body = body) -> yield formatSpans ctx body | QuotedBlock(paragraphs = paragraphs) -> for paragraph in paragraphs do diff --git a/tests/FSharp.Markdown.Tests/Markdown.fs b/tests/FSharp.Markdown.Tests/Markdown.fs index e5cf2b90..6cb6bc1e 100644 --- a/tests/FSharp.Markdown.Tests/Markdown.fs +++ b/tests/FSharp.Markdown.Tests/Markdown.fs @@ -1632,6 +1632,22 @@ let ``ToMd preserves a link with a title`` () = result |> should contain "[FSharp](" result |> should contain "https://fsharp.org" +[] +let ``ToMd preserves YAML frontmatter`` () = + let input = "---\ntitle: My Page\ndate: 2024-01-01\n---\n\nHello world.\n" + let result = input |> toMd + result |> should contain "---" + result |> should contain "title: My Page" + result |> should contain "date: 2024-01-01" + result |> should contain "Hello world." + +[] +let ``ToMd preserves empty YAML frontmatter`` () = + let input = "---\n---\n\nHello.\n" + let result = input |> toMd + result |> should contain "---" + result |> should contain "Hello." + [] let ``ToMd serialises EmbedParagraphs by delegating to Render()`` () = // EmbedParagraphs was previously falling through to the catch-all '| _' branch,