🎨 Database primary key anchor text supports line breaks https://github.com/siyuan-note/siyuan/issues/13624

This commit is contained in:
Daniel 2025-01-02 11:10:16 +08:00
parent 6125603e38
commit 07cd4ec0b5
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
5 changed files with 23 additions and 13 deletions

View file

@ -232,7 +232,7 @@ func setNodeAttrs0(node *ast.Node, nameValues map[string]string) (oldAttrs map[s
} }
for name, value := range nameValues { for name, value := range nameValues {
value = util.RemoveInvalid(value) value = util.RemoveInvalidRetainCtrl(value)
value = strings.TrimSpace(value) value = strings.TrimSpace(value)
value = strings.TrimSuffix(value, ",") value = strings.TrimSuffix(value, ",")
if "" == value { if "" == value {

View file

@ -240,7 +240,7 @@ func GetBlockRefText(id string) string {
func GetDOMText(dom string) (ret string) { func GetDOMText(dom string) (ret string) {
luteEngine := NewLute() luteEngine := NewLute()
tree := luteEngine.BlockDOM2Tree(dom) tree := luteEngine.BlockDOM2Tree(dom)
ret = renderBlockText(tree.Root.FirstChild, nil) ret = renderBlockText(tree.Root.FirstChild, nil, true)
return return
} }
@ -265,7 +265,7 @@ func getNodeRefText(node *ast.Node) string {
ret = util.EscapeHTML(ret) ret = util.EscapeHTML(ret)
return ret return ret
} }
return getNodeRefText0(node, Conf.Editor.BlockRefDynamicAnchorTextMaxLen) return getNodeRefText0(node, Conf.Editor.BlockRefDynamicAnchorTextMaxLen, true)
} }
func getNodeAvBlockText(node *ast.Node) (icon, content string) { func getNodeAvBlockText(node *ast.Node) (icon, content string) {
@ -279,7 +279,7 @@ func getNodeAvBlockText(node *ast.Node) (icon, content string) {
name = util.EscapeHTML(name) name = util.EscapeHTML(name)
content = name content = name
} else { } else {
content = getNodeRefText0(node, 1024) content = getNodeRefText0(node, 1024, false)
} }
content = strings.TrimSpace(content) content = strings.TrimSpace(content)
@ -290,7 +290,7 @@ func getNodeAvBlockText(node *ast.Node) (icon, content string) {
return return
} }
func getNodeRefText0(node *ast.Node, maxLen int) string { func getNodeRefText0(node *ast.Node, maxLen int, removeLineBreak bool) string {
switch node.Type { switch node.Type {
case ast.NodeBlockQueryEmbed: case ast.NodeBlockQueryEmbed:
return "Query Embed Block..." return "Query Embed Block..."
@ -313,7 +313,7 @@ func getNodeRefText0(node *ast.Node, maxLen int) string {
if ast.NodeDocument != node.Type && node.IsContainerBlock() { if ast.NodeDocument != node.Type && node.IsContainerBlock() {
node = treenode.FirstLeafBlock(node) node = treenode.FirstLeafBlock(node)
} }
ret := renderBlockText(node, nil) ret := renderBlockText(node, nil, removeLineBreak)
if maxLen < utf8.RuneCountInString(ret) { if maxLen < utf8.RuneCountInString(ret) {
ret = gulu.Str.SubStr(ret, maxLen) + "..." ret = gulu.Str.SubStr(ret, maxLen) + "..."
} }
@ -481,9 +481,9 @@ func buildBlockBreadcrumb(node *ast.Node, excludeTypes []string, isEmbedBlock bo
} else { } else {
if "" == name { if "" == name {
if ast.NodeListItem == parent.Type || ast.NodeList == parent.Type || ast.NodeSuperBlock == parent.Type || ast.NodeBlockquote == parent.Type { if ast.NodeListItem == parent.Type || ast.NodeList == parent.Type || ast.NodeSuperBlock == parent.Type || ast.NodeBlockquote == parent.Type {
name = gulu.Str.SubStr(renderBlockText(fc, excludeTypes), maxNameLen) name = gulu.Str.SubStr(renderBlockText(fc, excludeTypes, true), maxNameLen)
} else { } else {
name = gulu.Str.SubStr(renderBlockText(parent, excludeTypes), maxNameLen) name = gulu.Str.SubStr(renderBlockText(parent, excludeTypes, true), maxNameLen)
} }
} }
if ast.NodeHeading == parent.Type { if ast.NodeHeading == parent.Type {
@ -504,7 +504,7 @@ func buildBlockBreadcrumb(node *ast.Node, excludeTypes []string, isEmbedBlock bo
} }
if ast.NodeListItem == parent.Type { if ast.NodeListItem == parent.Type {
if "" == name { if "" == name {
name = gulu.Str.SubStr(renderBlockText(fc, excludeTypes), maxNameLen) name = gulu.Str.SubStr(renderBlockText(fc, excludeTypes, true), maxNameLen)
} }
} }
@ -542,7 +542,7 @@ func buildBlockBreadcrumb(node *ast.Node, excludeTypes []string, isEmbedBlock bo
continue continue
} }
name = gulu.Str.SubStr(renderBlockText(b, excludeTypes), maxNameLen) name = gulu.Str.SubStr(renderBlockText(b, excludeTypes, true), maxNameLen)
name = util.EscapeHTML(name) name = util.EscapeHTML(name)
ret = append([]*BlockPath{{ ret = append([]*BlockPath{{
ID: b.ID, ID: b.ID,

View file

@ -296,7 +296,7 @@ func Heading2Doc(srcHeadingID, targetBoxID, targetPath, previousPath string) (sr
} }
box := Conf.Box(targetBoxID) box := Conf.Box(targetBoxID)
headingText := getNodeRefText0(headingNode, Conf.Editor.BlockRefDynamicAnchorTextMaxLen) headingText := getNodeRefText0(headingNode, Conf.Editor.BlockRefDynamicAnchorTextMaxLen, true)
if strings.Contains(headingText, "/") { if strings.Contains(headingText, "/") {
headingText = strings.ReplaceAll(headingText, "/", "_") headingText = strings.ReplaceAll(headingText, "/", "_")
util.PushMsg(Conf.language(246), 7000) util.PushMsg(Conf.language(246), 7000)

View file

@ -103,14 +103,16 @@ func renderOutline(heading *ast.Node, luteEngine *lute.Lute) (ret string) {
return return
} }
func renderBlockText(node *ast.Node, excludeTypes []string) (ret string) { func renderBlockText(node *ast.Node, excludeTypes []string, removeLineBreak bool) (ret string) {
if nil == node { if nil == node {
return return
} }
ret = sql.NodeStaticContent(node, excludeTypes, false, false, false) ret = sql.NodeStaticContent(node, excludeTypes, false, false, false)
ret = strings.TrimSpace(ret) ret = strings.TrimSpace(ret)
if removeLineBreak {
ret = strings.ReplaceAll(ret, "\n", "") ret = strings.ReplaceAll(ret, "\n", "")
}
ret = util.UnescapeHTML(ret) ret = util.UnescapeHTML(ret)
ret = util.EscapeHTML(ret) ret = util.EscapeHTML(ret)
ret = strings.TrimSpace(ret) ret = strings.TrimSpace(ret)

View file

@ -20,6 +20,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings"
"unicode" "unicode"
"github.com/88250/gulu" "github.com/88250/gulu"
@ -69,6 +70,13 @@ func RemoveInvalid(text string) (ret string) {
return return
} }
func RemoveInvalidRetainCtrl(text string) (ret string) {
ret = strings.ReplaceAll(text, "\u00A0", " ") // NBSP 转换为普通空格
ret = gulu.Str.RemoveZeroWidthCharacters(ret)
ret = gulu.Str.RemovePUA(ret)
return
}
var NativeEmojiChars = map[string]bool{} var NativeEmojiChars = map[string]bool{}
func InitEmojiChars() { func InitEmojiChars() {