Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Fix escaped block ref text in search results https://github.com/siyuan-note/siyuan/issues/18229

Daniel3a4b575759ed
diff --git a/kernel/sql/block.go b/kernel/sql/block.go
index cab51a3ac7..1af0cb7d1f 100644
--- a/kernel/sql/block.go
+++ b/kernel/sql/block.go
@@ -170,7 +170,7 @@ func indexNode(tx *sql.Tx, id, boxID string) (err error) {
return
}
- content := NodeStaticContent(node, nil, true, indexAssetPath, true)
+ content := nodeStaticContent(node, nil, true, indexAssetPath, true, true)
content = strings.ReplaceAll(content, editor.Zwsp, "")
var rowID int64
if rowID, err = blockRowIDByBlockID(tx, id); err != nil {
@@ -191,6 +191,10 @@ func indexNode(tx *sql.Tx, id, boxID string) (err error) {
}
func NodeStaticContent(node *ast.Node, excludeTypes []string, includeTextMarkATitleURL, includeAssetPath, fullAttrView bool) string {
+ return nodeStaticContent(node, excludeTypes, includeTextMarkATitleURL, includeAssetPath, fullAttrView, false)
+}
+
+func nodeStaticContent(node *ast.Node, excludeTypes []string, includeTextMarkATitleURL, includeAssetPath, fullAttrView, unescapeBlockRef bool) string {
if nil == node {
return ""
}
@@ -328,7 +332,11 @@ func NodeStaticContent(node *ast.Node, excludeTypes []string, includeTextMarkATi
if n.IsTextMarkType("tag") {
buf.WriteByte('#')
}
- buf.WriteString(n.Content())
+ content := n.Content()
+ if unescapeBlockRef && treenode.IsBlockRef(n) {
+ content = util.UnescapeHTML(content)
+ }
+ buf.WriteString(content)
if n.IsTextMarkType("tag") {
buf.WriteByte('#')
}
diff --git a/kernel/sql/block_test.go b/kernel/sql/block_test.go
new file mode 100644
index 0000000000..4f8d444afa
--- /dev/null
+++ b/kernel/sql/block_test.go
@@ -0,0 +1,44 @@
+// SiYuan - Refactor your thinking
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package sql
+
+import (
+ "testing"
+
+ "github.com/88250/lute/ast"
+)
+
+func TestNodeStaticContentUnescapesBlockRefText(t *testing.T) {
+ paragraph := &ast.Node{Type: ast.NodeParagraph}
+ paragraph.AppendChild(&ast.Node{
+ Type: ast.NodeTextMark,
+ TextMarkType: "block-ref",
+ TextMarkTextContent: "123foo&amp;bar",
+ TextMarkBlockRefID: "20260714120000-abcdefg",
+ TextMarkBlockRefSubtype: "s",
+ })
+
+ content := nodeStaticContent(paragraph, nil, true, false, true, true)
+ if "123foo&bar" != content {
+ t.Fatalf("unexpected static content: %q", content)
+ }
+
+ content = NodeStaticContent(paragraph, nil, true, false, true)
+ if "123foo&amp;bar" != content {
+ t.Fatalf("unexpected generic static content: %q", content)
+ }
+}
diff --git a/kernel/sql/database.go b/kernel/sql/database.go
index 19b59bd857..5b81158e43 100644
--- a/kernel/sql/database.go
+++ b/kernel/sql/database.go
@@ -935,13 +935,13 @@ func buildBlockFromNode(n *ast.Node, tree *parse.Tree) (block *Block, attributes
if !treenode.IsNodeOCRed(n) {
util.PushNodeOCRQueue(n)
}
- content = NodeStaticContent(n, nil, true, indexAssetPath, true)
+ content = nodeStaticContent(n, nil, true, indexAssetPath, true, true)
fc := treenode.FirstLeafBlock(n)
if !treenode.IsNodeOCRed(fc) {
util.PushNodeOCRQueue(fc)
}
- fcontent = NodeStaticContent(fc, nil, true, false, true)
+ fcontent = nodeStaticContent(fc, nil, true, false, true, true)
parentID = n.Parent.ID
if h := treenode.HeadingParent(n); nil != h { // 如果在标题块下方,则将标题块作为父节点
@@ -953,7 +953,7 @@ func buildBlockFromNode(n *ast.Node, tree *parse.Tree) (block *Block, attributes
if !treenode.IsNodeOCRed(n) {
util.PushNodeOCRQueue(n)
}
- content = NodeStaticContent(n, nil, true, indexAssetPath, true)
+ content = nodeStaticContent(n, nil, true, indexAssetPath, true, true)
parentID = n.Parent.ID
if h := treenode.HeadingParent(n); nil != h {