-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
55 lines (44 loc) · 1.11 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"bytes"
"fmt"
"regexp"
"strconv"
"github.com/microcosm-cc/bluemonday"
"github.com/yuin/goldmark"
emoji "github.com/yuin/goldmark-emoji"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/renderer/html"
)
func parseMarkdownToHTML(text string) string {
var buf bytes.Buffer
md := goldmark.New(
goldmark.WithExtensions(
emoji.Emoji,
extension.GFM,
),
goldmark.WithRendererOptions(
html.WithUnsafe(), // Allow raw HTML if needed
),
)
if err := md.Convert([]byte(text), &buf); err != nil {
return text // Fall back to the original text on error
}
return buf.String()
}
func parseHTMLStrict(text string) string {
strict := bluemonday.StrictPolicy()
return strict.Sanitize(text)
}
func parseHTMLLessStrict(text string) string {
lessStrict := bluemonday.UGCPolicy()
return lessStrict.Sanitize(text)
}
func parseID(path string) (int64, error) {
re := regexp.MustCompile(`^/(thread|member)/([0-9]+)$`)
matches := re.FindStringSubmatch(path)
if len(matches) < 2 {
return 0, fmt.Errorf("invalid thread ID in URL")
}
return strconv.ParseInt(matches[2], 10, 64)
}