Skip to content

Commit 641baa2

Browse files
github-actions[bot]Copilotnojafdsyme
authored
[Repo Assist] fix: Markdown.ToMd preserves indented code blocks as fenced code blocks (#1134)
* fix: Markdown.ToMd preserves indented code blocks as fenced code blocks Indented code blocks (parsed with fence = None) were serialised by Markdown.ToMd as bare text without any code block markers. When the output was re-parsed, the content was interpreted as a Paragraph, not a CodeBlock — breaking the round-trip. Fix: use triple-backtick fences for code blocks that originally had no fence, ensuring the serialised form is always a valid fenced code block. Fenced code blocks round-trip correctly and render identically to indented code blocks in all Markdown renderers. Added a round-trip test to cover the case. 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: Florian Verdonck <florian.verdonck@outlook.com> Co-authored-by: Don Syme <dsyme@users.noreply.github.com>
1 parent d98e7e6 commit 641baa2

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

RELEASE_NOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
* Add missing `[<Test>]` attribute on `Can include-output-and-it` test so it is executed by the test runner.
3232
* Add regression test confirming that types whose name matches their enclosing namespace are correctly included in generated API docs. [#944](https://github.com/fsprojects/FSharp.Formatting/issues/944)
3333
* Fix crash (`failwith "tbd - IndirectImage"`) when `Markdown.ToMd` is called on a document containing reference-style images with bracket syntax. The indirect image is now serialised as `![alt](url)` when the reference is resolved, or in bracket notation when it is not. [#1094](https://github.com/fsprojects/FSharp.Formatting/pull/1094)
34+
* Fix `Markdown.ToMd` serialising italic spans with asterisks incorrectly as bold spans. [#1102](https://github.com/fsprojects/FSharp.Formatting/pull/1102)
35+
* Fix `Markdown.ToMd` serialising ordered list items with incorrect numbering and formatting. [#1102](https://github.com/fsprojects/FSharp.Formatting/pull/1102)
36+
* Fix `Markdown.ToMd` not preserving indented code blocks: bare code output was re-parsed as a paragraph. Indented code blocks are now serialised as fenced code blocks, which round-trip correctly.
3437
* Fix `Markdown.ToMd` serialising `*emphasis*` (italic) spans as `**...**` (bold) instead of `*...*`. [#1102](https://github.com/fsprojects/FSharp.Formatting/pull/1102)
3538
* Fix `Markdown.ToMd` serialising ordered list items with 0-based numbering and no period (e.g. `0 first`) instead of 1-based with a period (e.g. `1. first`). [#1102](https://github.com/fsprojects/FSharp.Formatting/pull/1102)
3639
* Fix `HtmlElement` SVG serialisation: `LinearGradient` now renders as `<linearGradient>` and `RadialGradient` now renders as `<radialGradient>` — both previously emitted the invalid tag `<radient>`.

src/FSharp.Formatting.Markdown/MarkdownUtils.fs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,15 @@ module internal MarkdownUtils =
187187
yield String.replicate 3 (string c)
188188
yield ""
189189
| CodeBlock(code = code; fence = fence; language = language) ->
190-
match fence with
191-
| None -> ()
192-
| Some f -> yield f + language
190+
// Indented code blocks (fence = None) are serialised as fenced blocks so
191+
// that the round-trip is valid — raw indented code without a '> ' prefix
192+
// or 4-space indent would be parsed as a paragraph, not a code block.
193+
let f = defaultArg fence "```"
194+
yield f + language
193195

194196
yield code
195197

196-
match fence with
197-
| None -> ()
198-
| Some f -> yield f
198+
yield f
199199

200200
yield ""
201201
| ListBlock(Unordered, paragraphsl, _) ->

tests/FSharp.Markdown.Tests/Markdown.fs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,38 @@ let ``ToMd round-trip: indirect image with unresolved reference`` () =
14441444
result |> should contain "![alt text][unknown-ref]"
14451445

14461446
// --------------------------------------------------------------------------------------
1447+
// ToMd round-trip: indented code block (fence = None) — issue #fix-tomd-indented-codeblock
1448+
// --------------------------------------------------------------------------------------
1449+
1450+
[<Test>]
1451+
let ``ToMd round-trip: indented code block is preserved as a code block`` () =
1452+
// An indented code block (4-space indent) is serialised as a fenced block to
1453+
// guarantee the round-trip: outputting bare code without any fence would cause
1454+
// re-parsing to produce a paragraph instead of a code block.
1455+
let input = " let x = 1\n let y = 2"
1456+
let doc = Markdown.Parse(input)
1457+
// The parser should have produced a CodeBlock, not a Paragraph
1458+
let cbs =
1459+
doc.Paragraphs
1460+
|> List.choose (function
1461+
| CodeBlock _ as cb -> Some cb
1462+
| _ -> None)
1463+
1464+
cbs |> should haveLength 1
1465+
// ToMd should produce a fenced form so the round-trip is valid
1466+
let result = Markdown.ToMd(doc, newline = "\n")
1467+
result |> should contain "```"
1468+
result |> should contain "let x = 1"
1469+
result |> should contain "let y = 2"
1470+
// The serialised form re-parses to a CodeBlock, not a Paragraph
1471+
let doc2 = Markdown.Parse(result)
1472+
1473+
doc2.Paragraphs
1474+
|> List.choose (function
1475+
| CodeBlock _ as cb -> Some cb
1476+
| _ -> None)
1477+
|> should haveLength 1
1478+
14471479
// ToMd: HardLineBreak and HorizontalRule round-trip
14481480
// --------------------------------------------------------------------------------------
14491481

0 commit comments

Comments
 (0)