Concatenate markdown strings

For ordinary strings, "as"*"df" produces "asdf". For markdown strings, md"as"*md"df" produces an error. So does md_str("as"*"df"). Is there any way to reinterpret a string with a complicated history as markdown?

Eventually I would like to generate markdown strings programmatically (for automatic display in a Pluto notebook). Resolution of the simplified question above would probably get me on the right track. Thanks!

1 Like

This was an interesting question! After reading the source of Markdown.jl that is located in julia/stdlib/v1.6/Markdown/src I came up with this solution:

using Markdown
Markdown.parse("a"*"b")
str = "a"*"b"
Markdown.parse(str)

That is, we bypass the macro and call the underlying function directly.

1 Like

If you’re going to be generating a lot of text programmatically you may want to use an IOBuffer instead of string concatenation:

julia> io = IOBuffer();

julia> print(io, "a")

julia> print(io, "b")

julia> Markdown.parse(seekstart(io))
  ab
2 Likes

This isn’t quite right but maybe you can combine the content attributes of the objects.


julia> let x = md"hello"; y = md"world"; 
Markdown.MD(vcat(x.content[1].content , y.content[1].content)) 
end
"hello"

"world"
1 Like

Perfect: two working methods in 4 lines total. Thank you! See also the contribution from mike below.

Thank you. The earlier solution from HenrikM covers my needs completely, but this one includes extra info with no computational overhead. I’m marking this one as the solution go give impatient future searchers the fullest possible story.