Displaying program generated markdown

Apologies if this is an obvious question…

I have a function that generates a big chunk of markdown with embedded newline characters. A simple version would be:


function makemd()
	md"# HELLO\n## Some\n	* list\n* list\n"	
end

That produces a string that prints correctly in the repl, but of course you can’t print to Pluto. So, how can I get the output to render nicely in Pluto, as when you simply type in a chunk of markdown? At present I just see the raw string, with the "\n"s embedded in it.

Graham

This works:

function makemd()
	md"""# HELLO
	## Some
	* list
	* list"""	
end

makemd()

Looks like the md macro automatically escapes \n.

Hi Lungbenm

Indeed that does work, but unfortunately it’s not possible for me to produce my markdown in that way: I’m actually producing quite big md tables line-by-line. Sorry if my example was misleading.

Graham

You can use Markdown.parse:

function makemd()
	Markdown.parse("# HELLO\n## Some\n	* list\n* list\n")
end

When str is a string of markdown code, Markdown.parse(str) and @eval @md_str $str work fine.

That’s brilliant; worked perfectly. Many thanks, both.

Graham

Hello. Apologies if this has in some way made easier, but the accepted solution does not seem to work anymore. I’ll be most grateful for any help I can get.

My MWE:

begin
	str = "| a | b |\n"
	str *= "| - | - |\n"
	for ind in 1:10
		str *= "| $(ind) | $(2*ind) |\n"
	end
	@eval @md_str $str
end

and the output I get

For some reason it seems that Markdown.parse requires at least three dashes (---) per column. Maybe file an issue to change that, or a PR to document this behavior…

Thank you so much! That is amazing :slight_smile: