diff --git a/kernel/av/value.go b/kernel/av/value.go index ebf1317aa..d1708c292 100644 --- a/kernel/av/value.go +++ b/kernel/av/value.go @@ -303,19 +303,35 @@ const ( func NewFormattedValueDate(content, content2 int64, format DateFormat, isNotTime bool) (ret *ValueDate) { var formatted string - if isNotTime { - formatted = time.UnixMilli(content).Format("2006-01-02") - } else { - formatted = time.UnixMilli(content).Format("2006-01-02 15:04") + contentTime := time.UnixMilli(content) + if 0 == content || contentTime.IsZero() { + ret = &ValueDate{ + Content: content, + Content2: content2, + HasEndDate: false, + IsNotTime: true, + FormattedContent: formatted, + } + return } + + if isNotTime { + formatted = contentTime.Format("2006-01-02") + } else { + formatted = contentTime.Format("2006-01-02 15:04") + } + if 0 < content2 { var formattedContent2 string + content2Time := time.UnixMilli(content2) if isNotTime { - formattedContent2 = time.UnixMilli(content2).Format("2006-01-02") + formattedContent2 = content2Time.Format("2006-01-02") } else { - formattedContent2 = time.UnixMilli(content2).Format("2006-01-02 15:04") + formattedContent2 = content2Time.Format("2006-01-02 15:04") + } + if !content2Time.IsZero() { + formatted += " → " + formattedContent2 } - formatted += " → " + formattedContent2 } switch format { case DateFormatNone: diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index 8de0e42d3..14be072c3 100644 --- a/kernel/model/attribute_view.go +++ b/kernel/model/attribute_view.go @@ -604,7 +604,9 @@ func renderTemplateCol(ial map[string]string, tplContent string, rowValues []*av } goTpl := template.New("").Delims(".action{", "}") - goTpl = goTpl.Funcs(util.BuiltInTemplateFuncs()) + tplFuncMap := util.BuiltInTemplateFuncs() + SQLTemplateFuncs(&tplFuncMap) + goTpl = goTpl.Funcs(tplFuncMap) tpl, tplErr := goTpl.Parse(tplContent) if nil != tplErr { logging.LogWarnf("parse template [%s] failed: %s", tplContent, tplErr) diff --git a/kernel/model/export.go b/kernel/model/export.go index 118c37f87..4b06ac064 100644 --- a/kernel/model/export.go +++ b/kernel/model/export.go @@ -169,7 +169,7 @@ func ExportAv2CSV(avID string) (zipPath string, err error) { if nil != removeErr { logging.LogErrorf("remove export folder [%s] failed: %s", exportFolder, removeErr) } - zipPath = "/export/" + url.PathEscape(filepath.Base(zipPath)) + zipPath = "/export/csv/" + url.PathEscape(filepath.Base(zipPath)) return } diff --git a/kernel/model/template.go b/kernel/model/template.go index 43fa5190f..f6d8d6519 100644 --- a/kernel/model/template.go +++ b/kernel/model/template.go @@ -20,6 +20,7 @@ import ( "bytes" "errors" "fmt" + "github.com/araddon/dateparse" "io/fs" "os" "path/filepath" @@ -32,7 +33,6 @@ import ( "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" @@ -44,7 +44,9 @@ import ( func RenderGoTemplate(templateContent string) (ret string, err error) { tmpl := template.New("") - tmpl = tmpl.Funcs(util.BuiltInTemplateFuncs()) + tplFuncMap := util.BuiltInTemplateFuncs() + SQLTemplateFuncs(&tplFuncMap) + tmpl = tmpl.Funcs(tplFuncMap) tpl, err := tmpl.Parse(templateContent) if nil != err { return "", errors.New(fmt.Sprintf(Conf.Language(44), err.Error())) @@ -217,33 +219,11 @@ 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{", "}") - tpl, err := goTpl.Funcs(funcMap).Parse(gulu.Str.FromBytes(md)) + tplFuncMap := util.BuiltInTemplateFuncs() + SQLTemplateFuncs(&tplFuncMap) + goTpl = goTpl.Funcs(tplFuncMap) + tpl, err := goTpl.Funcs(tplFuncMap).Parse(gulu.Str.FromBytes(md)) if nil != err { return "", errors.New(fmt.Sprintf(Conf.Language(44), err.Error())) } @@ -414,3 +394,29 @@ func addBlockIALNodes(tree *parse.Tree, removeUpdated bool) { block.InsertAfter(&ast.Node{Type: ast.NodeKramdownBlockIAL, Tokens: parse.IAL2Tokens(block.KramdownIAL)}) } } + +func SQLTemplateFuncs(templateFuncMap *template.FuncMap) { + (*templateFuncMap)["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 + } + (*templateFuncMap)["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 + } + (*templateFuncMap)["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 + } +} diff --git a/kernel/treenode/node.go b/kernel/treenode/node.go index 0853c513b..e5f836cb4 100644 --- a/kernel/treenode/node.go +++ b/kernel/treenode/node.go @@ -962,9 +962,11 @@ 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{", "}") - tpl, tplErr := goTpl.Funcs(funcMap).Parse(tplContent) + tplFuncMap := util.BuiltInTemplateFuncs() + // 这里存在依赖问题所以不支持 SQLTemplateFuncs(&tplFuncMap) + goTpl = goTpl.Funcs(tplFuncMap) + tpl, tplErr := goTpl.Funcs(tplFuncMap).Parse(tplContent) if nil != tplErr { logging.LogWarnf("parse template [%s] failed: %s", tplContent, tplErr) return "" diff --git a/kernel/util/template.go b/kernel/util/template.go index 053790ade..a388c43f9 100644 --- a/kernel/util/template.go +++ b/kernel/util/template.go @@ -17,11 +17,10 @@ package util import ( - "math" - "text/template" - "github.com/Masterminds/sprig/v3" "github.com/spf13/cast" + "math" + "text/template" ) func BuiltInTemplateFuncs() (ret template.FuncMap) {