From 0ff99f758ad8d7ac84b3d5ae27fdb013eaaefe9d Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Thu, 19 Sep 2024 22:18:43 +0800 Subject: [PATCH] :art: Add a template function `getHPathByID` https://github.com/siyuan-note/siyuan/issues/11734 --- kernel/model/template.go | 4 ++-- kernel/sql/av.go | 2 +- kernel/{util => treenode}/template.go | 21 ++++++++++++++++----- 3 files changed, 19 insertions(+), 8 deletions(-) rename kernel/{util => treenode}/template.go (84%) diff --git a/kernel/model/template.go b/kernel/model/template.go index 2015711d83d..be8a6a1191d 100644 --- a/kernel/model/template.go +++ b/kernel/model/template.go @@ -42,7 +42,7 @@ import ( func RenderGoTemplate(templateContent string) (ret string, err error) { tmpl := template.New("") - tplFuncMap := util.BuiltInTemplateFuncs() + tplFuncMap := treenode.BuiltInTemplateFuncs() sql.SQLTemplateFuncs(&tplFuncMap) tmpl = tmpl.Funcs(tplFuncMap) tpl, err := tmpl.Parse(templateContent) @@ -224,7 +224,7 @@ func RenderTemplate(p, id string, preview bool) (tree *parse.Tree, dom string, e } goTpl := template.New("").Delims(".action{", "}") - tplFuncMap := util.BuiltInTemplateFuncs() + tplFuncMap := treenode.BuiltInTemplateFuncs() sql.SQLTemplateFuncs(&tplFuncMap) goTpl = goTpl.Funcs(tplFuncMap) tpl, err := goTpl.Funcs(tplFuncMap).Parse(gulu.Str.FromBytes(md)) diff --git a/kernel/sql/av.go b/kernel/sql/av.go index fa185f0fdd9..4c9ace3ff59 100644 --- a/kernel/sql/av.go +++ b/kernel/sql/av.go @@ -404,7 +404,7 @@ func RenderTemplateCol(ial map[string]string, rowValues []*av.KeyValues, tplCont } goTpl := template.New("").Delims(".action{", "}") - tplFuncMap := util.BuiltInTemplateFuncs() + tplFuncMap := treenode.BuiltInTemplateFuncs() SQLTemplateFuncs(&tplFuncMap) goTpl = goTpl.Funcs(tplFuncMap) tpl, err := goTpl.Parse(tplContent) diff --git a/kernel/util/template.go b/kernel/treenode/template.go similarity index 84% rename from kernel/util/template.go rename to kernel/treenode/template.go index 2f133364b33..84561fc6106 100644 --- a/kernel/util/template.go +++ b/kernel/treenode/template.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -package util +package treenode import ( "math" @@ -25,21 +25,23 @@ import ( "github.com/Masterminds/sprig/v3" "github.com/araddon/dateparse" "github.com/siyuan-note/logging" + "github.com/siyuan-note/siyuan/kernel/util" "github.com/spf13/cast" ) func BuiltInTemplateFuncs() (ret template.FuncMap) { ret = sprig.TxtFuncMap() - ret["Weekday"] = Weekday - ret["WeekdayCN"] = WeekdayCN - ret["WeekdayCN2"] = WeekdayCN2 - ret["ISOWeek"] = ISOWeek + ret["Weekday"] = util.Weekday + ret["WeekdayCN"] = util.WeekdayCN + ret["WeekdayCN2"] = util.WeekdayCN2 + ret["ISOWeek"] = util.ISOWeek ret["pow"] = pow ret["powf"] = powf ret["log"] = log ret["logf"] = logf ret["parseTime"] = parseTime ret["FormatFloat"] = FormatFloat + ret["getHPathByID"] = getHPathByID return } @@ -63,3 +65,12 @@ func parseTime(dateStr string) time.Time { func FormatFloat(format string, n float64) string { return humanize.FormatFloat(format, n) } + +func getHPathByID(id string) (ret string) { + bt := GetBlockTree(id) + if nil == bt { + return + } + ret = bt.HPath + return +}