Adding "Blackfriday" as a custom renderer
Author | Topic |
---|---|
mysh
|
Posted on
Follow these steps to implement a custom renderer! Due to some design limitations, we need to use Go's build tags to replace render function. 1. Create `convert_blackfriday.go`Create 2. Set Up Build ConstraintsIn Add the build tags at the top: //go:build blackfriday // +build blackfriday In Ensure it’s used when //go:build !blackfriday // +build !blackfriday This way if we provide 3. Implement the `Convert` FunctionAdd the following to package syntax import ( "strings" "github.com/microcosm-cc/bluemonday" "github.com/russross/blackfriday/v2" ) func Convert(gmi string, wrap bool) string { clearedString := strings.ReplaceAll(gmi, "\r\n", "\n") unsafeHTML := blackfriday.Run([]byte(clearedString), blackfriday.WithExtensions(blackfriday.CommonExtensions)) safeHTML := bluemonday.UGCPolicy().SanitizeBytes(unsafeHTML) return string(safeHTML) } 4. Build with the `blackfriday` FlagCompile go build -tags blackfriday . This will use Last edited on |