From 536aa39f9529d50cc70830b5b3ebc8d465fae440 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Mon, 31 Mar 2025 16:39:24 +0800 Subject: [PATCH] :art: Add template function `getBlock` https://github.com/siyuan-note/siyuan/issues/14474 --- .../20201204184532-3qm9l8n.sy | 115 +++++++++++------- .../20201204181006-7bkppue.sy | 115 +++++++++++------- .../20211226123004-dplpw0o.sy | 105 +++++++++++----- .../20240530101000-6x9ivi7.sy | 105 +++++++++++----- kernel/sql/database.go | 11 ++ 5 files changed, 301 insertions(+), 150 deletions(-) diff --git a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20201204184532-3qm9l8n.sy b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20201204184532-3qm9l8n.sy index bdc706d45..62ef36b7a 100644 --- a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20201204184532-3qm9l8n.sy +++ b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20201204184532-3qm9l8n.sy @@ -6,7 +6,7 @@ "id": "20201204184532-3qm9l8n", "title": "Template snippet", "type": "doc", - "updated": "20250115153127" + "updated": "20250331163727" }, "Children": [ { @@ -57,7 +57,7 @@ "HeadingLevel": 2, "Properties": { "id": "20210104091444-jy56z0p", - "updated": "20241225192808" + "updated": "20250331163727" }, "Children": [ { @@ -215,7 +215,7 @@ "ListData": {}, "Properties": { "id": "20210104091444-mwbvc9m", - "updated": "20241225192808" + "updated": "20250331163727" }, "Children": [ { @@ -249,15 +249,7 @@ }, { "Type": "NodeText", - "Data": "​: Use this variable to insert the current document name. For example, if the " - }, - { - "Type": "NodeText", - "Data": "template" - }, - { - "Type": "NodeText", - "Data": " content is " + "Data": "​: Use this variable to insert the current document name. For example, if the template content is " }, { "Type": "NodeTextMark", @@ -429,7 +421,7 @@ }, "Properties": { "id": "20210121193829-wfzsf6m", - "updated": "20240403150956" + "updated": "20250331163727" }, "Children": [ { @@ -451,15 +443,7 @@ }, { "Type": "NodeText", - "Data": "​: This function is used to query the " - }, - { - "Type": "NodeText", - "Data": "database" - }, - { - "Type": "NodeText", - "Data": ", and the return value is a list of blocks" + "Data": "​: This function is used to query the database, and the return value is a list of blocks" } ] }, @@ -470,7 +454,7 @@ "Properties": { "id": "20241213215602-dk7iion", "style": "line-height: 22px;", - "updated": "20241213215602" + "updated": "20250331163727" }, "Children": [ { @@ -482,7 +466,70 @@ }, { "Type": "NodeCodeBlockCode", - "Data": ".action{$today := now | date \"20060102150405\"}\n.action{$blocks :=queryBlocks \"SELECT * FROM blocks WHERE content LIKE '?' AND updated \u003e '?' LIMIT ?\" \"%foo%\" $today \"3\"}\n" + "Data": ".action{$today := now | date \"20060102150405\"}\n.action{$blocks := queryBlocks \"SELECT * FROM blocks WHERE content LIKE '?' AND updated \u003e '?' LIMIT ?\" \"%foo%\" $today \"3\"}\n" + }, + { + "Type": "NodeCodeBlockFenceCloseMarker", + "Data": "```" + } + ] + } + ] + }, + { + "ID": "20250331163529-vixv1we", + "Type": "NodeListItem", + "ListData": { + "BulletChar": 42, + "Marker": "Kg==" + }, + "Properties": { + "id": "20250331163529-vixv1we", + "updated": "20250331163604" + }, + "Children": [ + { + "ID": "20250331163529-m6ezl4h", + "Type": "NodeParagraph", + "Properties": { + "id": "20250331163529-m6ezl4h", + "updated": "20250331163547" + }, + "Children": [ + { + "Type": "NodeText", + "Data": "​" + }, + { + "Type": "NodeTextMark", + "TextMarkType": "code", + "TextMarkTextContent": "getBlock" + }, + { + "Type": "NodeText", + "Data": "​: This function is used to query the database based on the block ID, and the return value is block" + } + ] + }, + { + "ID": "20250331163550-2r69a54", + "Type": "NodeCodeBlock", + "IsFencedCodeBlock": true, + "Properties": { + "id": "20250331163550-2r69a54", + "updated": "20250331163604" + }, + "Children": [ + { + "Type": "NodeCodeBlockFenceOpenMarker", + "Data": "```" + }, + { + "Type": "NodeCodeBlockFenceInfoMarker" + }, + { + "Type": "NodeCodeBlockCode", + "Data": ".action{getBlock \"20250331163529-vixv1we\"}\n" }, { "Type": "NodeCodeBlockFenceCloseMarker", @@ -523,15 +570,7 @@ }, { "Type": "NodeText", - "Data": "​: This function is used to query the " - }, - { - "Type": "NodeText", - "Data": "database" - }, - { - "Type": "NodeText", - "Data": ", and the return value is a list of spans" + "Data": "​: This function is used to query the database, and the return value is a list of spans" } ] }, @@ -595,15 +634,7 @@ }, { "Type": "NodeText", - "Data": "​: This function is used to query the " - }, - { - "Type": "NodeText", - "Data": "database" - }, - { - "Type": "NodeText", - "Data": " and the return value is a result set" + "Data": "​: This function is used to query the database and the return value is a result set" } ] }, diff --git a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20201204181006-7bkppue.sy b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20201204181006-7bkppue.sy index 29f96a134..80d697553 100644 --- a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20201204181006-7bkppue.sy +++ b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20201204181006-7bkppue.sy @@ -6,7 +6,7 @@ "id": "20201204181006-7bkppue", "title": "模板片段", "type": "doc", - "updated": "20250115153028" + "updated": "20250331163228" }, "Children": [ { @@ -69,7 +69,7 @@ "HeadingLevel": 2, "Properties": { "id": "20210104091309-fhb549c", - "updated": "20241225192538" + "updated": "20250331163228" }, "Children": [ { @@ -237,7 +237,7 @@ "ListData": {}, "Properties": { "id": "20210104091309-gjkg3u5", - "updated": "20241225192538" + "updated": "20250331163228" }, "Children": [ { @@ -271,15 +271,7 @@ }, { "Type": "NodeText", - "Data": "​:该变量用于插入当前文档名。比如" - }, - { - "Type": "NodeText", - "Data": "模板" - }, - { - "Type": "NodeText", - "Data": "内容为 " + "Data": "​:该变量用于插入当前文档名。比如模板内容为 " }, { "Type": "NodeTextMark", @@ -451,7 +443,7 @@ }, "Properties": { "id": "20210121193601-uwo8s5h", - "updated": "20240403150712" + "updated": "20250331163052" }, "Children": [ { @@ -473,15 +465,7 @@ }, { "Type": "NodeText", - "Data": "​:该函数用于查询" - }, - { - "Type": "NodeText", - "Data": "数据库" - }, - { - "Type": "NodeText", - "Data": ",返回值为 blocks 列表" + "Data": "​:该函数用于查询数据库,返回值为 blocks 列表" } ] }, @@ -492,7 +476,7 @@ "Properties": { "id": "20210604111446-p6vadfc", "style": "line-height: 22px;", - "updated": "20241213215118" + "updated": "20250331163052" }, "Children": [ { @@ -504,7 +488,70 @@ }, { "Type": "NodeCodeBlockCode", - "Data": ".action{$today := now | date \"20060102150405\"}\n.action{$blocks :=queryBlocks \"SELECT * FROM blocks WHERE content LIKE '?' AND updated \u003e '?' LIMIT ?\" \"%foo%\" $today \"3\"}\n" + "Data": ".action{$today := now | date \"20060102150405\"}\n.action{$blocks := queryBlocks \"SELECT * FROM blocks WHERE content LIKE '?' AND updated \u003e '?' LIMIT ?\" \"%foo%\" $today \"3\"}\n" + }, + { + "Type": "NodeCodeBlockFenceCloseMarker", + "Data": "```" + } + ] + } + ] + }, + { + "ID": "20250331162928-53comqi", + "Type": "NodeListItem", + "ListData": { + "BulletChar": 42, + "Marker": "Kg==" + }, + "Properties": { + "id": "20250331162928-53comqi", + "updated": "20250331163228" + }, + "Children": [ + { + "ID": "20250331162928-8ugpm3u", + "Type": "NodeParagraph", + "Properties": { + "id": "20250331162928-8ugpm3u", + "updated": "20250331163210" + }, + "Children": [ + { + "Type": "NodeText", + "Data": "​" + }, + { + "Type": "NodeTextMark", + "TextMarkType": "code", + "TextMarkTextContent": "getBlock" + }, + { + "Type": "NodeText", + "Data": "​:该函数用于根据块 ID 查询数据库,返回值为 block" + } + ] + }, + { + "ID": "20250331163038-6j2ttlc", + "Type": "NodeCodeBlock", + "IsFencedCodeBlock": true, + "Properties": { + "id": "20250331163038-6j2ttlc", + "updated": "20250331163228" + }, + "Children": [ + { + "Type": "NodeCodeBlockFenceOpenMarker", + "Data": "```" + }, + { + "Type": "NodeCodeBlockFenceInfoMarker" + }, + { + "Type": "NodeCodeBlockCode", + "Data": ".action{getBlock \"20250331162928-53comqi\"}\n" }, { "Type": "NodeCodeBlockFenceCloseMarker", @@ -545,15 +592,7 @@ }, { "Type": "NodeText", - "Data": "​:该函数用于查询" - }, - { - "Type": "NodeText", - "Data": "数据库" - }, - { - "Type": "NodeText", - "Data": ",返回值为 spans 列表" + "Data": "​:该函数用于查询数据库,返回值为 spans 列表" } ] }, @@ -617,15 +656,7 @@ }, { "Type": "NodeText", - "Data": "​:该函数用于查询" - }, - { - "Type": "NodeText", - "Data": "数据库" - }, - { - "Type": "NodeText", - "Data": ",返回值为结果集" + "Data": "​:该函数用于查询数据库,返回值为结果集" } ] }, diff --git a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226123004-dplpw0o.sy b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226123004-dplpw0o.sy index 4059649c2..d9473e141 100644 --- a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226123004-dplpw0o.sy +++ b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20211226123004-dplpw0o.sy @@ -6,7 +6,7 @@ "id": "20211226123004-dplpw0o", "title": "範本片段", "type": "doc", - "updated": "20250115153115" + "updated": "20250331163725" }, "Children": [ { @@ -53,7 +53,7 @@ "HeadingLevel": 2, "Properties": { "id": "20211226123024-eyagqur", - "updated": "20241225192757" + "updated": "20250331163725" }, "Children": [ { @@ -208,7 +208,7 @@ "ListData": {}, "Properties": { "id": "20211226123024-pjvw31z", - "updated": "20241225192757" + "updated": "20250331163725" }, "Children": [ { @@ -414,7 +414,7 @@ }, "Properties": { "id": "20211226123024-e42d7xq", - "updated": "20240403150942" + "updated": "20250331163725" }, "Children": [ { @@ -436,15 +436,7 @@ }, { "Type": "NodeText", - "Data": "​:該函數用於查詢" - }, - { - "Type": "NodeText", - "Data": "資料庫" - }, - { - "Type": "NodeText", - "Data": ",返回值為 blocks 列表" + "Data": "​:該函數用於查詢資料庫,返回值為 blocks 列表" } ] }, @@ -455,7 +447,7 @@ "Properties": { "id": "20241213215519-mfgruqs", "style": "line-height: 22px;", - "updated": "20241213215519" + "updated": "20250331163725" }, "Children": [ { @@ -467,7 +459,70 @@ }, { "Type": "NodeCodeBlockCode", - "Data": ".action{$today := now | date \"20060102150405\"}\n.action{$blocks :=queryBlocks \"SELECT * FROM blocks WHERE content LIKE '?' AND updated \u003e '?' LIMIT ?\" \"%foo%\" $today \"3\"}\n" + "Data": ".action{$today := now | date \"20060102150405\"}\n.action{$blocks := queryBlocks \"SELECT * FROM blocks WHERE content LIKE '?' AND updated \u003e '?' LIMIT ?\" \"%foo%\" $today \"3\"}\n" + }, + { + "Type": "NodeCodeBlockFenceCloseMarker", + "Data": "```" + } + ] + } + ] + }, + { + "ID": "20250331163425-s5u08ng", + "Type": "NodeListItem", + "ListData": { + "BulletChar": 42, + "Marker": "Kg==" + }, + "Properties": { + "id": "20250331163425-s5u08ng", + "updated": "20250331163515" + }, + "Children": [ + { + "ID": "20250331163425-6fe2kvm", + "Type": "NodeParagraph", + "Properties": { + "id": "20250331163425-6fe2kvm", + "updated": "20250331163515" + }, + "Children": [ + { + "Type": "NodeText", + "Data": "​" + }, + { + "Type": "NodeTextMark", + "TextMarkType": "code", + "TextMarkTextContent": "getBlock" + }, + { + "Type": "NodeText", + "Data": "​:函數用於根據塊 ID 查詢資料庫,傳回值為 block" + } + ] + }, + { + "ID": "20250331163435-irjmxt9", + "Type": "NodeCodeBlock", + "IsFencedCodeBlock": true, + "Properties": { + "id": "20250331163435-irjmxt9", + "updated": "20250331163452" + }, + "Children": [ + { + "Type": "NodeCodeBlockFenceOpenMarker", + "Data": "```" + }, + { + "Type": "NodeCodeBlockFenceInfoMarker" + }, + { + "Type": "NodeCodeBlockCode", + "Data": ".action{getBlock \"20250331163425-s5u08ng\"}\n" }, { "Type": "NodeCodeBlockFenceCloseMarker", @@ -508,15 +563,7 @@ }, { "Type": "NodeText", - "Data": "​:該函數用於查詢" - }, - { - "Type": "NodeText", - "Data": "資料庫" - }, - { - "Type": "NodeText", - "Data": ",返回值為 spans 列表" + "Data": "​:該函數用於查詢資料庫,返回值為 spans 列表" } ] }, @@ -580,15 +627,7 @@ }, { "Type": "NodeText", - "Data": "​:函數用於查詢" - }, - { - "Type": "NodeText", - "Data": "資料庫" - }, - { - "Type": "NodeText", - "Data": ",傳回值為結果集" + "Data": "​:函數用於查詢資料庫,傳回值為結果集" } ] }, diff --git a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-6x9ivi7.sy b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-6x9ivi7.sy index 4c76f248c..0b6cc40c2 100644 --- a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-6x9ivi7.sy +++ b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-6x9ivi7.sy @@ -7,7 +7,7 @@ "id": "20240530101000-6x9ivi7", "title": "テンプレートスニペット", "type": "doc", - "updated": "20241225192831" + "updated": "20250331163729" }, "Children": [ { @@ -66,7 +66,7 @@ "Properties": { "ID": "20240530101000-5nvn9ad", "id": "20240530101000-pi05yzt", - "updated": "20241225192831" + "updated": "20250331163729" }, "Children": [ { @@ -226,7 +226,7 @@ "ListData": {}, "Properties": { "id": "20240530101000-by5hqnb", - "updated": "20241225192831" + "updated": "20250331163729" }, "Children": [ { @@ -432,7 +432,7 @@ }, "Properties": { "id": "20240530101000-0y2zlji", - "updated": "20240530101000" + "updated": "20250331163729" }, "Children": [ { @@ -454,15 +454,7 @@ }, { "Type": "NodeText", - "Data": "​: この関数は" - }, - { - "Type": "NodeText", - "Data": "データベース" - }, - { - "Type": "NodeText", - "Data": "をクエリしてブロックのリストを返します" + "Data": "​: この関数はデータベースをクエリしてブロックのリストを返します" } ] }, @@ -473,7 +465,7 @@ "Properties": { "id": "20241213215628-5gixpmp", "style": "line-height: 22px;", - "updated": "20241213215628" + "updated": "20250331163729" }, "Children": [ { @@ -485,7 +477,70 @@ }, { "Type": "NodeCodeBlockCode", - "Data": ".action{$today := now | date \"20060102150405\"}\n.action{$blocks :=queryBlocks \"SELECT * FROM blocks WHERE content LIKE '?' AND updated \u003e '?' LIMIT ?\" \"%foo%\" $today \"3\"}\n" + "Data": ".action{$today := now | date \"20060102150405\"}\n.action{$blocks := queryBlocks \"SELECT * FROM blocks WHERE content LIKE '?' AND updated \u003e '?' LIMIT ?\" \"%foo%\" $today \"3\"}\n" + }, + { + "Type": "NodeCodeBlockFenceCloseMarker", + "Data": "```" + } + ] + } + ] + }, + { + "ID": "20250331163610-n3e9zft", + "Type": "NodeListItem", + "ListData": { + "BulletChar": 42, + "Marker": "Kg==" + }, + "Properties": { + "id": "20250331163610-n3e9zft", + "updated": "20250331163642" + }, + "Children": [ + { + "ID": "20250331163610-nbmwe90", + "Type": "NodeParagraph", + "Properties": { + "id": "20250331163610-nbmwe90", + "updated": "20250331163621" + }, + "Children": [ + { + "Type": "NodeText", + "Data": "​" + }, + { + "Type": "NodeTextMark", + "TextMarkType": "code", + "TextMarkTextContent": "getBlock" + }, + { + "Type": "NodeText", + "Data": "​: この関数はブロックIDに基づいてデータベースを照会するために使用され、戻り値はブロックです" + } + ] + }, + { + "ID": "20250331163621-j4iofxv", + "Type": "NodeCodeBlock", + "IsFencedCodeBlock": true, + "Properties": { + "id": "20250331163621-j4iofxv", + "updated": "20250331163642" + }, + "Children": [ + { + "Type": "NodeCodeBlockFenceOpenMarker", + "Data": "```" + }, + { + "Type": "NodeCodeBlockFenceInfoMarker" + }, + { + "Type": "NodeCodeBlockCode", + "Data": ".action{getBlock \"20250331163610-n3e9zft\"}\n" }, { "Type": "NodeCodeBlockFenceCloseMarker", @@ -526,15 +581,7 @@ }, { "Type": "NodeText", - "Data": "​: この関数は" - }, - { - "Type": "NodeText", - "Data": "データベース" - }, - { - "Type": "NodeText", - "Data": "をクエリしてスパンのリストを返します" + "Data": "​: この関数はデータベースをクエリしてスパンのリストを返します" } ] }, @@ -598,15 +645,7 @@ }, { "Type": "NodeText", - "Data": "​: この関数は" - }, - { - "Type": "NodeText", - "Data": "データベース" - }, - { - "Type": "NodeText", - "Data": "へのクエリに使用され、戻り値は結果セットです" + "Data": "​: この関数はデータベースへのクエリに使用され、戻り値は結果セットです" } ] }, diff --git a/kernel/sql/database.go b/kernel/sql/database.go index 3325366db..c14c6568e 100644 --- a/kernel/sql/database.go +++ b/kernel/sql/database.go @@ -1518,6 +1518,17 @@ func SQLTemplateFuncs(templateFuncMap *template.FuncMap) { retBlocks = SelectBlocksRawStmt(stmt, 1, 512) return } + (*templateFuncMap)["getBlock"] = func(arg any) (retBlock *Block) { + switch v := arg.(type) { + case string: + retBlock = GetBlock(v) + case map[string]interface{}: + if id, ok := v["id"]; ok { + retBlock = GetBlock(id.(string)) + } + } + return + } (*templateFuncMap)["querySpans"] = func(stmt string, args ...string) (retSpans []*Span) { for _, arg := range args { stmt = strings.Replace(stmt, "?", arg, 1)