Skip to content

Commit d98e7e6

Browse files
github-actions[bot]Copilotdsyme
authored
[Repo Assist] fix: Markdown.ToMd preserves YAML frontmatter (#1165)
* 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> * ci: trigger checks --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Don Syme <dsyme@users.noreply.github.com>
1 parent b013f37 commit d98e7e6

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Bump `System.Memory` transitive-dependency pin from 4.5.5 to 4.6.3.0
1010

1111
### Fixed
12+
* Fix `Markdown.ToMd` silently dropping YAML frontmatter when serialising a parsed `MarkdownDocument` back to Markdown text. Frontmatter is now preserved with its `---` delimiters.
1213
* 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.
1314
* 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`.
1415
* 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.

src/FSharp.Formatting.Markdown/MarkdownUtils.fs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,14 @@ module internal MarkdownUtils =
298298
| InlineHtmlBlock(code, _, _) ->
299299
let lines = code.Replace("\r\n", "\n").Split('\n') |> Array.toList
300300
yield! lines
301-
| YamlFrontmatter _ -> ()
301+
| YamlFrontmatter(lines, _) ->
302+
yield "---"
303+
304+
for line in lines do
305+
yield line
306+
307+
yield "---"
308+
yield ""
302309
| Span(body = body) -> yield formatSpans ctx body
303310
| QuotedBlock(paragraphs = paragraphs) ->
304311
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
@@ -1632,6 +1632,22 @@ let ``ToMd preserves a link with a title`` () =
16321632
result |> should contain "[FSharp]("
16331633
result |> should contain "https://fsharp.org"
16341634

1635+
[<Test>]
1636+
let ``ToMd preserves YAML frontmatter`` () =
1637+
let input = "---\ntitle: My Page\ndate: 2024-01-01\n---\n\nHello world.\n"
1638+
let result = input |> toMd
1639+
result |> should contain "---"
1640+
result |> should contain "title: My Page"
1641+
result |> should contain "date: 2024-01-01"
1642+
result |> should contain "Hello world."
1643+
1644+
[<Test>]
1645+
let ``ToMd preserves empty YAML frontmatter`` () =
1646+
let input = "---\n---\n\nHello.\n"
1647+
let result = input |> toMd
1648+
result |> should contain "---"
1649+
result |> should contain "Hello."
1650+
16351651
[<Test>]
16361652
let ``ToMd serialises EmbedParagraphs by delegating to Render()`` () =
16371653
// EmbedParagraphs was previously falling through to the catch-all '| _' branch,

0 commit comments

Comments
 (0)