-
Notifications
You must be signed in to change notification settings - Fork 1
/
tplfunc.go
46 lines (38 loc) · 1.01 KB
/
tplfunc.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
package web
import (
"html/template"
"strings"
"time"
"github.com/go-whisper/go-whisper/app/instance"
"github.com/go-whisper/go-whisper/app/model"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/parser"
)
func init() {
service := instance.WebService()
service.SetFuncMap(template.FuncMap{
"TimeYear": TimeYear,
"TimeMonth": TimeMonth,
"TimeDay": TimeDay,
"MarkdownToHTML": MarkdownToHTML,
"StringListJoin": StringListJoin,
})
}
func StringListJoin(strList model.StringList) string {
return strings.Join(strList, ",")
}
func TimeYear(t time.Time) int {
return t.Year()
}
func TimeMonth(t time.Time) string {
return t.Month().String()
}
func TimeDay(t time.Time) int {
return t.Day()
}
func MarkdownToHTML(d string) template.HTML {
extensions := parser.CommonExtensions | parser.AutoHeadingIDs
parser := parser.NewWithExtensions(extensions)
d = strings.ReplaceAll(d, model.SummarySeparator(), "")
return template.HTML(markdown.ToHTML([]byte(d), parser, nil))
}