add alert template function md_to_html

This commit is contained in:
liugq 2023-08-08 11:32:01 +08:00
parent fc2d2a586e
commit 4cc69c8e53
4 changed files with 44 additions and 3 deletions

View File

@ -12,6 +12,7 @@ import (
"infini.sh/framework/core/global" "infini.sh/framework/core/global"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"time" "time"
log "github.com/cihub/seelog" log "github.com/cihub/seelog"
@ -190,6 +191,7 @@ func (h *AlertAPI) searchChannel(w http.ResponseWriter, req *http.Request, ps ht
strFrom = h.GetParameterOrDefault(req, "from", "0") strFrom = h.GetParameterOrDefault(req, "from", "0")
subType = h.GetParameterOrDefault(req, "sub_type", "") subType = h.GetParameterOrDefault(req, "sub_type", "")
typ = h.GetParameterOrDefault(req, "type", "") typ = h.GetParameterOrDefault(req, "type", "")
sort = h.GetParameterOrDefault(req, "sort", "updated:desc")
) )
mustQ := []interface{}{} mustQ := []interface{}{}
if keyword != "" { if keyword != "" {
@ -223,6 +225,18 @@ func (h *AlertAPI) searchChannel(w http.ResponseWriter, req *http.Request, ps ht
if from < 0 { if from < 0 {
from = 0 from = 0
} }
var (
sortField string
sortDirection string
)
sortParts := strings.Split(sort, ":")
sortField = sortParts[0]
if len(sortParts) >= 2 {
sortDirection = sortParts[1]
}
if sortDirection == "" {
sortDirection = "asc"
}
query := util.MapStr{ query := util.MapStr{
"size": size, "size": size,
"from": from, "from": from,
@ -231,6 +245,13 @@ func (h *AlertAPI) searchChannel(w http.ResponseWriter, req *http.Request, ps ht
"must": mustQ, "must": mustQ,
}, },
}, },
"sort": []util.MapStr{
{
sortField: util.MapStr{
"order": sortDirection,
},
},
},
} }
q := orm.Query{ q := orm.Query{

View File

@ -17,8 +17,8 @@ import (
"infini.sh/framework/core/orm" "infini.sh/framework/core/orm"
"infini.sh/framework/core/util" "infini.sh/framework/core/util"
"net/http" "net/http"
"src/github.com/buger/jsonparser" "github.com/buger/jsonparser"
"src/github.com/gopkg.in/gomail.v2" "github.com/gopkg.in/gomail.v2"
"strconv" "strconv"
"time" "time"
) )

View File

@ -33,5 +33,6 @@ var genericMap = map[string]interface{}{
"mul": mul, "mul": mul,
"lookup": lookup, "lookup": lookup,
"str_replace": replace, "str_replace": replace,
"md_to_html": mdToHTML,
//"get_keystore_secret": getKeystoreSecret, //"get_keystore_secret": getKeystoreSecret,
} }

View File

@ -4,7 +4,12 @@
package funcs package funcs
import "strings" import (
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
"strings"
)
func substring(start, end int, s string) string { func substring(start, end int, s string) string {
runes := []rune(s) runes := []rune(s)
@ -17,4 +22,18 @@ func substring(start, end int, s string) string {
func replace(old, new, src string) string { func replace(old, new, src string) string {
return strings.Replace(src, old, new, -1) return strings.Replace(src, old, new, -1)
}
func mdToHTML(mdText string) string {
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
p := parser.NewWithExtensions(extensions)
doc := p.Parse([]byte(mdText))
// create HTML renderer with extensions
htmlFlags := html.CommonFlags | html.HrefTargetBlank
opts := html.RendererOptions{Flags: htmlFlags}
renderer := html.NewRenderer(opts)
buf := markdown.Render(doc, renderer)
return string(buf)
} }