Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased]

### Fixed
* Fix `Markdown.ToMd` emitting invalid `[text]([key])` for unresolved reference-style links. Unresolved `IndirectLink` spans now serialise as `[text][key]` (preserving the reference notation) instead of incorrectly inserting the key string with its brackets as a URL.
* Fix `Markdown.ToMd` dropping link titles in `DirectLink` and `DirectImage` spans. Links with a title attribute (e.g. `[text](url "title")`) now round-trip correctly; without this fix the title was silently discarded on serialisation.
* Fix `Markdown.ToMd` serialising inline code spans that contain backtick characters. Previously, `InlineCode` was always wrapped in single backticks, producing syntactically incorrect Markdown when the code body contained backticks. Now the serialiser selects the shortest backtick fence that does not collide with the body content (e.g. a double-backtick fence for bodies containing single backticks, triple for double, etc.), matching the CommonMark spec.

Expand Down
1 change: 1 addition & 0 deletions src/FSharp.Formatting.Markdown/MarkdownUtils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ module internal MarkdownUtils =
"[" + formatSpans ctx body + "](" + link + t + ")"

| IndirectLink(body, _, LookupKey ctx.Links (link, _), _)
| DirectLink(body, link, _, _) -> "[" + formatSpans ctx body + "](" + link + ")"
| IndirectLink(body, link, _, _) -> "[" + formatSpans ctx body + "](" + link + ")"

| IndirectImage(body, _, LookupKey ctx.Links (link, _), _) -> sprintf "![%s](%s)" body link
Expand Down
24 changes: 24 additions & 0 deletions tests/FSharp.Markdown.Tests/Markdown.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1417,3 +1417,27 @@ let ``ToMd round-trip: indirect image with unresolved reference`` () =
let result = Markdown.ToMd(doc)
// When key is not resolved, should preserve the indirect form
result |> should contain "![alt text][unknown-ref]"

// --------------------------------------------------------------------------------------
// ToMd round-trip: unresolved indirect links
// --------------------------------------------------------------------------------------

[<Test>]
let ``ToMd preserves unresolved indirect link in reference notation`` () =
// Indirect link with NO matching reference definition in the document.
// Before fix: emitted [link text]([unknown-key]) β€” invalid Markdown.
// After fix: emits [link text][unknown-key] β€” valid reference link.
let input = "[link text][unknown-key]"
let doc = Markdown.Parse(input)
let result = Markdown.ToMd(doc)
// Must use reference notation, not inline notation with [key] as the URL
result |> should contain "[link text][unknown-key]"
result |> should not' (contain "[link text]([")

[<Test>]
let ``ToMd resolves indirect link when reference is present`` () =
// Indirect link whose reference definition IS present β€” key resolves to a URL.
let input = "[FSharp][fs-link]\n\n[fs-link]: https://fsharp.org"
let result = toMd input
result |> should contain "[FSharp]("
result |> should contain "https://fsharp.org"
Loading