forked from jason/cpolis
		
	Compare commits
	
		
			3 Commits
		
	
	
		
			devel
			...
			feature/id
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| ed1d456912 | |||
| 547c9a5567 | |||
| f9e16c7c36 | 
@@ -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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user