Skip to content

Commit 0e46e50

Browse files
[Repo Assist] Fix crash in Markdown.ToMd when document contains indirect images (#1094)
* Fix crash in Markdown.ToMd when document contains indirect images Markdown.ToMd (formatAsMarkdown) called formatSpan in MarkdownUtils.fs which had a failwith "tbd - IndirectImage" for both arms of IndirectImage. This caused any call to Markdown.ToMd on a document containing Fix: implement the two cases by analogy with IndirectLink: (preserving the original indirect form) Add two regression tests in FSharp.Markdown.Tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * ci: trigger checks --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent d314437 commit 0e46e50

3 files changed

Lines changed: 29 additions & 3 deletions

File tree

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Fixed
66
* 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)
7+
* Fix crash (`failwith "tbd - IndirectImage"`) when `Markdown.ToMd` is called on a document containing reference-style images (`![alt][ref]`). The indirect image is now serialised as `![alt](url)` when the reference is resolved, or `![alt][ref]` when it is not.
78

89
## 22.0.0-alpha.2 - 2026-03-13
910

src/FSharp.Formatting.Markdown/MarkdownUtils.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ module internal MarkdownUtils =
110110
| DirectLink(body, link, _, _)
111111
| IndirectLink(body, link, _, _) -> "[" + formatSpans ctx body + "](" + link + ")"
112112

113-
| IndirectImage(_body, _, LookupKey ctx.Links (_link, _), _)
114-
| IndirectImage(_body, _link, _, _) -> failwith "tbd - IndirectImage"
115-
| DirectImage(_body, _link, _, _) -> sprintf "![%s](%s)" _body _link
113+
| IndirectImage(body, _, LookupKey ctx.Links (link, _), _) -> sprintf "![%s](%s)" body link
114+
| IndirectImage(body, _, key, _) -> sprintf "![%s][%s]" body key
115+
| DirectImage(body, link, _, _) -> sprintf "![%s](%s)" body link
116116
| Strong(body, _) -> "**" + formatSpans ctx body + "**"
117117
| InlineCode(body, _) -> "`" + body + "`"
118118
| Emphasis(body, _) -> "**" + formatSpans ctx body + "**"

tests/FSharp.Markdown.Tests/Markdown.fs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,3 +1201,28 @@ let ``Paragraph between sublists should not be absorbed into first sublist item
12011201
paragraphPos |> should be (greaterThan firstSublistEnd)
12021202
// Second sublist starts after paragraph.
12031203
secondSublistStart |> should be (greaterThan paragraphPos)
1204+
1205+
// --------------------------------------------------------------------------------------
1206+
// ToMd round-trip: indirect images (issue - failwith "tbd - IndirectImage")
1207+
// --------------------------------------------------------------------------------------
1208+
1209+
[<Test>]
1210+
let ``ToMd round-trip: indirect image with resolved reference`` () =
1211+
// Markdown with a reference-style image and a reference definition
1212+
let input = "![alt text][img-id]\n\n[img-id]: http://example.com/image.png \"Title\""
1213+
let doc = Markdown.Parse(input)
1214+
// Should NOT throw (was failing with failwith "tbd - IndirectImage")
1215+
let result = Markdown.ToMd(doc)
1216+
// When key is resolved, should render as a direct image
1217+
result |> should contain "![alt text]("
1218+
result |> should contain "http://example.com/image.png"
1219+
1220+
[<Test>]
1221+
let ``ToMd round-trip: indirect image with unresolved reference`` () =
1222+
// Parse just the image token without a reference definition
1223+
let input = "![alt text][unknown-ref]"
1224+
let doc = Markdown.Parse(input)
1225+
// Should NOT throw (was failing with failwith "tbd - IndirectImage")
1226+
let result = Markdown.ToMd(doc)
1227+
// When key is not resolved, should preserve the indirect form
1228+
result |> should contain "![alt text][unknown-ref]"

0 commit comments

Comments
 (0)