🎨 Add word count template function runeCount and wordCount https://github.com/siyuan-note/siyuan/issues/13625

This commit is contained in:
Daniel 2024-12-25 19:31:56 +08:00
parent 353cf08fd9
commit 2f1885b07e
No known key found for this signature in database
GPG Key ID: 86211BA83DF03017
5 changed files with 326 additions and 43 deletions

View File

@ -6,7 +6,7 @@
"id": "20201204184532-3qm9l8n", "id": "20201204184532-3qm9l8n",
"title": "Template snippet", "title": "Template snippet",
"type": "doc", "type": "doc",
"updated": "20241224162810" "updated": "20241225192808"
}, },
"Children": [ "Children": [
{ {
@ -57,7 +57,7 @@
"HeadingLevel": 2, "HeadingLevel": 2,
"Properties": { "Properties": {
"id": "20210104091444-jy56z0p", "id": "20210104091444-jy56z0p",
"updated": "20241224162810" "updated": "20241225192808"
}, },
"Children": [ "Children": [
{ {
@ -215,7 +215,7 @@
"ListData": {}, "ListData": {},
"Properties": { "Properties": {
"id": "20210104091444-mwbvc9m", "id": "20210104091444-mwbvc9m",
"updated": "20241224162810" "updated": "20241225192808"
}, },
"Children": [ "Children": [
{ {
@ -249,7 +249,15 @@
}, },
{ {
"Type": "NodeText", "Type": "NodeText",
"Data": ": Use this variable to insert the current document name. For example, if the template content is " "Data": ": Use this variable to insert the current document name. For example, if the "
},
{
"Type": "NodeText",
"Data": "template"
},
{
"Type": "NodeText",
"Data": " content is "
}, },
{ {
"Type": "NodeTextMark", "Type": "NodeTextMark",
@ -443,7 +451,15 @@
}, },
{ {
"Type": "NodeText", "Type": "NodeText",
"Data": ": This function is used to query the database, and the return value is a list of blocks" "Data": ": This function is used to query the "
},
{
"Type": "NodeText",
"Data": "database"
},
{
"Type": "NodeText",
"Data": ", and the return value is a list of blocks"
} }
] ]
}, },
@ -453,6 +469,7 @@
"IsFencedCodeBlock": true, "IsFencedCodeBlock": true,
"Properties": { "Properties": {
"id": "20241213215602-dk7iion", "id": "20241213215602-dk7iion",
"style": "line-height: 22px;",
"updated": "20241213215602" "updated": "20241213215602"
}, },
"Children": [ "Children": [
@ -506,7 +523,15 @@
}, },
{ {
"Type": "NodeText", "Type": "NodeText",
"Data": ": This function is used to query the database, and the return value is a list of spans" "Data": ": This function is used to query the "
},
{
"Type": "NodeText",
"Data": "database"
},
{
"Type": "NodeText",
"Data": ", and the return value is a list of spans"
} }
] ]
}, },
@ -516,6 +541,7 @@
"IsFencedCodeBlock": true, "IsFencedCodeBlock": true,
"Properties": { "Properties": {
"id": "20241213215606-a1mq6zf", "id": "20241213215606-a1mq6zf",
"style": "line-height: 22px;",
"updated": "20241224162810" "updated": "20241224162810"
}, },
"Children": [ "Children": [
@ -569,7 +595,15 @@
}, },
{ {
"Type": "NodeText", "Type": "NodeText",
"Data": ": This function is used to query the database and the return value is a result set" "Data": ": This function is used to query the "
},
{
"Type": "NodeText",
"Data": "database"
},
{
"Type": "NodeText",
"Data": " and the return value is a result set"
} }
] ]
}, },
@ -579,6 +613,7 @@
"IsFencedCodeBlock": true, "IsFencedCodeBlock": true,
"Properties": { "Properties": {
"id": "20241213215554-rox3jk7", "id": "20241213215554-rox3jk7",
"style": "line-height: 22px;",
"updated": "20241213215554" "updated": "20241213215554"
}, },
"Children": [ "Children": [
@ -642,6 +677,7 @@
"IsFencedCodeBlock": true, "IsFencedCodeBlock": true,
"Properties": { "Properties": {
"id": "20241213215714-u0j3i4k", "id": "20241213215714-u0j3i4k",
"style": "line-height: 22px;",
"updated": "20241213215714" "updated": "20241213215714"
}, },
"Children": [ "Children": [
@ -860,7 +896,7 @@
"Type": "NodeParagraph", "Type": "NodeParagraph",
"Properties": { "Properties": {
"id": "20241212163841-d8t3qxb", "id": "20241212163841-d8t3qxb",
"updated": "20241212163918" "updated": "20241225192802"
}, },
"Children": [ "Children": [
{ {
@ -870,11 +906,48 @@
{ {
"Type": "NodeTextMark", "Type": "NodeTextMark",
"TextMarkType": "code", "TextMarkType": "code",
"TextMarkTextContent": "runeLen" "TextMarkTextContent": "runeCount"
}, },
{ {
"Type": "NodeText", "Type": "NodeText",
"Data": ": This function is used to return the string length" "Data": ": This function is used to return the number of characters in a string"
}
]
}
]
},
{
"ID": "20241225192802-4sojmmk",
"Type": "NodeListItem",
"ListData": {
"BulletChar": 42,
"Marker": "Kg=="
},
"Properties": {
"id": "20241225192802-4sojmmk",
"updated": "20241225192808"
},
"Children": [
{
"ID": "20241225192802-0rma1gp",
"Type": "NodeParagraph",
"Properties": {
"id": "20241225192802-0rma1gp",
"updated": "20241225192808"
},
"Children": [
{
"Type": "NodeText",
"Data": ""
},
{
"Type": "NodeTextMark",
"TextMarkType": "code",
"TextMarkTextContent": "wordCount"
},
{
"Type": "NodeText",
"Data": ": This function is used to return the number of words in a string"
} }
] ]
} }

View File

@ -6,7 +6,7 @@
"id": "20201204181006-7bkppue", "id": "20201204181006-7bkppue",
"title": "模板片段", "title": "模板片段",
"type": "doc", "type": "doc",
"updated": "20241224162826" "updated": "20241225192538"
}, },
"Children": [ "Children": [
{ {
@ -69,7 +69,7 @@
"HeadingLevel": 2, "HeadingLevel": 2,
"Properties": { "Properties": {
"id": "20210104091309-fhb549c", "id": "20210104091309-fhb549c",
"updated": "20241224162826" "updated": "20241225192538"
}, },
"Children": [ "Children": [
{ {
@ -237,7 +237,7 @@
"ListData": {}, "ListData": {},
"Properties": { "Properties": {
"id": "20210104091309-gjkg3u5", "id": "20210104091309-gjkg3u5",
"updated": "20241224162826" "updated": "20241225192538"
}, },
"Children": [ "Children": [
{ {
@ -271,7 +271,15 @@
}, },
{ {
"Type": "NodeText", "Type": "NodeText",
"Data": "​:该变量用于插入当前文档名。比如模板内容为 " "Data": "​:该变量用于插入当前文档名。比如"
},
{
"Type": "NodeText",
"Data": "模板"
},
{
"Type": "NodeText",
"Data": "内容为 "
}, },
{ {
"Type": "NodeTextMark", "Type": "NodeTextMark",
@ -465,7 +473,15 @@
}, },
{ {
"Type": "NodeText", "Type": "NodeText",
"Data": "​:该函数用于查询数据库,返回值为 blocks 列表" "Data": "​:该函数用于查询"
},
{
"Type": "NodeText",
"Data": "数据库"
},
{
"Type": "NodeText",
"Data": ",返回值为 blocks 列表"
} }
] ]
}, },
@ -475,6 +491,7 @@
"IsFencedCodeBlock": true, "IsFencedCodeBlock": true,
"Properties": { "Properties": {
"id": "20210604111446-p6vadfc", "id": "20210604111446-p6vadfc",
"style": "line-height: 22px;",
"updated": "20241213215118" "updated": "20241213215118"
}, },
"Children": [ "Children": [
@ -528,7 +545,15 @@
}, },
{ {
"Type": "NodeText", "Type": "NodeText",
"Data": "​:该函数用于查询数据库,返回值为 spans 列表" "Data": "​:该函数用于查询"
},
{
"Type": "NodeText",
"Data": "数据库"
},
{
"Type": "NodeText",
"Data": ",返回值为 spans 列表"
} }
] ]
}, },
@ -538,6 +563,7 @@
"IsFencedCodeBlock": true, "IsFencedCodeBlock": true,
"Properties": { "Properties": {
"id": "20241213215337-rhkoc8k", "id": "20241213215337-rhkoc8k",
"style": "line-height: 22px;",
"updated": "20241224162826" "updated": "20241224162826"
}, },
"Children": [ "Children": [
@ -591,7 +617,15 @@
}, },
{ {
"Type": "NodeText", "Type": "NodeText",
"Data": "​:该函数用于查询数据库,返回值为结果集" "Data": "​:该函数用于查询"
},
{
"Type": "NodeText",
"Data": "数据库"
},
{
"Type": "NodeText",
"Data": ",返回值为结果集"
} }
] ]
}, },
@ -601,6 +635,7 @@
"IsFencedCodeBlock": true, "IsFencedCodeBlock": true,
"Properties": { "Properties": {
"id": "20241213215240-f33bsqs", "id": "20241213215240-f33bsqs",
"style": "line-height: 22px;",
"updated": "20241213215421" "updated": "20241213215421"
}, },
"Children": [ "Children": [
@ -664,6 +699,7 @@
"IsFencedCodeBlock": true, "IsFencedCodeBlock": true,
"Properties": { "Properties": {
"id": "20241212101810-6ffociu", "id": "20241212101810-6ffociu",
"style": "line-height: 22px;",
"updated": "20241213215655" "updated": "20241213215655"
}, },
"Children": [ "Children": [
@ -882,7 +918,7 @@
"Type": "NodeParagraph", "Type": "NodeParagraph",
"Properties": { "Properties": {
"id": "20241212163704-i9p11h6", "id": "20241212163704-i9p11h6",
"updated": "20241212163739" "updated": "20241225192521"
}, },
"Children": [ "Children": [
{ {
@ -892,11 +928,48 @@
{ {
"Type": "NodeTextMark", "Type": "NodeTextMark",
"TextMarkType": "code", "TextMarkType": "code",
"TextMarkTextContent": "runeLen" "TextMarkTextContent": "runeCount"
}, },
{ {
"Type": "NodeText", "Type": "NodeText",
"Data": "​:该函数用于返回字符串长度" "Data": "​:该函数用于返回字符串中的字符数"
}
]
}
]
},
{
"ID": "20241225192524-3b660ss",
"Type": "NodeListItem",
"ListData": {
"BulletChar": 42,
"Marker": "Kg=="
},
"Properties": {
"id": "20241225192524-3b660ss",
"updated": "20241225192538"
},
"Children": [
{
"ID": "20241225192524-vyuve5f",
"Type": "NodeParagraph",
"Properties": {
"id": "20241225192524-vyuve5f",
"updated": "20241225192538"
},
"Children": [
{
"Type": "NodeText",
"Data": ""
},
{
"Type": "NodeTextMark",
"TextMarkType": "code",
"TextMarkTextContent": "wordCount"
},
{
"Type": "NodeText",
"Data": "​:该函数用于返回字符串中的字数"
} }
] ]
} }

View File

@ -6,7 +6,7 @@
"id": "20211226123004-dplpw0o", "id": "20211226123004-dplpw0o",
"title": "範本片段", "title": "範本片段",
"type": "doc", "type": "doc",
"updated": "20241224162821" "updated": "20241225192757"
}, },
"Children": [ "Children": [
{ {
@ -53,7 +53,7 @@
"HeadingLevel": 2, "HeadingLevel": 2,
"Properties": { "Properties": {
"id": "20211226123024-eyagqur", "id": "20211226123024-eyagqur",
"updated": "20241224162821" "updated": "20241225192757"
}, },
"Children": [ "Children": [
{ {
@ -208,7 +208,7 @@
"ListData": {}, "ListData": {},
"Properties": { "Properties": {
"id": "20211226123024-pjvw31z", "id": "20211226123024-pjvw31z",
"updated": "20241224162821" "updated": "20241225192757"
}, },
"Children": [ "Children": [
{ {
@ -436,7 +436,15 @@
}, },
{ {
"Type": "NodeText", "Type": "NodeText",
"Data": "​:該函數用於查詢資料庫,返回值為 blocks 列表" "Data": "​:該函數用於查詢"
},
{
"Type": "NodeText",
"Data": "資料庫"
},
{
"Type": "NodeText",
"Data": ",返回值為 blocks 列表"
} }
] ]
}, },
@ -446,6 +454,7 @@
"IsFencedCodeBlock": true, "IsFencedCodeBlock": true,
"Properties": { "Properties": {
"id": "20241213215519-mfgruqs", "id": "20241213215519-mfgruqs",
"style": "line-height: 22px;",
"updated": "20241213215519" "updated": "20241213215519"
}, },
"Children": [ "Children": [
@ -499,7 +508,15 @@
}, },
{ {
"Type": "NodeText", "Type": "NodeText",
"Data": "​:該函數用於查詢資料庫,返回值為 spans 列表" "Data": "​:該函數用於查詢"
},
{
"Type": "NodeText",
"Data": "資料庫"
},
{
"Type": "NodeText",
"Data": ",返回值為 spans 列表"
} }
] ]
}, },
@ -509,6 +526,7 @@
"IsFencedCodeBlock": true, "IsFencedCodeBlock": true,
"Properties": { "Properties": {
"id": "20241213215524-9an3khb", "id": "20241213215524-9an3khb",
"style": "line-height: 22px;",
"updated": "20241224162821" "updated": "20241224162821"
}, },
"Children": [ "Children": [
@ -562,7 +580,15 @@
}, },
{ {
"Type": "NodeText", "Type": "NodeText",
"Data": "​:函數用於查詢資料庫,傳回值為結果集" "Data": "​:函數用於查詢"
},
{
"Type": "NodeText",
"Data": "資料庫"
},
{
"Type": "NodeText",
"Data": ",傳回值為結果集"
} }
] ]
}, },
@ -572,6 +598,7 @@
"IsFencedCodeBlock": true, "IsFencedCodeBlock": true,
"Properties": { "Properties": {
"id": "20241213215530-f2iwrjz", "id": "20241213215530-f2iwrjz",
"style": "line-height: 22px;",
"updated": "20241213215530" "updated": "20241213215530"
}, },
"Children": [ "Children": [
@ -635,6 +662,7 @@
"IsFencedCodeBlock": true, "IsFencedCodeBlock": true,
"Properties": { "Properties": {
"id": "20241213215707-lxpmd6b", "id": "20241213215707-lxpmd6b",
"style": "line-height: 22px;",
"updated": "20241213215707" "updated": "20241213215707"
}, },
"Children": [ "Children": [
@ -845,7 +873,7 @@
}, },
"Properties": { "Properties": {
"id": "20241212164016-nxteno5", "id": "20241212164016-nxteno5",
"updated": "20241212164017" "updated": "20241225192757"
}, },
"Children": [ "Children": [
{ {
@ -853,7 +881,7 @@
"Type": "NodeParagraph", "Type": "NodeParagraph",
"Properties": { "Properties": {
"id": "20241212164016-ot8vo9l", "id": "20241212164016-ot8vo9l",
"updated": "20241212164017" "updated": "20241225192757"
}, },
"Children": [ "Children": [
{ {
@ -863,11 +891,48 @@
{ {
"Type": "NodeTextMark", "Type": "NodeTextMark",
"TextMarkType": "code", "TextMarkType": "code",
"TextMarkTextContent": "runeLen" "TextMarkTextContent": "runeCount"
}, },
{ {
"Type": "NodeText", "Type": "NodeText",
"Data": "​:此函數用於傳回字串長度" "Data": "​:該函數用於傳回字符串中的字符數"
}
]
}
]
},
{
"ID": "20241225192712-lunq7os",
"Type": "NodeListItem",
"ListData": {
"BulletChar": 42,
"Marker": "Kg=="
},
"Properties": {
"id": "20241225192712-lunq7os",
"updated": "20241225192723"
},
"Children": [
{
"ID": "20241225192712-sk60mxv",
"Type": "NodeParagraph",
"Properties": {
"id": "20241225192712-sk60mxv",
"updated": "20241225192723"
},
"Children": [
{
"Type": "NodeText",
"Data": ""
},
{
"Type": "NodeTextMark",
"TextMarkType": "code",
"TextMarkTextContent": "wordCount"
},
{
"Type": "NodeText",
"Data": "​:該函數用於傳回字符串中的字數"
} }
] ]
} }

View File

@ -7,7 +7,7 @@
"id": "20240530101000-6x9ivi7", "id": "20240530101000-6x9ivi7",
"title": "テンプレートスニペット", "title": "テンプレートスニペット",
"type": "doc", "type": "doc",
"updated": "20241224162728" "updated": "20241225192831"
}, },
"Children": [ "Children": [
{ {
@ -66,7 +66,7 @@
"Properties": { "Properties": {
"ID": "20240530101000-5nvn9ad", "ID": "20240530101000-5nvn9ad",
"id": "20240530101000-pi05yzt", "id": "20240530101000-pi05yzt",
"updated": "20241224162728" "updated": "20241225192831"
}, },
"Children": [ "Children": [
{ {
@ -226,7 +226,7 @@
"ListData": {}, "ListData": {},
"Properties": { "Properties": {
"id": "20240530101000-by5hqnb", "id": "20240530101000-by5hqnb",
"updated": "20241224162728" "updated": "20241225192831"
}, },
"Children": [ "Children": [
{ {
@ -454,7 +454,15 @@
}, },
{ {
"Type": "NodeText", "Type": "NodeText",
"Data": ": この関数はデータベースをクエリしてブロックのリストを返します" "Data": ": この関数は"
},
{
"Type": "NodeText",
"Data": "データベース"
},
{
"Type": "NodeText",
"Data": "をクエリしてブロックのリストを返します"
} }
] ]
}, },
@ -464,6 +472,7 @@
"IsFencedCodeBlock": true, "IsFencedCodeBlock": true,
"Properties": { "Properties": {
"id": "20241213215628-5gixpmp", "id": "20241213215628-5gixpmp",
"style": "line-height: 22px;",
"updated": "20241213215628" "updated": "20241213215628"
}, },
"Children": [ "Children": [
@ -517,7 +526,15 @@
}, },
{ {
"Type": "NodeText", "Type": "NodeText",
"Data": ": この関数はデータベースをクエリしてスパンのリストを返します" "Data": ": この関数は"
},
{
"Type": "NodeText",
"Data": "データベース"
},
{
"Type": "NodeText",
"Data": "をクエリしてスパンのリストを返します"
} }
] ]
}, },
@ -527,6 +544,7 @@
"IsFencedCodeBlock": true, "IsFencedCodeBlock": true,
"Properties": { "Properties": {
"id": "20241213215619-gkqs282", "id": "20241213215619-gkqs282",
"style": "line-height: 22px;",
"updated": "20241224162728" "updated": "20241224162728"
}, },
"Children": [ "Children": [
@ -580,7 +598,15 @@
}, },
{ {
"Type": "NodeText", "Type": "NodeText",
"Data": ": この関数はデータベースへのクエリに使用され、戻り値は結果セットです" "Data": ": この関数は"
},
{
"Type": "NodeText",
"Data": "データベース"
},
{
"Type": "NodeText",
"Data": "へのクエリに使用され、戻り値は結果セットです"
} }
] ]
}, },
@ -590,6 +616,7 @@
"IsFencedCodeBlock": true, "IsFencedCodeBlock": true,
"Properties": { "Properties": {
"id": "20241213215633-69ltmv1", "id": "20241213215633-69ltmv1",
"style": "line-height: 22px;",
"updated": "20241213215633" "updated": "20241213215633"
}, },
"Children": [ "Children": [
@ -653,6 +680,7 @@
"IsFencedCodeBlock": true, "IsFencedCodeBlock": true,
"Properties": { "Properties": {
"id": "20241213215720-ueqpsna", "id": "20241213215720-ueqpsna",
"style": "line-height: 22px;",
"updated": "20241213215720" "updated": "20241213215720"
}, },
"Children": [ "Children": [
@ -871,7 +899,7 @@
"Type": "NodeParagraph", "Type": "NodeParagraph",
"Properties": { "Properties": {
"id": "20241212163938-y1al3b9", "id": "20241212163938-y1al3b9",
"updated": "20241212163955" "updated": "20241225192824"
}, },
"Children": [ "Children": [
{ {
@ -881,11 +909,48 @@
{ {
"Type": "NodeTextMark", "Type": "NodeTextMark",
"TextMarkType": "code", "TextMarkType": "code",
"TextMarkTextContent": "runeLen" "TextMarkTextContent": "runeCount"
}, },
{ {
"Type": "NodeText", "Type": "NodeText",
"Data": ": この関数は文字列の長さを返すために使用されます" "Data": ": この関数は文字列内の文字数を返すために使用されます"
}
]
}
]
},
{
"ID": "20241225192825-vi77q0u",
"Type": "NodeListItem",
"ListData": {
"BulletChar": 42,
"Marker": "Kg=="
},
"Properties": {
"id": "20241225192825-vi77q0u",
"updated": "20241225192831"
},
"Children": [
{
"ID": "20241225192825-uicespl",
"Type": "NodeParagraph",
"Properties": {
"id": "20241225192825-uicespl",
"updated": "20241225192831"
},
"Children": [
{
"Type": "NodeText",
"Data": ""
},
{
"Type": "NodeTextMark",
"TextMarkType": "code",
"TextMarkTextContent": "wordCount"
},
{
"Type": "NodeText",
"Data": "​:この関数は文字列内の単語数を返すために使用されます"
} }
] ]
} }

View File

@ -20,9 +20,9 @@ import (
"math" "math"
"text/template" "text/template"
"time" "time"
"unicode/utf8"
"github.com/88250/go-humanize" "github.com/88250/go-humanize"
util2 "github.com/88250/lute/util"
"github.com/Masterminds/sprig/v3" "github.com/Masterminds/sprig/v3"
"github.com/araddon/dateparse" "github.com/araddon/dateparse"
"github.com/siyuan-note/logging" "github.com/siyuan-note/logging"
@ -51,12 +51,19 @@ func BuiltInTemplateFuncs() (ret template.FuncMap) {
ret["FormatFloat"] = FormatFloat ret["FormatFloat"] = FormatFloat
ret["getHPathByID"] = getHPathByID ret["getHPathByID"] = getHPathByID
ret["statBlock"] = StatBlock ret["statBlock"] = StatBlock
ret["runeLen"] = runeLen ret["runeCount"] = runeCount
ret["wordCount"] = wordCount
return return
} }
func runeLen(s string) int { func runeCount(s string) (ret int) {
return utf8.RuneCountInString(s) ret, _ = util2.WordCount(s)
return
}
func wordCount(s string) (ret int) {
_, ret = util2.WordCount(s)
return
} }
func pow(a, b interface{}) int64 { return int64(math.Pow(cast.ToFloat64(a), cast.ToFloat64(b))) } func pow(a, b interface{}) int64 { return int64(math.Pow(cast.ToFloat64(a), cast.ToFloat64(b))) }