forked from jason/cpolis
make headers id great again
This commit is contained in:
parent
351a1d2f77
commit
f9e16c7c36
@ -6,19 +6,38 @@ import (
|
|||||||
|
|
||||||
"github.com/microcosm-cc/bluemonday"
|
"github.com/microcosm-cc/bluemonday"
|
||||||
"github.com/yuin/goldmark"
|
"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) {
|
func ConvertToHTML(md string) (string, error) {
|
||||||
var buf bytes.Buffer
|
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)
|
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 := bluemonday.UGCPolicy()
|
||||||
html := p.Sanitize(buf.String())
|
p.AllowAttrs("id").OnElements("h1", "h2", "h3", "h4", "h5", "h6")
|
||||||
|
htmlOutput := p.Sanitize(buf.String())
|
||||||
|
|
||||||
return html, nil
|
return htmlOutput, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ConvertToPlain(md string) (string, error) {
|
func ConvertToPlain(md string) (string, error) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user