Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Vanessa 2022-11-29 23:20:54 +08:00
commit 85477cc30a
5 changed files with 35 additions and 25 deletions

View File

@ -204,7 +204,7 @@ func buildBacklink(refID string, refTree *parse.Tree, mentionKeywords []string,
} }
if ast.NodeText == n.Type { if ast.NodeText == n.Type {
text := string(n.Tokens) text := string(n.Tokens)
newText := markReplaceSpanWithSplit(text, mentionKeywords, searchMarkSpanStart, searchMarkSpanEnd) newText := markReplaceSpanWithSplit(text, mentionKeywords, getMarkSpanStart(searchMarkDataType), getMarkSpanEnd())
if text == newText { if text == newText {
return ast.WalkContinue return ast.WalkContinue
} }
@ -627,7 +627,6 @@ func buildTreeBackmention(defSQLBlock *sql.Block, refBlocks []*Block, keyword st
func searchBackmention(mentionKeywords []string, keyword string, excludeBacklinkIDs *hashset.Set, rootID string, beforeLen int) (ret []*Block) { func searchBackmention(mentionKeywords []string, keyword string, excludeBacklinkIDs *hashset.Set, rootID string, beforeLen int) (ret []*Block) {
ret = []*Block{} ret = []*Block{}
if 1 > len(mentionKeywords) { if 1 > len(mentionKeywords) {
return return
} }
@ -696,7 +695,7 @@ func searchBackmention(mentionKeywords []string, keyword string, excludeBacklink
continue continue
} }
newText := markReplaceSpanWithSplit(text, mentionKeywords, searchMarkSpanStart, searchMarkSpanEnd) newText := markReplaceSpanWithSplit(text, mentionKeywords, getMarkSpanStart(searchMarkDataType), getMarkSpanEnd())
if text != newText { if text != newText {
tmp = append(tmp, b) tmp = append(tmp, b)
} }

View File

@ -440,12 +440,18 @@ func StatTree(id string) (ret *util.BlockStatResult) {
} }
const ( const (
searchMarkSpanStart = "<span data-type=\"search-mark\">" searchMarkDataType = "search-mark"
searchMarkSpanEnd = "</span>" virtualBlockRefDataType = "virtual-block-ref"
virtualBlockRefSpanStart = "<span data-type=\"virtual-block-ref\">"
virtualBlockRefSpanEnd = "</span>"
) )
func getMarkSpanStart(dataType string) string {
return fmt.Sprintf("<span data-type=\"%s\">", dataType)
}
func getMarkSpanEnd() string {
return "</span>"
}
func GetDoc(startID, endID, id string, index int, keyword string, mode int, size int) (blockCount, childBlockCount int, dom, parentID, parent2ID, rootID, typ string, eof bool, boxID, docPath string, err error) { func GetDoc(startID, endID, id string, index int, keyword string, mode int, size int) (blockCount, childBlockCount int, dom, parentID, parent2ID, rootID, typ string, eof bool, boxID, docPath string, err error) {
WaitForWritingFiles() // 写入数据时阻塞,避免获取到的数据不一致 WaitForWritingFiles() // 写入数据时阻塞,避免获取到的数据不一致
@ -631,7 +637,7 @@ func GetDoc(startID, endID, id string, index int, keyword string, mode int, size
} }
} }
if hitBlock { if hitBlock {
if markReplaceSpan(n, &unlinks, string(n.Tokens), keywords, searchMarkSpanStart, searchMarkSpanEnd, luteEngine) { if markReplaceSpan(n, &unlinks, keywords, searchMarkDataType, luteEngine) {
return ast.WalkContinue return ast.WalkContinue
} }
} }

View File

@ -179,7 +179,7 @@ func GetDocHistoryContent(historyPath, keyword string) (id, rootID, content stri
if ast.NodeText == n.Type { if ast.NodeText == n.Type {
if 0 < len(keywords) { if 0 < len(keywords) {
if markReplaceSpan(n, &unlinks, string(n.Tokens), keywords, searchMarkSpanStart, searchMarkSpanEnd, luteEngine) { if markReplaceSpan(n, &unlinks, keywords, searchMarkDataType, luteEngine) {
return ast.WalkContinue return ast.WalkContinue
} }
} }

View File

@ -830,8 +830,10 @@ func stringQuery(query string) string {
} }
// markReplaceSpan 用于处理搜索高亮。 // markReplaceSpan 用于处理搜索高亮。
func markReplaceSpan(n *ast.Node, unlinks *[]*ast.Node, text string, keywords []string, replacementStart, replacementEnd string, luteEngine *lute.Lute) bool { func markReplaceSpan(n *ast.Node, unlinks *[]*ast.Node, keywords []string, markSpanDataType string, luteEngine *lute.Lute) bool {
text = search.EncloseHighlighting(text, keywords, searchMarkSpanStart, searchMarkSpanEnd, Conf.Search.CaseSensitive) text := n.Content()
if ast.NodeText == n.Type {
text = search.EncloseHighlighting(text, keywords, getMarkSpanStart(markSpanDataType), getMarkSpanEnd(), Conf.Search.CaseSensitive)
n.Tokens = gulu.Str.ToBytes(text) n.Tokens = gulu.Str.ToBytes(text)
if bytes.Contains(n.Tokens, []byte("search-mark")) { if bytes.Contains(n.Tokens, []byte("search-mark")) {
n.Tokens = lex.EscapeMarkers(n.Tokens) n.Tokens = lex.EscapeMarkers(n.Tokens)
@ -846,6 +848,9 @@ func markReplaceSpan(n *ast.Node, unlinks *[]*ast.Node, text string, keywords []
*unlinks = append(*unlinks, n) *unlinks = append(*unlinks, n)
return true return true
} }
} else if ast.NodeTextMark == n.Type {
// 搜索结果高亮支持大部分行级元素 https://github.com/siyuan-note/siyuan/issues/6745
}
return false return false
} }

View File

@ -41,7 +41,7 @@ func processVirtualRef(n *ast.Node, unlinks *[]*ast.Node, virtualBlockRefKeyword
} }
content := string(n.Tokens) content := string(n.Tokens)
newContent := markReplaceSpanWithSplit(content, virtualBlockRefKeywords, virtualBlockRefSpanStart, virtualBlockRefSpanEnd) newContent := markReplaceSpanWithSplit(content, virtualBlockRefKeywords, getMarkSpanStart(virtualBlockRefDataType), getMarkSpanEnd())
if content != newContent { if content != newContent {
// 虚拟引用排除命中自身块命名和别名的情况 https://github.com/siyuan-note/siyuan/issues/3185 // 虚拟引用排除命中自身块命名和别名的情况 https://github.com/siyuan-note/siyuan/issues/3185
var blockKeys []string var blockKeys []string
@ -52,7 +52,7 @@ func processVirtualRef(n *ast.Node, unlinks *[]*ast.Node, virtualBlockRefKeyword
blockKeys = append(blockKeys, alias) blockKeys = append(blockKeys, alias)
} }
if 0 < len(blockKeys) { if 0 < len(blockKeys) {
keys := gulu.Str.SubstringsBetween(newContent, virtualBlockRefSpanStart, virtualBlockRefSpanEnd) keys := gulu.Str.SubstringsBetween(newContent, getMarkSpanStart(virtualBlockRefDataType), getMarkSpanEnd())
for _, k := range keys { for _, k := range keys {
if gulu.Str.Contains(k, blockKeys) { if gulu.Str.Contains(k, blockKeys) {
return true return true