Skip to content

Commit

Permalink
Print markdown code with correct backticks
Browse files Browse the repository at this point in the history
This adds extra backticks and spaces around code fragments when printing
if the fragment contains backticks. This allows `Markdown.parse` to
properly re-parse text correctly, i.e.

    Markdown.plain(Markdown.parse("``` `x` ```")) == "``` `x` ```"

rather than

    Markdown.plain(Markdown.parse("``` `x` ```")) == "``x``"
  • Loading branch information
MichaelHatherly committed Jul 24, 2016
1 parent 43c4dca commit 302e0b8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
11 changes: 10 additions & 1 deletion base/markdown/render/plain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,16 @@ plaininline(io::IO, md::Bold) = plaininline(io, "**", md.text, "**")

plaininline(io::IO, md::Italic) = plaininline(io, "*", md.text, "*")

plaininline(io::IO, md::Code) = print(io, "`", md.code, "`")
function plaininline(io::IO, md::Code)
if contains(md.code, "`")
n = maximum(length(m) for m in matchall(r"(`+)", md.code))
s = "`"^((iseven(n) ? 1 : 2) + n)
print(io, s, Base.startswith(md.code, "`") ? " " : "")
print(io, md.code, endswith(md.code, "`") ? " " : "", s)
else
print(io, "`", md.code, "`")
end
end

plaininline(io::IO, br::LineBreak) = println(io)

Expand Down
5 changes: 5 additions & 0 deletions test/markdown.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ code_in_code = md"""
@test code_in_code == MD(Code("```"))
@test plain(code_in_code) == "````\n```\n````\n"

let text = "Foo ```bar` ``baz`` ```\n",
md = Markdown.parse(text)
@test text == Markdown.plain(md)
end

@test md"A footnote [^foo]." == MD(Paragraph(["A footnote ", Footnote("foo", nothing), "."]))

@test md"[^foo]: footnote" == MD(Paragraph([Footnote("foo", Any[" footnote"])]))
Expand Down

0 comments on commit 302e0b8

Please sign in to comment.