feature/id-for-hmtl-headers #3
@ -6,16 +6,35 @@ import (
|
||||
|
||||
"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 ConvertToHTML(md string) (string, error) {
|
||||
var buf bytes.Buffer
|
||||
|
||||
if err := goldmark.Convert([]byte(md), &buf); err != nil {
|
||||
// Goldmark-Instanz mit GFM und aktivierter Attribute-Unterstützung initialisieren.
|
||||
gm := goldmark.New(
|
||||
goldmark.WithExtensions(
|
||||
extension.GFM,
|
||||
),
|
||||
goldmark.WithParserOptions(
|
||||
parser.WithAttribute(),
|
||||
),
|
||||
goldmark.WithRendererOptions(
|
||||
html.WithUnsafe(), // Falls du HTML-Inhalte erlauben möchtest
|
||||
),
|
||||
)
|
||||
|
||||
// Markdown in HTML konvertieren.
|
||||
if err := gm.Convert([]byte(md), &buf); err != nil {
|
||||
return "", fmt.Errorf("error converting markdown to html: %v", err)
|
||||
}
|
||||
|
||||
// Bluemonday-Policy anpassen, sodass id-Attribute auf h1-h6 erlaubt sind.
|
||||
p := bluemonday.UGCPolicy()
|
||||
p.AllowAttrs("id").OnElements("h1", "h2", "h3", "h4", "h5", "h6")
|
||||
html := p.Sanitize(buf.String())
|
||||
|
||||
return html, nil
|
||||
|
Loading…
x
Reference in New Issue
Block a user