Skip to content

Commit 3be99c5

Browse files
authored
Fix Markdown word wrap (#38502)
* Revert "fix markdown rendering (#37235)" This reverts commit 36505aa. * Fix Markdown word-wrap (fixes #38275) This changes one test, but IMO the extra spaces in the test result are not actually desirable. The alignment on Julia 1.5 is also off-by-one, whereas this seems well-aligned.
1 parent 8c7464e commit 3be99c5

File tree

3 files changed

+50
-23
lines changed

3 files changed

+50
-23
lines changed

stdlib/Markdown/src/render/terminal/formatting.jl

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ end
99
words(s) = split(s, " ")
1010
lines(s) = split(s, "\n")
1111

12-
function _wrapped_lines(s::AbstractString, width, i)
12+
function wrapped_lines!(lines, io::IO, s::AbstractString, width, i)
1313
ws = words(s)
14-
lines = String[]
1514
for word in ws
1615
word_length = ansi_length(word)
17-
if i + word_length + 1 > width
16+
word_length == 0 && continue
17+
if isempty(lines) || i + word_length + 1 > width
1818
i = word_length
1919
push!(lines, word)
2020
else
2121
i += word_length + 1
22-
if isempty(lines)
23-
push!(lines, word)
24-
else
25-
lines[end] *= " " * word # this could be more efficient
26-
end
22+
lines[end] *= " " * word # this could be more efficient
2723
end
2824
end
29-
return i, lines
25+
return i
3026
end
3127

3228
function wrapped_lines(io::IO, s::AbstractString; width = 80, i = 0)
33-
lines = String[]
34-
for ss in split(s, "\n")
35-
i, line = _wrapped_lines(ss, width, i)
36-
append!(lines, line)
29+
lines = AbstractString[]
30+
if occursin(r"\n", s)
31+
for ss in split(s, "\n")
32+
i = wrapped_lines!(lines, io, ss, width, i)
33+
end
34+
else
35+
wrapped_lines!(lines, io, s, width, i)
3736
end
3837
return lines
3938
end

stdlib/Markdown/src/render/terminal/render.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ end
7070
function term(io::IO, md::List, columns)
7171
for (i, point) in enumerate(md.items)
7272
print(io, ' '^2margin, isordered(md) ? "$(i + md.ordered - 1). " : "• ")
73-
print_wrapped(io, width = columns-(4margin+2), pre = ' '^(2margin+2),
73+
print_wrapped(io, width = columns-(4margin+2), pre = ' '^(2margin+3),
7474
i = 2margin+2) do io
7575
term(io, point, columns - 10)
7676
end

stdlib/Markdown/test/runtests.jl

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,42 @@ end
272272
| L |
273273
""") == " │ Tables in admonitions\n │\n │ R\n │ –\n │ L"
274274

275+
# Issue #38275
276+
function test_list_wrap(str, lenmin, lenmax)
277+
strs = split(str, '\n')
278+
l = length.(strs)
279+
for i = 1:length(l)-1
280+
if l[i] != 0 && l[i+1] != 0 # the next line isn't blank, so this line should be "full"
281+
lenmin <= l[i] <= lenmax || return false
282+
else
283+
l[i] <= lenmax || return false # this line isn't too long (but there is no min)
284+
end
285+
end
286+
# Check consistent indentation
287+
rngs = findfirst.((". ",), strs)
288+
k = last(rngs[1])
289+
rex = Regex('^' * " "^k * "\\w")
290+
for (i, rng) in enumerate(rngs)
291+
isa(rng, AbstractRange) && last(rng) == k && continue # every numbered line starts the text at the same position
292+
rng === nothing && (isempty(strs[i]) || match(rex, strs[i]) !== nothing) && continue # every unnumbered line is indented to text in numbered lines
293+
return false
294+
end
295+
return true
296+
end
297+
298+
let doc =
299+
md"""
300+
1. a bc def ghij a bc def ghij a bc def ghij a bc def ghij a bc def ghij a bc def ghij a bc def ghij a bc def ghij a bc def ghij
301+
2. a bc def ghij a bc def ghij a bc def ghij a bc def ghij a bc def ghij a bc def ghij a bc def ghij a bc def ghij a bc def ghij
302+
"""
303+
str = sprint(term, doc, 50)
304+
@test test_list_wrap(str, 40, 50)
305+
str = sprint(term, doc, 60)
306+
@test test_list_wrap(str, 50, 60)
307+
str = sprint(term, doc, 80)
308+
@test test_list_wrap(str, 70, 80)
309+
end
310+
275311
# HTML output
276312
@test md"foo *bar* baz" |> html == "<p>foo <em>bar</em> baz</p>\n"
277313
@test md"something ***" |> html == "<p>something ***</p>\n"
@@ -340,7 +376,7 @@ table = md"""
340376
# mime output
341377
let out =
342378
@test sprint(show, "text/plain", book) ==
343-
" Title\n ≡≡≡≡≡≡≡\n\n Some discussion\n\n │ A quote\n\n Section important\n ===================\n\n Some bolded\n\n • list1\n\n • list2"
379+
" Title\n ≡≡≡≡≡≡≡\n\n Some discussion\n\n │ A quote\n\n Section important\n ===================\n\n Some bolded\n\n • list1\n\n • list2"
344380
@test sprint(show, "text/markdown", book) ==
345381
"""
346382
# Title
@@ -1177,11 +1213,3 @@ end
11771213
| $x |
11781214
""")
11791215
end
1180-
1181-
@testset "issue #37232: linebreaks" begin
1182-
s = @md_str """
1183-
Misc:\\
1184-
- line\\
1185-
"""
1186-
@test sprint(show, MIME("text/plain"), s) == " Misc:\n - line\n "
1187-
end

0 commit comments

Comments
 (0)