package builder import ( "bytes" "regexp" "strings" "github.com/yuin/goldmark" "github.com/yuin/goldmark/extension" "github.com/yuin/goldmark/parser" "github.com/yuin/goldmark/renderer/html" ) var md = goldmark.New( goldmark.WithExtensions( extension.GFM, extension.Table, extension.Strikethrough, extension.TaskList, ), goldmark.WithParserOptions( parser.WithAutoHeadingID(), ), goldmark.WithRendererOptions( // Allow raw HTML pass-through so component tags survive round-trip. html.WithUnsafe(), ), ) // MarkdownToHTML converts a markdown string to an HTML fragment. func MarkdownToHTML(body string) (string, error) { var buf bytes.Buffer if err := md.Convert([]byte(body), &buf); err != nil { return "", err } return buf.String(), nil } var ( htmlTagRe = regexp.MustCompile(`<[^>]+>`) multiSpaceRe = regexp.MustCompile(`\s+`) ) // StripHTML removes HTML tags and normalises whitespace for search indexing. func StripHTML(h string) string { plain := htmlTagRe.ReplaceAllString(h, " ") plain = multiSpaceRe.ReplaceAllString(plain, " ") return strings.TrimSpace(plain) }