Closed TACIXAT closed 5 months ago
There's no built-in option like that.
There's SkipHTML
flag on HTML renderer that doesn't output HTML blocks.
One way is to customize renderer and escape ast.HTMLBlock nodes (see https://blog.kowalczyk.info/article/cxn3/advanced-markdown-processing-in-go.html#customizing-html-renderer)
The original code is:
func (r *Renderer) HTMLBlock(w io.Writer, node *ast.HTMLBlock) {
if r.Opts.Flags&SkipHTML != 0 {
return
}
r.CR(w)
r.Out(w, node.Literal)
r.CR(w)
}
You would just do:
func (r *Renderer) HTMLBlock(w io.Writer, node *ast.HTMLBlock) {
s := EscapeHTML(node.Literal)
r.CR(w)
r.Out(w, s)
r.CR(w)
}
Instead of simple escaping you can sanitize with e.g. https://github.com/microcosm-cc/bluemonday
Sanitizing only removes dangerous HTML and leaves the non-dangerous.
Instead of customizing a renderer, you can also traverse parsed ast before html rendering and replace Literal of ast.HTMLBlock with escaped / sanitized version (https://blog.kowalczyk.info/article/cxn3/advanced-markdown-processing-in-go.html#modify-ast-tree)
Thank you!
Let's say I had a markdown document:
This is rendered to HTML unescaped as:
I guess this is working as intended because you are supposed to be able to just add HTML to markdown according to this answer.
If you put it in backticks ` it escapes it:
Is there an option to always escape, even if the HTML is not in backticks? .