Adding "Blackfriday" as a custom renderer

Author Topic
mysh

m

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 convert_blackfriday.go in the /syntax folder.

2. Set Up Build Constraints

In convert_blackfriday.go

Add the build tags at the top:

//go:build blackfriday
// +build blackfriday

In convert_vanilla.go

Ensure it’s used when blackfriday is not set:

//go:build !blackfriday
// +build !blackfriday

This way if we provide -tags blackfriday we will use convert_blackfriday.go and if not, then convert_vanilla.go.

3. Implement the Convert Function

Add the following to convert_blackfriday.go:

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 Flag

Compile vpub-plus using the build tag:

go build -tags blackfriday .

This will use convert_blackfriday.go instead of convert_vanilla.go.

Last edited on