Trouble interpolating variables in Markdown strings

The fact that Markdown strings use $ for both latex inline math and interpolation leads to some unexpected behavior:

julia> a = "z"
"z"

julia> md"$a$a"
a

  a

julia> md" $a$a"
  aa

julia> md"$a $a"
"z"
  z

julia> md" $a $a"
  z z

julia> md" $a x$a"
  a xa

I am after a way to interpolate a variable without having a space preceed the $, as in md" $a x$a". Anyone have any tips for doing this?

IMHO, it would be better to only support inline math via

md"``1+1``"

(already supported) and use $ for interpolation only.

EDIT: I strongly suspect that I could implement a custom Markdown “flavour” that would work in this way and put it in a package. However I am finding it tricky to pin down the code that decides how to treat $. Any advice would be appreciated.

EDIT: I think I just need to import the appropriate functions and copy this line from stdlib/Markdown/src/Julia/Julia.jl but removing the blocktex and tex items:

@flavor julia [blocktex, blockinterp, hashheader, list, indentcode, fencedcode,
               blockquote, admonition, footnote, github_table, horizontalrule, setextheader, paragraph,

               linebreak, escapes, tex, interp, en_dash, inline_code,
               asterisk_bold, underscore_bold, asterisk_italic, underscore_italic, image, footnote_link, link, autolink]

No time now but will give it a go later and report back.

EDIT: My thanks to the devs for making this so easy. In 0.6:

julia> using Base.Markdown

julia> MD=Markdown
Base.Markdown

julia> MD.@flavor toms_md [
           MD.blockinterp,
           MD.hashheader,
           MD.list,
           MD.indentcode,
           MD.fencedcode,
           MD.blockquote,
           MD.admonition,
           MD.footnote,
           MD.github_table,
           MD.horizontalrule,
           MD.setextheader,
           MD.paragraph,
           MD.linebreak,
           MD.escapes,
           MD.interp,
           MD.en_dash,
           MD.inline_code,
           MD.asterisk_bold,
           #MD.underscore_bold, #0.7 only
           MD.asterisk_italic,
           #MD.underscore_italic, #0.7 only
           MD.image,
           MD.footnote_link,
           MD.link,
           MD.autolink,
       ]
Base.Markdown.Config(Function[Base.Markdown.hashheader, Base.Markdown.list, Base.Markdown.fencedcode, Base.Markdown.blockquote, Base.Markdown.admonition], Function[Base.Markdown.blockinterp, Base.Markdown.indentcode, Base.Markdown.footnote, Base.Markdown.github_table, Base.Markdown.horizontalrule, Base.Markdown.setextheader, Base.Markdown.paragraph], Dict('-'=>Function[Base.Markdown.en_dash],'!'=>Function[Base.Markdown.image],'\\'=>Function[Base.Markdown.linebreak, Base.Markdown.escapes],'*'=>Function[Base.Markdown.asterisk_bold, Base.Markdown.asterisk_italic],'$'=>Function[Base.Markdown.interp],'<'=>Function[Base.Markdown.autolink],'`'=>Function[Base.Markdown.inline_code],'['=>Function[Base.Markdown.footnote_link, Base.Markdown.link]))

julia> a = "z"
"z"

julia> md" $a$a"
  aa

julia> md" $a$a"toms_md
  zz

julia> md" ``a``$a"toms_md # use `` for math
  az
1 Like