Making use of HTML stash in a Python-Markdown extension
For this website, I wrote a custom Python-Markdown extension to support converting a standard JSON format for LLM chats into HTML. I wrote it a while ago and only had fairly simple tests for it. Everything was fine until my first article to make use of it.
I ran into issue in a Javascript file displayed in a code fence, which itself contained things that looked like mardown (**
, ```, etc...). I couldn't figure out how my extension was doing this and then I realized that a downstream (builtin) processor was modifying my HTML.
The solution was to use the HTML stash:
user_div = SubElement(parent, 'div')
html = '...'
user_div.text = self.parser.md.htmlStash.store(html)
This stores the raw HTML in the stash and returns a placeholder string that's inserted into the element tree. A postprocessor will replace the placeholder with the stashed HTML, keeping it intact.