How to interpolate variables in Markdown file?


#1

Say I have a file test.md which contains:

Hello $user_name

Then in the REPL I do:

julia> user_name = "John"
julia> Markdown.parse_file("test.md") |> Markdown.html

This will render Hello user_name instead of Hello John. How can I get it to eval the interpolated variables?


#2

Not possible / easily doable / efficient? Or just insufficiently documented?


#3

Is Mustache.jl what you’re looking for?


#4

Thanks. No Mustaches - I was specifically looking to use Markdown files.


#5

Do you want to be able to use arbitrary Julia code or just simple variable substitution?


#6

I would like to allow Markdown layouts and views in Genie, à la Jekyll. Functionality similar to string interpolation – so both $var and more complex $(...) – should be enough. Including large pieces of code into views is a bad practice anyway, so such code should go into a helper function that can be invoked via a construct like $(Helper.f(...)).


#7

That still means you need to support arbitrary Julia code.

Anyway you can do something like:

Markdown.parse(include_string(string('"', readstring("test.md"), '"'))) |> Markdown.html

Note that this will evaluate the file so you can be pwned if someone put bad stuff in there.


#8

@kristoffer.carlsson Nice, it works, thanks! That’s OK, these files are meant to be written by the developers.