mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-05-03 04:20:05 +08:00
🎨 Improve search highlighting https://github.com/siyuan-note/siyuan/issues/13343
This commit is contained in:
parent
4b3f95e4bf
commit
c1fd34f57b
@ -1061,8 +1061,14 @@ func getDoc(c *gin.Context) {
|
|||||||
if nil != isBacklinkArg {
|
if nil != isBacklinkArg {
|
||||||
isBacklink = isBacklinkArg.(bool)
|
isBacklink = isBacklinkArg.(bool)
|
||||||
}
|
}
|
||||||
|
highlightArg := arg["highlight"]
|
||||||
|
highlight := true
|
||||||
|
if nil != highlightArg {
|
||||||
|
highlight = highlightArg.(bool)
|
||||||
|
}
|
||||||
|
|
||||||
blockCount, content, parentID, parent2ID, rootID, typ, eof, scroll, boxID, docPath, isBacklinkExpand, err := model.GetDoc(startID, endID, id, index, query, queryTypes, queryMethod, mode, size, isBacklink)
|
blockCount, content, parentID, parent2ID, rootID, typ, eof, scroll, boxID, docPath, isBacklinkExpand, err :=
|
||||||
|
model.GetDoc(startID, endID, id, index, query, queryTypes, queryMethod, mode, size, isBacklink, highlight)
|
||||||
if model.ErrBlockNotFound == err {
|
if model.ErrBlockNotFound == err {
|
||||||
ret.Code = 3
|
ret.Code = 3
|
||||||
return
|
return
|
||||||
|
@ -145,7 +145,11 @@ func getDocHistoryContent(c *gin.Context) {
|
|||||||
if nil != k {
|
if nil != k {
|
||||||
keyword = k.(string)
|
keyword = k.(string)
|
||||||
}
|
}
|
||||||
id, rootID, content, isLargeDoc, err := model.GetDocHistoryContent(historyPath, keyword)
|
highlight := true
|
||||||
|
if val, ok := arg["highlight"]; ok {
|
||||||
|
highlight = val.(bool)
|
||||||
|
}
|
||||||
|
id, rootID, content, isLargeDoc, err := model.GetDocHistoryContent(historyPath, keyword, highlight)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ret.Code = -1
|
ret.Code = -1
|
||||||
ret.Msg = err.Error()
|
ret.Msg = err.Error()
|
||||||
|
@ -56,7 +56,11 @@ func getBackmentionDoc(c *gin.Context) {
|
|||||||
if val, ok := arg["containChildren"]; ok {
|
if val, ok := arg["containChildren"]; ok {
|
||||||
containChildren = val.(bool)
|
containChildren = val.(bool)
|
||||||
}
|
}
|
||||||
backlinks := model.GetBackmentionDoc(defID, refTreeID, keyword, containChildren)
|
highlight := true
|
||||||
|
if val, ok := arg["highlight"]; ok {
|
||||||
|
highlight = val.(bool)
|
||||||
|
}
|
||||||
|
backlinks := model.GetBackmentionDoc(defID, refTreeID, keyword, containChildren, highlight)
|
||||||
ret.Data = map[string]interface{}{
|
ret.Data = map[string]interface{}{
|
||||||
"backmentions": backlinks,
|
"backmentions": backlinks,
|
||||||
}
|
}
|
||||||
@ -78,7 +82,11 @@ func getBacklinkDoc(c *gin.Context) {
|
|||||||
if val, ok := arg["containChildren"]; ok {
|
if val, ok := arg["containChildren"]; ok {
|
||||||
containChildren = val.(bool)
|
containChildren = val.(bool)
|
||||||
}
|
}
|
||||||
backlinks := model.GetBacklinkDoc(defID, refTreeID, keyword, containChildren)
|
highlight := true
|
||||||
|
if val, ok := arg["highlight"]; ok {
|
||||||
|
highlight = val.(bool)
|
||||||
|
}
|
||||||
|
backlinks := model.GetBacklinkDoc(defID, refTreeID, keyword, containChildren, highlight)
|
||||||
ret.Data = map[string]interface{}{
|
ret.Data = map[string]interface{}{
|
||||||
"backlinks": backlinks,
|
"backlinks": backlinks,
|
||||||
}
|
}
|
||||||
|
@ -62,7 +62,7 @@ type Backlink struct {
|
|||||||
node *ast.Node // 仅用于按文档内容顺序排序
|
node *ast.Node // 仅用于按文档内容顺序排序
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetBackmentionDoc(defID, refTreeID, keyword string, containChildren bool) (ret []*Backlink) {
|
func GetBackmentionDoc(defID, refTreeID, keyword string, containChildren, highlight bool) (ret []*Backlink) {
|
||||||
var keywords []string
|
var keywords []string
|
||||||
keyword = strings.TrimSpace(keyword)
|
keyword = strings.TrimSpace(keyword)
|
||||||
if "" != keyword {
|
if "" != keyword {
|
||||||
@ -102,7 +102,7 @@ func GetBackmentionDoc(defID, refTreeID, keyword string, containChildren bool) (
|
|||||||
var refTree *parse.Tree
|
var refTree *parse.Tree
|
||||||
trees := filesys.LoadTrees(mentionBlockIDs)
|
trees := filesys.LoadTrees(mentionBlockIDs)
|
||||||
for id, tree := range trees {
|
for id, tree := range trees {
|
||||||
backlink := buildBacklink(id, tree, mentionKeywords, luteEngine)
|
backlink := buildBacklink(id, tree, mentionKeywords, highlight, luteEngine)
|
||||||
if nil != backlink {
|
if nil != backlink {
|
||||||
ret = append(ret, backlink)
|
ret = append(ret, backlink)
|
||||||
}
|
}
|
||||||
@ -118,7 +118,7 @@ func GetBackmentionDoc(defID, refTreeID, keyword string, containChildren bool) (
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetBacklinkDoc(defID, refTreeID, keyword string, containChildren bool) (ret []*Backlink) {
|
func GetBacklinkDoc(defID, refTreeID, keyword string, containChildren, highlight bool) (ret []*Backlink) {
|
||||||
var keywords []string
|
var keywords []string
|
||||||
keyword = strings.TrimSpace(keyword)
|
keyword = strings.TrimSpace(keyword)
|
||||||
if "" != keyword {
|
if "" != keyword {
|
||||||
@ -150,7 +150,7 @@ func GetBacklinkDoc(defID, refTreeID, keyword string, containChildren bool) (ret
|
|||||||
|
|
||||||
luteEngine := util.NewLute()
|
luteEngine := util.NewLute()
|
||||||
for _, linkRef := range linkRefs {
|
for _, linkRef := range linkRefs {
|
||||||
backlink := buildBacklink(linkRef.ID, refTree, keywords, luteEngine)
|
backlink := buildBacklink(linkRef.ID, refTree, keywords, highlight, luteEngine)
|
||||||
if nil != backlink {
|
if nil != backlink {
|
||||||
ret = append(ret, backlink)
|
ret = append(ret, backlink)
|
||||||
}
|
}
|
||||||
@ -191,7 +191,7 @@ func sortBacklinks(backlinks []*Backlink, tree *parse.Tree) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildBacklink(refID string, refTree *parse.Tree, keywords []string, luteEngine *lute.Lute) (ret *Backlink) {
|
func buildBacklink(refID string, refTree *parse.Tree, keywords []string, highlight bool, luteEngine *lute.Lute) (ret *Backlink) {
|
||||||
n := treenode.GetNodeInTree(refTree, refID)
|
n := treenode.GetNodeInTree(refTree, refID)
|
||||||
if nil == n {
|
if nil == n {
|
||||||
return
|
return
|
||||||
@ -199,7 +199,7 @@ func buildBacklink(refID string, refTree *parse.Tree, keywords []string, luteEng
|
|||||||
|
|
||||||
renderNodes, expand := getBacklinkRenderNodes(n)
|
renderNodes, expand := getBacklinkRenderNodes(n)
|
||||||
|
|
||||||
if 0 < len(keywords) {
|
if highlight && 0 < len(keywords) {
|
||||||
for _, renderNode := range renderNodes {
|
for _, renderNode := range renderNodes {
|
||||||
var unlinks []*ast.Node
|
var unlinks []*ast.Node
|
||||||
|
|
||||||
|
@ -599,7 +599,7 @@ func StatTree(id string) (ret *util.BlockStatResult) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDoc(startID, endID, id string, index int, query string, queryTypes map[string]bool, queryMethod, mode int, size int, isBacklink bool) (blockCount int, dom, parentID, parent2ID, rootID, typ string, eof, scroll bool, boxID, docPath string, isBacklinkExpand bool, err error) {
|
func GetDoc(startID, endID, id string, index int, query string, queryTypes map[string]bool, queryMethod, mode int, size int, isBacklink, highlight bool) (blockCount int, dom, parentID, parent2ID, rootID, typ string, eof, scroll bool, boxID, docPath string, isBacklinkExpand bool, err error) {
|
||||||
//os.MkdirAll("pprof", 0755)
|
//os.MkdirAll("pprof", 0755)
|
||||||
//cpuProfile, _ := os.Create("pprof/GetDoc")
|
//cpuProfile, _ := os.Create("pprof/GetDoc")
|
||||||
//pprof.StartCPUProfile(cpuProfile)
|
//pprof.StartCPUProfile(cpuProfile)
|
||||||
@ -844,7 +844,7 @@ func GetDoc(startID, endID, id string, index int, query string, queryTypes map[s
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if 0 < len(keywords) {
|
if highlight && 0 < len(keywords) {
|
||||||
hitBlock := false
|
hitBlock := false
|
||||||
for p := n.Parent; nil != p; p = p.Parent {
|
for p := n.Parent; nil != p; p = p.Parent {
|
||||||
if p.ID == id {
|
if p.ID == id {
|
||||||
|
@ -147,7 +147,7 @@ func ClearWorkspaceHistory() (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDocHistoryContent(historyPath, keyword string) (id, rootID, content string, isLargeDoc bool, err error) {
|
func GetDocHistoryContent(historyPath, keyword string, highlight bool) (id, rootID, content string, isLargeDoc bool, err error) {
|
||||||
if !gulu.File.IsExist(historyPath) {
|
if !gulu.File.IsExist(historyPath) {
|
||||||
logging.LogWarnf("doc history [%s] not exist", historyPath)
|
logging.LogWarnf("doc history [%s] not exist", historyPath)
|
||||||
return
|
return
|
||||||
@ -185,7 +185,7 @@ func GetDocHistoryContent(historyPath, keyword string) (id, rootID, content stri
|
|||||||
n.RemoveIALAttr("heading-fold")
|
n.RemoveIALAttr("heading-fold")
|
||||||
n.RemoveIALAttr("fold")
|
n.RemoveIALAttr("fold")
|
||||||
|
|
||||||
if 0 < len(keywords) {
|
if highlight && 0 < len(keywords) {
|
||||||
if markReplaceSpan(n, &unlinks, keywords, search.MarkDataType, luteEngine) {
|
if markReplaceSpan(n, &unlinks, keywords, search.MarkDataType, luteEngine) {
|
||||||
return ast.WalkContinue
|
return ast.WalkContinue
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user