Skip to content

Commit bc67f6b

Browse files
fredrikekreKristofferC
authored andcommitted
Fix string interpolation into Markdown.Table and Markdown.List in md"..." strings, fixes #16194. (#37130)
(cherry picked from commit d446c27)
1 parent 9f1cc7e commit bc67f6b

3 files changed

Lines changed: 48 additions & 3 deletions

File tree

stdlib/Markdown/src/Julia/interp.jl

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ function interpinner(stream::IO, greedy = false)
1111
startswith(stream, '$') || return
1212
(eof(stream) || peek(stream, Char) in whitespace) && return
1313
try
14-
return _parse(stream::IOBuffer, greedy = greedy)
14+
return _parse(stream, greedy = greedy)
1515
catch e
16-
return
16+
if isa(e, Meta.ParseError)
17+
return nothing
18+
else
19+
rethrow()
20+
end
1721
end
1822
end
1923

@@ -39,10 +43,19 @@ end
3943

4044
toexpr(x) = x
4145

42-
toexpr(xs::Vector{Any}) = Expr(:call, GlobalRef(Base,:getindex), Any, map(toexpr, xs)...)
46+
toexpr(xs::Union{Vector{Any},Vector{Vector{Any}}}) =
47+
Expr(:call, GlobalRef(Base,:getindex), Any, map(toexpr, xs)...)
4348

4449
for T in Any[MD, Paragraph, Header, Link, Bold, Italic]
4550
@eval function toexpr(md::$T)
4651
Expr(:call, typeof(md), $(map(x->:(toexpr(md.$x)), fieldnames(Base.unwrap_unionall(T)))...))
4752
end
4853
end
54+
55+
function toexpr(md::Table)
56+
Expr(:call, Table, toexpr(md.rows), md.align)
57+
end
58+
59+
function toexpr(md::List)
60+
Expr(:call, List, toexpr(md.items), md.ordered, md.loose)
61+
end

stdlib/Markdown/test/runtests.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,3 +1138,23 @@ let m = Markdown.parse("---"), io = IOBuffer()
11381138
show(io, "text/latex", m)
11391139
@test String(take!(io)) == "\\rule{\\textwidth}{1pt}\n"
11401140
end
1141+
1142+
# issue #16194: interpolation in md"..." strings
1143+
@testset "issue #16194: interpolation in md\"...\" strings" begin
1144+
x = "X"
1145+
contains_X(md) = occursin(x, sprint(show, MIME("text/plain"), md))
1146+
@test contains_X(md"# $x") # H1
1147+
@test contains_X(md"## $x") # H2
1148+
@test contains_X(md"### $x") # H3
1149+
@test contains_X(md"x = $x") # Paragraph
1150+
@test contains_X(md"- $x") # List
1151+
@test contains_X(md"[$x](..)") # Link
1152+
@test contains_X(md"**$x**") # Bold
1153+
@test contains_X(md"*$x*") # Italic
1154+
@test contains_X( # Table
1155+
md"""
1156+
| name |
1157+
|------|
1158+
| $x |
1159+
""")
1160+
end

stdlib/TOML/Manifest.toml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)