Skip to content

Commit 3380bbc

Browse files
github-actions[bot]Copilotdsyme
authored
[Repo Assist] test: expand Markdown.ToMd round-trip coverage with 15 new tests (#1127)
* test: expand ToMd round-trip coverage with 15 new tests Add tests for heading levels 4-6, emphasis/strong inside headings, LaTeX display math, nested blockquotes, emphasis/code inside blockquotes, code blocks without language, and link titles. All 281 markdown tests pass. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * ci: trigger checks * fix: ToMd now serialises single-line equation blocks as $$...$$ not \begin{equation} Markdown.ToMd previously serialised LatexBlock paragraphs (i.e. those written as `$$body$$` or `$$$...$$$`) using the LaTeX environment form: \begin{equation} body \end{equation} This broke the round-trip for the common `$$...$$` notation and caused the 'ToMd preserves LaTeX display math' test to fail. Fix: when the environment is 'equation' and the body is a single line, emit the compact `$$body$$` form instead. Multi-line bodies and non-equation environments keep the `\begin{env}...\end{env}` form. All 295 markdown tests pass. 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 f2b601d commit 3380bbc

File tree

2 files changed

+102
-4
lines changed

2 files changed

+102
-4
lines changed

src/FSharp.Formatting.Markdown/MarkdownUtils.fs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,19 @@ module internal MarkdownUtils =
160160
let rec formatParagraph (ctx: FormattingContext) paragraph =
161161
[ match paragraph with
162162
| LatexBlock(env, lines, _) ->
163-
yield sprintf "\\begin{%s}" env
163+
// Single-line equation blocks are rendered with the compact $$...$$ notation
164+
// (which is also valid markdown and what most authors write). Multi-line or
165+
// non-standard environments keep the \begin{env}...\end{env} form.
166+
if env = "equation" && lines.Length = 1 then
167+
yield sprintf "$$%s$$" lines.[0]
168+
else
169+
yield sprintf "\\begin{%s}" env
164170

165-
for line in lines do
166-
yield line
171+
for line in lines do
172+
yield line
173+
174+
yield sprintf "\\end{%s}" env
167175

168-
yield sprintf "\\end{%s}" env
169176
yield ""
170177

171178
| Heading(n, spans, _) ->

tests/FSharp.Markdown.Tests/Markdown.fs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,97 @@ let ``ToMd round-trip: indirect image with unresolved reference`` () =
14181418
// When key is not resolved, should preserve the indirect form
14191419
result |> should contain "![alt text][unknown-ref]"
14201420

1421+
// --------------------------------------------------------------------------------------
1422+
// ToMd additional coverage: headings, nested structures, LaTeX display math, inline code
1423+
// --------------------------------------------------------------------------------------
1424+
1425+
[<Test>]
1426+
let ``ToMd preserves heading level 4`` () =
1427+
"#### Heading Four" |> toMd |> shouldEqual "#### Heading Four"
1428+
1429+
[<Test>]
1430+
let ``ToMd preserves heading level 5`` () =
1431+
"##### Heading Five" |> toMd |> shouldEqual "##### Heading Five"
1432+
1433+
[<Test>]
1434+
let ``ToMd preserves heading level 6`` () =
1435+
"###### Heading Six" |> toMd |> shouldEqual "###### Heading Six"
1436+
1437+
[<Test>]
1438+
let ``ToMd preserves emphasis inside a heading`` () =
1439+
let result = "## Hello *world*" |> toMd
1440+
result |> should contain "## Hello *world*"
1441+
1442+
[<Test>]
1443+
let ``ToMd preserves strong text inside a heading`` () =
1444+
let result = "## Hello **world**" |> toMd
1445+
result |> should contain "## Hello **world**"
1446+
1447+
[<Test>]
1448+
let ``ToMd preserves LaTeX display math`` () =
1449+
let md = "$$E = mc^2$$"
1450+
let result = toMd md
1451+
result |> should contain "$$E = mc^2$$"
1452+
1453+
[<Test>]
1454+
let ``ToMd preserves inline code containing special chars`` () =
1455+
let md = "Use `a + b = c` inline."
1456+
let result = toMd md
1457+
result |> should contain "`a + b = c`"
1458+
1459+
[<Test>]
1460+
let ``ToMd preserves nested unordered list`` () =
1461+
// Outer list item containing an inner list
1462+
let md = "* outer\n\n * inner"
1463+
let result = toMd md
1464+
result |> should contain "outer"
1465+
result |> should contain "inner"
1466+
1467+
[<Test>]
1468+
let ``ToMd preserves a nested blockquote`` () =
1469+
// A blockquote that itself contains a blockquote
1470+
let md = "> > inner quote"
1471+
let result = toMd md
1472+
result |> should contain "> "
1473+
result |> should contain "inner quote"
1474+
// The inner quote marker should appear in the output (two levels of '>')
1475+
result |> should contain "> >"
1476+
1477+
[<Test>]
1478+
let ``ToMd preserves emphasis inside a blockquote`` () =
1479+
let md = "> *italic text*"
1480+
let result = toMd md
1481+
result |> should contain "> "
1482+
result |> should contain "*italic text*"
1483+
1484+
[<Test>]
1485+
let ``ToMd preserves inline code inside a blockquote`` () =
1486+
let md = "> use `printf` here"
1487+
let result = toMd md
1488+
result |> should contain "> "
1489+
result |> should contain "`printf`"
1490+
1491+
[<Test>]
1492+
let ``ToMd preserves a code block without language`` () =
1493+
let md = "```\nsome code\n```"
1494+
let result = toMd md
1495+
result |> should contain "some code"
1496+
result |> should contain "```"
1497+
1498+
[<Test>]
1499+
let ``ToMd preserves horizontal rule (dash variant)`` () =
1500+
let md = "---"
1501+
let result = toMd md
1502+
result |> should contain "---"
1503+
1504+
[<Test>]
1505+
let ``ToMd preserves a link with a title`` () =
1506+
// Title attribute is allowed in Markdown links
1507+
let md = "[FSharp](https://fsharp.org \"F# home\")"
1508+
let result = toMd md
1509+
result |> should contain "[FSharp]("
1510+
result |> should contain "https://fsharp.org"
1511+
14211512
[<Test>]
14221513
let ``ToMd serialises EmbedParagraphs by delegating to Render()`` () =
14231514
// EmbedParagraphs was previously falling through to the catch-all '| _' branch,

0 commit comments

Comments
 (0)