On GitHub, you can use HTML which is quite nice, especially for things like spoilers; It should be fairly trivial to add HTML support as far as I'm aware. If I'm not missing something major, I believe /appview/pages/markdown.go just needs to have the goldmark HTML renderer in unsafe mode passed into the bluemonday sanitizer (which is already a dependency of this repo). I would have opened a PR but not entirely sure the proper etiquette to do that with just branches :)
diff --git a/appview/pages/markdown.go b/appview/pages/markdown.go
index d27d906..4e92a0d 100644
--- a/appview/pages/markdown.go
+++ b/appview/pages/markdown.go
@@ -3,21 +3,44 @@ package pages
import (
"bytes"
+ "github.com/microcosm-cc/bluemonday"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
+ "github.com/yuin/goldmark/renderer/html"
)
func renderMarkdown(source string) string {
+ // Configure goldmark to allow raw HTML
md := goldmark.New(
goldmark.WithExtensions(extension.GFM),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
),
+ goldmark.WithRendererOptions(
+ html.WithHardWraps(),
+ html.WithUnsafe(), // Allow raw HTML to pass through
+ ),
)
+
+ // Convert markdown to HTML
var buf bytes.Buffer
if err := md.Convert([]byte(source), &buf); err != nil {
return source
}
- return buf.String()
+
+ // Create a policy that allows common HTML elements and attributes
+ policy := bluemonday.UGCPolicy()
+ // You can customize the policy to allow specific tags and attributes
+ policy.AllowElements("div", "span", "p", "br", "hr", "h1", "h2", "h3", "h4", "h5", "h6")
+ policy.AllowElements("ul", "ol", "li")
+ policy.AllowElements("table", "thead", "tbody", "tr", "th", "td")
+ policy.AllowElements("a", "img")
+ policy.AllowAttrs("href").OnElements("a")
+ policy.AllowAttrs("src", "alt").OnElements("img")
+ policy.AllowAttrs("class").Globally()
+
+ // Sanitize the HTML
+ sanitized := policy.Sanitize(buf.String())
+ return sanitized
}
I think spoilers like this should work in markdown too... well it all depends on how strict the UGCPolicy is
Hi