Thanks for posting more code. I see now that you want to encode the text as an HTML element attribute, not as Javascript. Note that it can be tricky to precisely use HTML inside Markdown – in theory it works, but there are all sorts of edge cases (like newline handling).
You’ve named your include code.html
, but because it is include in a Markdown file, it is interpreted as Markdown, so newlines are problematic.
One kludgy workaround is to URI-encode the text, and decode it as needed. For example:
<button
data-txt="{{ txt | uri_escape }}"
onclick="alert(decodeURI(this.getAttribute('data-txt')))">
Click Me
</button>
Stepping back, I think a better design would be to use innerText
to directly fetch the text from the code block, rather then duplicate it in a data attribute. This will avoid the duplication, and simplify the code and markup.