feat: Add some template function about date (#9812)
* ✨ feat(template): Add some template func about week of date * ✨ feat: rename `WeekOfYear` to `ISOWeek` * fix: 命名错误
This commit is contained in:
parent
0d93d14368
commit
86547f77b5
1 changed files with 30 additions and 1 deletions
|
@ -43,8 +43,34 @@ import (
|
|||
"github.com/siyuan-note/siyuan/kernel/util"
|
||||
)
|
||||
|
||||
/**
|
||||
* [0-6] represents [Sunday - Saturday]
|
||||
*/
|
||||
func Weekday(date time.Time) int {
|
||||
return int(date.Weekday())
|
||||
}
|
||||
|
||||
func WeekdayCN(date time.Time) string {
|
||||
week := GetWeekday(date)
|
||||
weekdayCN := []string{"日", "一", "二", "三", "四", "五", "六"}
|
||||
return weekdayCN[week]
|
||||
}
|
||||
|
||||
// date is in `week`-th weak of this year
|
||||
func ISOWeek(date time.Time) int {
|
||||
_, week := date.ISOWeek()
|
||||
return week
|
||||
}
|
||||
|
||||
func RenderGoTemplate(templateContent string) (ret string, err error) {
|
||||
tpl, err := template.New("").Funcs(sprig.TxtFuncMap()).Parse(templateContent)
|
||||
tmpl := template.New("")
|
||||
tmpl = tmpl.Funcs(sprig.TxtFuncMap())
|
||||
tmpl = tmpl.Funcs(template.FuncMap{
|
||||
"Weekday": Weekday,
|
||||
"WeekdayCN": WeekdayCN,
|
||||
"ISOWeek": ISOWeek,
|
||||
})
|
||||
tpl, err := tmpl.Parse(templateContent)
|
||||
if nil != err {
|
||||
return "", errors.New(fmt.Sprintf(Conf.Language(44), err.Error()))
|
||||
}
|
||||
|
@ -240,6 +266,9 @@ func renderTemplate(p, id string, preview bool) (string, error) {
|
|||
}
|
||||
return ret
|
||||
}
|
||||
funcMap["Weekday"] = Weekday
|
||||
funcMap["WeekdayCN"] = WeekdayCN
|
||||
funcMap["ISOWeek"] = ISOWeek
|
||||
|
||||
goTpl := template.New("").Delims(".action{", "}")
|
||||
tpl, err := goTpl.Funcs(funcMap).Parse(gulu.Str.FromBytes(md))
|
||||
|
|
Loading…
Add table
Reference in a new issue