2024-07-13 13:58:36 +02:00
|
|
|
package backend
|
2024-02-18 10:07:49 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
|
2024-02-22 15:22:45 +01:00
|
|
|
"github.com/microcosm-cc/bluemonday"
|
2024-02-18 10:07:49 +01:00
|
|
|
"github.com/yuin/goldmark"
|
|
|
|
)
|
|
|
|
|
2024-02-18 12:41:49 +01:00
|
|
|
func ConvertToHTML(md string) (string, error) {
|
2024-02-18 10:07:49 +01:00
|
|
|
var buf bytes.Buffer
|
|
|
|
|
2024-02-18 12:41:49 +01:00
|
|
|
if err := goldmark.Convert([]byte(md), &buf); err != nil {
|
2024-02-22 15:22:45 +01:00
|
|
|
return "", fmt.Errorf("error converting markdown to html: %v", err)
|
2024-02-18 10:07:49 +01:00
|
|
|
}
|
|
|
|
|
2024-02-22 15:22:45 +01:00
|
|
|
p := bluemonday.UGCPolicy()
|
|
|
|
html := p.Sanitize(buf.String())
|
|
|
|
|
|
|
|
return html, nil
|
2024-02-18 10:07:49 +01:00
|
|
|
}
|
2024-02-27 09:03:21 +01:00
|
|
|
|
|
|
|
func ConvertToPlain(md string) (string, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
if err := goldmark.Convert([]byte(md), &buf); err != nil {
|
|
|
|
return "", fmt.Errorf("error converting markdown to html: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := bluemonday.StrictPolicy()
|
|
|
|
plain := p.Sanitize(buf.String())
|
|
|
|
|
|
|
|
return plain, nil
|
|
|
|
}
|