How to interpolate variables in Markdown file?

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?

1 Like

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

Is Mustache.jl what you’re looking for?

1 Like

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

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

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(...)).

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.

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