Replace placeholder string in text field with HTML generated by Asset query
This is partially side stepping your question but, it could be a solid option just the same: 

You might consider checking out Selvin's Doxter Markdown plugin. It has support for Entries, Assets, Users, Tags, Globals and Categories.



This all via reference codes which can be copied and pasted into the Markdown as needed.