context.go

 1package ansi
 2
 3import (
 4	"html"
 5	"strings"
 6
 7	"github.com/microcosm-cc/bluemonday"
 8)
 9
10// RenderContext holds the current rendering options and state.
11type RenderContext struct {
12	options Options
13
14	blockStack *BlockStack
15	table      *TableElement
16
17	stripper *bluemonday.Policy
18}
19
20// NewRenderContext returns a new RenderContext.
21func NewRenderContext(options Options) RenderContext {
22	return RenderContext{
23		options:    options,
24		blockStack: &BlockStack{},
25		table:      &TableElement{},
26		stripper:   bluemonday.StrictPolicy(),
27	}
28}
29
30// SanitizeHTML sanitizes HTML content.
31func (ctx RenderContext) SanitizeHTML(s string, trimSpaces bool) string {
32	s = ctx.stripper.Sanitize(s)
33	if trimSpaces {
34		s = strings.TrimSpace(s)
35	}
36
37	return html.UnescapeString(s)
38}