Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 增加一些日期相关的模板函数 #9811

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 30 additions & 1 deletion kernel/model/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
Expand Down Expand Up @@ -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))
Expand Down