♻️ 搜索结果高亮支持大部分行级元素 https://github.com/siyuan-note/siyuan/issues/6745
This commit is contained in:
parent
32fb7e1882
commit
9a0f2e7bc3
5 changed files with 35 additions and 25 deletions
|
@ -204,7 +204,7 @@ func buildBacklink(refID string, refTree *parse.Tree, mentionKeywords []string,
|
|||
}
|
||||
if ast.NodeText == n.Type {
|
||||
text := string(n.Tokens)
|
||||
newText := markReplaceSpanWithSplit(text, mentionKeywords, searchMarkSpanStart, searchMarkSpanEnd)
|
||||
newText := markReplaceSpanWithSplit(text, mentionKeywords, getMarkSpanStart(searchMarkDataType), getMarkSpanEnd())
|
||||
if text == newText {
|
||||
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) {
|
||||
ret = []*Block{}
|
||||
|
||||
if 1 > len(mentionKeywords) {
|
||||
return
|
||||
}
|
||||
|
@ -696,7 +695,7 @@ func searchBackmention(mentionKeywords []string, keyword string, excludeBacklink
|
|||
continue
|
||||
}
|
||||
|
||||
newText := markReplaceSpanWithSplit(text, mentionKeywords, searchMarkSpanStart, searchMarkSpanEnd)
|
||||
newText := markReplaceSpanWithSplit(text, mentionKeywords, getMarkSpanStart(searchMarkDataType), getMarkSpanEnd())
|
||||
if text != newText {
|
||||
tmp = append(tmp, b)
|
||||
}
|
||||
|
|
|
@ -440,12 +440,18 @@ func StatTree(id string) (ret *util.BlockStatResult) {
|
|||
}
|
||||
|
||||
const (
|
||||
searchMarkSpanStart = "<span data-type=\"search-mark\">"
|
||||
searchMarkSpanEnd = "</span>"
|
||||
virtualBlockRefSpanStart = "<span data-type=\"virtual-block-ref\">"
|
||||
virtualBlockRefSpanEnd = "</span>"
|
||||
searchMarkDataType = "search-mark"
|
||||
virtualBlockRefDataType = "virtual-block-ref"
|
||||
)
|
||||
|
||||
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) {
|
||||
WaitForWritingFiles() // 写入数据时阻塞,避免获取到的数据不一致
|
||||
|
||||
|
@ -631,7 +637,7 @@ func GetDoc(startID, endID, id string, index int, keyword string, mode int, size
|
|||
}
|
||||
}
|
||||
if hitBlock {
|
||||
if markReplaceSpan(n, &unlinks, string(n.Tokens), keywords, searchMarkSpanStart, searchMarkSpanEnd, luteEngine) {
|
||||
if markReplaceSpan(n, &unlinks, keywords, searchMarkDataType, luteEngine) {
|
||||
return ast.WalkContinue
|
||||
}
|
||||
}
|
||||
|
|
|
@ -179,7 +179,7 @@ func GetDocHistoryContent(historyPath, keyword string) (id, rootID, content stri
|
|||
|
||||
if ast.NodeText == n.Type {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -830,21 +830,26 @@ func stringQuery(query string) string {
|
|||
}
|
||||
|
||||
// markReplaceSpan 用于处理搜索高亮。
|
||||
func markReplaceSpan(n *ast.Node, unlinks *[]*ast.Node, text string, keywords []string, replacementStart, replacementEnd string, luteEngine *lute.Lute) bool {
|
||||
text = search.EncloseHighlighting(text, keywords, searchMarkSpanStart, searchMarkSpanEnd, Conf.Search.CaseSensitive)
|
||||
n.Tokens = gulu.Str.ToBytes(text)
|
||||
if bytes.Contains(n.Tokens, []byte("search-mark")) {
|
||||
n.Tokens = lex.EscapeMarkers(n.Tokens)
|
||||
linkTree := parse.Inline("", n.Tokens, luteEngine.ParseOptions)
|
||||
var children []*ast.Node
|
||||
for c := linkTree.Root.FirstChild.FirstChild; nil != c; c = c.Next {
|
||||
children = append(children, c)
|
||||
func markReplaceSpan(n *ast.Node, unlinks *[]*ast.Node, keywords []string, markSpanDataType string, luteEngine *lute.Lute) bool {
|
||||
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)
|
||||
if bytes.Contains(n.Tokens, []byte("search-mark")) {
|
||||
n.Tokens = lex.EscapeMarkers(n.Tokens)
|
||||
linkTree := parse.Inline("", n.Tokens, luteEngine.ParseOptions)
|
||||
var children []*ast.Node
|
||||
for c := linkTree.Root.FirstChild.FirstChild; nil != c; c = c.Next {
|
||||
children = append(children, c)
|
||||
}
|
||||
for _, c := range children {
|
||||
n.InsertBefore(c)
|
||||
}
|
||||
*unlinks = append(*unlinks, n)
|
||||
return true
|
||||
}
|
||||
for _, c := range children {
|
||||
n.InsertBefore(c)
|
||||
}
|
||||
*unlinks = append(*unlinks, n)
|
||||
return true
|
||||
} else if ast.NodeTextMark == n.Type {
|
||||
// 搜索结果高亮支持大部分行级元素 https://github.com/siyuan-note/siyuan/issues/6745
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ func processVirtualRef(n *ast.Node, unlinks *[]*ast.Node, virtualBlockRefKeyword
|
|||
}
|
||||
|
||||
content := string(n.Tokens)
|
||||
newContent := markReplaceSpanWithSplit(content, virtualBlockRefKeywords, virtualBlockRefSpanStart, virtualBlockRefSpanEnd)
|
||||
newContent := markReplaceSpanWithSplit(content, virtualBlockRefKeywords, getMarkSpanStart(virtualBlockRefDataType), getMarkSpanEnd())
|
||||
if content != newContent {
|
||||
// 虚拟引用排除命中自身块命名和别名的情况 https://github.com/siyuan-note/siyuan/issues/3185
|
||||
var blockKeys []string
|
||||
|
@ -52,7 +52,7 @@ func processVirtualRef(n *ast.Node, unlinks *[]*ast.Node, virtualBlockRefKeyword
|
|||
blockKeys = append(blockKeys, alias)
|
||||
}
|
||||
if 0 < len(blockKeys) {
|
||||
keys := gulu.Str.SubstringsBetween(newContent, virtualBlockRefSpanStart, virtualBlockRefSpanEnd)
|
||||
keys := gulu.Str.SubstringsBetween(newContent, getMarkSpanStart(virtualBlockRefDataType), getMarkSpanEnd())
|
||||
for _, k := range keys {
|
||||
if gulu.Str.Contains(k, blockKeys) {
|
||||
return true
|
||||
|
|
Loading…
Add table
Reference in a new issue