🎨 The database template column supports queryBlocks function https://github.com/siyuan-note/siyuan/issues/10077

This commit is contained in:
Daniel 2024-01-04 22:04:29 +08:00
parent 9b6cb211ab
commit 20cd2ac696
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
3 changed files with 31 additions and 28 deletions

View file

@ -26,13 +26,11 @@ import (
"sort"
"strings"
"text/template"
"time"
"github.com/88250/gulu"
"github.com/88250/lute/ast"
"github.com/88250/lute/parse"
"github.com/88250/lute/render"
"github.com/araddon/dateparse"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/av"
@ -217,32 +215,8 @@ func renderTemplate(p, id string, preview bool) (string, error) {
dataModel["alias"] = block.Alias
}
funcMap := util.BuiltInTemplateFuncs()
funcMap["queryBlocks"] = func(stmt string, args ...string) (ret []*sql.Block) {
for _, arg := range args {
stmt = strings.Replace(stmt, "?", arg, 1)
}
ret = sql.SelectBlocksRawStmt(stmt, 1, Conf.Search.Limit)
return
}
funcMap["querySpans"] = func(stmt string, args ...string) (ret []*sql.Span) {
for _, arg := range args {
stmt = strings.Replace(stmt, "?", arg, 1)
}
ret = sql.SelectSpansRawStmt(stmt, Conf.Search.Limit)
return
}
funcMap["parseTime"] = func(dateStr string) time.Time {
now := time.Now()
ret, err := dateparse.ParseIn(dateStr, now.Location())
if nil != err {
logging.LogWarnf("parse date [%s] failed [%s], return current time instead", dateStr, err)
return now
}
return ret
}
goTpl := template.New("").Delims(".action{", "}")
funcMap := util.BuiltInTemplateFuncs()
tpl, err := goTpl.Funcs(funcMap).Parse(gulu.Str.FromBytes(md))
if nil != err {
return "", errors.New(fmt.Sprintf(Conf.Language(44), err.Error()))

View file

@ -962,8 +962,8 @@ func renderTemplateCol(ial map[string]string, tplContent string, rowValues []*av
ial["updated"] = time.UnixMilli(block.Block.Updated).Format("20060102150405")
}
funcMap := util.BuiltInTemplateFuncs()
goTpl := template.New("").Delims(".action{", "}")
funcMap := util.BuiltInTemplateFuncs()
tpl, tplErr := goTpl.Funcs(funcMap).Parse(tplContent)
if nil != tplErr {
logging.LogWarnf("parse template [%s] failed: %s", tplContent, tplErr)

View file

@ -18,9 +18,14 @@ package util
import (
"math"
"strings"
"text/template"
"time"
"github.com/Masterminds/sprig/v3"
"github.com/araddon/dateparse"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/spf13/cast"
)
@ -34,6 +39,30 @@ func BuiltInTemplateFuncs() (ret template.FuncMap) {
ret["powf"] = powf
ret["log"] = log
ret["logf"] = logf
ret["queryBlocks"] = func(stmt string, args ...string) (retBlocks []*sql.Block) {
for _, arg := range args {
stmt = strings.Replace(stmt, "?", arg, 1)
}
retBlocks = sql.SelectBlocksRawStmt(stmt, 1, 512)
return
}
ret["querySpans"] = func(stmt string, args ...string) (retSpans []*sql.Span) {
for _, arg := range args {
stmt = strings.Replace(stmt, "?", arg, 1)
}
retSpans = sql.SelectSpansRawStmt(stmt, 512)
return
}
ret["parseTime"] = func(dateStr string) time.Time {
now := time.Now()
retTime, err := dateparse.ParseIn(dateStr, now.Location())
if nil != err {
logging.LogWarnf("parse date [%s] failed [%s], return current time instead", dateStr, err)
return now
}
return retTime
}
return
}