From ea0146ceb96d0467da41a6e9d3f6343ed0f96180 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sat, 16 Nov 2024 00:17:18 +0800 Subject: [PATCH] :art: Improve av block text https://github.com/siyuan-note/siyuan/issues/13140 --- kernel/model/attribute_view.go | 6 +++--- kernel/model/blockinfo.go | 21 +++++++++++++++++---- kernel/model/heading.go | 2 +- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index 236cdec9a65..ac5ac113a55 100644 --- a/kernel/model/attribute_view.go +++ b/kernel/model/attribute_view.go @@ -1981,7 +1981,7 @@ func addAttributeViewBlock(now int64, avID, blockID, previousBlockID, addingBloc } if !isDetached { - addingBlockContent = getNodeRefText(node) + addingBlockContent = getNodeAvBlockText(node) } // 检查是否重复添加相同的块 @@ -2936,7 +2936,7 @@ func replaceAttributeViewBlock(operation *Operation, tx *Transaction) (err error if !operation.IsDetached { bindBlockAv0(tx, operation.AvID, node, tree) value.IsDetached = false - value.Block.Content = getNodeRefText(node) + value.Block.Content = getNodeAvBlockText(node) value.UpdatedAt = now err = av.SaveAttributeView(attrView) } @@ -2973,7 +2973,7 @@ func replaceAttributeViewBlock(operation *Operation, tx *Transaction) (err error value.Block.ID = operation.NextID value.IsDetached = operation.IsDetached if !operation.IsDetached { - value.Block.Content = getNodeRefText(node) + value.Block.Content = getNodeAvBlockText(node) } } diff --git a/kernel/model/blockinfo.go b/kernel/model/blockinfo.go index 07422d968b0..c1db275a9e5 100644 --- a/kernel/model/blockinfo.go +++ b/kernel/model/blockinfo.go @@ -262,10 +262,23 @@ func getNodeRefText(node *ast.Node) string { ret = util.EscapeHTML(ret) return ret } - return getNodeRefText0(node) + return getNodeRefText0(node, Conf.Editor.BlockRefDynamicAnchorTextMaxLen) } -func getNodeRefText0(node *ast.Node) string { +func getNodeAvBlockText(node *ast.Node) string { + if nil == node { + return "" + } + + if ret := node.IALAttr("name"); "" != ret { + ret = strings.TrimSpace(ret) + ret = util.EscapeHTML(ret) + return ret + } + return getNodeRefText0(node, 1024) +} + +func getNodeRefText0(node *ast.Node, maxLen int) string { switch node.Type { case ast.NodeBlockQueryEmbed: return "Query Embed Block..." @@ -289,8 +302,8 @@ func getNodeRefText0(node *ast.Node) string { node = treenode.FirstLeafBlock(node) } ret := renderBlockText(node, nil) - if Conf.Editor.BlockRefDynamicAnchorTextMaxLen < utf8.RuneCountInString(ret) { - ret = gulu.Str.SubStr(ret, Conf.Editor.BlockRefDynamicAnchorTextMaxLen) + "..." + if maxLen < utf8.RuneCountInString(ret) { + ret = gulu.Str.SubStr(ret, maxLen) + "..." } return ret } diff --git a/kernel/model/heading.go b/kernel/model/heading.go index 4f96b537f56..c5e8d90eb07 100644 --- a/kernel/model/heading.go +++ b/kernel/model/heading.go @@ -296,7 +296,7 @@ func Heading2Doc(srcHeadingID, targetBoxID, targetPath string) (srcRootBlockID, } box := Conf.Box(targetBoxID) - headingText := getNodeRefText0(headingNode) + headingText := getNodeRefText0(headingNode, Conf.Editor.BlockRefDynamicAnchorTextMaxLen) if strings.Contains(headingText, "/") { headingText = strings.ReplaceAll(headingText, "/", "_") util.PushMsg(Conf.language(246), 7000)