Skip to content

Commit 74bf7ad

Browse files
committed
🎨 New template functions markdown2text and markdown2content #13880
1 parent eaeb255 commit 74bf7ad

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

kernel/filesys/template.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"time"
2323

2424
"github.com/88250/go-humanize"
25+
"github.com/88250/lute/parse"
2526
util2 "github.com/88250/lute/util"
2627
"github.com/Masterminds/sprig/v3"
2728
"github.com/araddon/dateparse"
@@ -53,9 +54,31 @@ func BuiltInTemplateFuncs() (ret template.FuncMap) {
5354
ret["statBlock"] = StatBlock
5455
ret["runeCount"] = runeCount
5556
ret["wordCount"] = wordCount
57+
ret["markdown2text"] = markdown2text
58+
ret["markdown2content"] = markdown2content
5659
return
5760
}
5861

62+
func markdown2content(md string) (ret string) {
63+
luteEngine := util.NewLute()
64+
tree := parse.Parse("", []byte(md), luteEngine.ParseOptions)
65+
if nil == tree || nil == tree.Root {
66+
logging.LogWarnf("parse markdown [%s] failed", md)
67+
return
68+
}
69+
return tree.Root.Content()
70+
}
71+
72+
func markdown2text(md string) (ret string) {
73+
luteEngine := util.NewLute()
74+
tree := parse.Parse("", []byte(md), luteEngine.ParseOptions)
75+
if nil == tree || nil == tree.Root {
76+
logging.LogWarnf("parse markdown [%s] failed", md)
77+
return
78+
}
79+
return tree.Root.Text()
80+
}
81+
5982
func runeCount(s string) (ret int) {
6083
ret, _ = util2.WordCount(s)
6184
return

0 commit comments

Comments
 (0)