mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 07:00:12 +01:00
🎨 块引搜索结果高亮片段改进 https://github.com/siyuan-note/siyuan/issues/5125
改进全局搜索多个关键字命中时高亮片段 https://github.com/siyuan-note/siyuan/issues/5124
This commit is contained in:
parent
e3e3546eea
commit
f14b71878b
6 changed files with 29 additions and 21 deletions
|
|
@ -526,7 +526,7 @@
|
||||||
"md35": "Virtual Reference keyword exclusion list",
|
"md35": "Virtual Reference keyword exclusion list",
|
||||||
"md36": "Use the English comma <code>,</code> to separate, the comma itself can be escaped by <code>\\,</code>",
|
"md36": "Use the English comma <code>,</code> to separate, the comma itself can be escaped by <code>\\,</code>",
|
||||||
"md37": "The maximum length of block ref dynamic anchor text",
|
"md37": "The maximum length of block ref dynamic anchor text",
|
||||||
"md38": "The maximum length of the anchor text that is automatically rendered when the block ref anchor text is not customized, the default is <code>64</code> characters",
|
"md38": "The maximum length of the anchor text that is automatically rendered when the block ref anchor text is not customized, the default is <code>96</code> characters",
|
||||||
"md39": "PlantUML Serve Address",
|
"md39": "PlantUML Serve Address",
|
||||||
"md40": "Leave blank to restore default <code>https://www.plantuml.com/plantuml/svg/~1</code>",
|
"md40": "Leave blank to restore default <code>https://www.plantuml.com/plantuml/svg/~1</code>",
|
||||||
"fileTree2": "The doc tree will automatically select the current document when the editor tab is switched",
|
"fileTree2": "The doc tree will automatically select the current document when the editor tab is switched",
|
||||||
|
|
|
||||||
|
|
@ -526,7 +526,7 @@
|
||||||
"md35": "Liste d'exclusion des mots-clés de la référence virtuelle",
|
"md35": "Liste d'exclusion des mots-clés de la référence virtuelle",
|
||||||
"md36": "Utiliser la virgule anglaise <code>,</code> pour séparer, la virgule elle-même peut être échappée par <code>\\,</code>",
|
"md36": "Utiliser la virgule anglaise <code>,</code> pour séparer, la virgule elle-même peut être échappée par <code>\\,</code>",
|
||||||
"md37": "La longueur maximale du texte d'ancrage dynamique de référence de bloc",
|
"md37": "La longueur maximale du texte d'ancrage dynamique de référence de bloc",
|
||||||
"md38": "La longueur maximale du texte d'ancrage qui est automatiquement rendu lorsque le texte d'ancrage de la référence de bloc n'est pas personnalisé, la valeur par défaut est de <code>64</code> caractères",
|
"md38": "La longueur maximale du texte d'ancrage qui est automatiquement rendu lorsque le texte d'ancrage de la référence de bloc n'est pas personnalisé, la valeur par défaut est de <code>96</code> caractères",
|
||||||
"md39": "Adresse Servo PlantUML",
|
"md39": "Adresse Servo PlantUML",
|
||||||
"md40": "Laissez vide pour restaurer <code>https://www.plantuml.com/plantuml/svg/~1/code> par défaut",
|
"md40": "Laissez vide pour restaurer <code>https://www.plantuml.com/plantuml/svg/~1/code> par défaut",
|
||||||
"fileTree2": "L'arbre des Docs sélectionne automatiquement le document en cours lorsque l'on change d'onglet d'édition",
|
"fileTree2": "L'arbre des Docs sélectionne automatiquement le document en cours lorsque l'on change d'onglet d'édition",
|
||||||
|
|
|
||||||
|
|
@ -526,7 +526,7 @@
|
||||||
"md35": "虛擬引用關鍵字排除列表",
|
"md35": "虛擬引用關鍵字排除列表",
|
||||||
"md36": "使用英文狀態下的逗號 <code>,</code> 進行分隔,逗號自身可通過 <code>\\,</code> 轉義",
|
"md36": "使用英文狀態下的逗號 <code>,</code> 進行分隔,逗號自身可通過 <code>\\,</code> 轉義",
|
||||||
"md37": "塊引動態錨文本最大長度",
|
"md37": "塊引動態錨文本最大長度",
|
||||||
"md38": "不自訂塊引錨文本時自動渲染錨文本的最大長度,預設為 <code>64</code> 個字元",
|
"md38": "不自訂塊引錨文本時自動渲染錨文本的最大長度,預設為 <code>96</code> 個字元",
|
||||||
"md39": "PlantUML 伺服地址",
|
"md39": "PlantUML 伺服地址",
|
||||||
"md40": "留空將恢復默認值 <code>https://www.plantuml.com/plantuml/svg/~1</code>",
|
"md40": "留空將恢復默認值 <code>https://www.plantuml.com/plantuml/svg/~1</code>",
|
||||||
"fileTree2": "在編輯器切換分頁時文檔樹會自動選中當前的文檔",
|
"fileTree2": "在編輯器切換分頁時文檔樹會自動選中當前的文檔",
|
||||||
|
|
|
||||||
|
|
@ -527,7 +527,7 @@
|
||||||
"md35": "虚拟引用关键字排除列表",
|
"md35": "虚拟引用关键字排除列表",
|
||||||
"md36": "使用英文状态下的逗号 <code>,</code> 进行分隔,逗号自身可通过 <code>\\,</code> 转义",
|
"md36": "使用英文状态下的逗号 <code>,</code> 进行分隔,逗号自身可通过 <code>\\,</code> 转义",
|
||||||
"md37": "块引动态锚文本最大长度",
|
"md37": "块引动态锚文本最大长度",
|
||||||
"md38": "不自定义块引锚文本时自动渲染锚文本的最大长度,默认为 <code>64</code> 个字符",
|
"md38": "不自定义块引锚文本时自动渲染锚文本的最大长度,默认为 <code>96</code> 个字符",
|
||||||
"md39": "PlantUML 伺服地址",
|
"md39": "PlantUML 伺服地址",
|
||||||
"md40": "留空将恢复默认值 <code>https://www.plantuml.com/plantuml/svg/~1</code>",
|
"md40": "留空将恢复默认值 <code>https://www.plantuml.com/plantuml/svg/~1</code>",
|
||||||
"fileTree2": "在编辑器页签切换时文档树会自动选中当前的文档",
|
"fileTree2": "在编辑器页签切换时文档树会自动选中当前的文档",
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ func NewEditor() *Editor {
|
||||||
HistoryRetentionDays: 30,
|
HistoryRetentionDays: 30,
|
||||||
Emoji: []string{},
|
Emoji: []string{},
|
||||||
VirtualBlockRef: false,
|
VirtualBlockRef: false,
|
||||||
BlockRefDynamicAnchorTextMaxLen: 64,
|
BlockRefDynamicAnchorTextMaxLen: 96,
|
||||||
PlantUMLServePath: "https://www.plantuml.com/plantuml/svg/~1",
|
PlantUMLServePath: "https://www.plantuml.com/plantuml/svg/~1",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,6 @@ import (
|
||||||
|
|
||||||
"github.com/88250/gulu"
|
"github.com/88250/gulu"
|
||||||
"github.com/88250/lute/ast"
|
"github.com/88250/lute/ast"
|
||||||
"github.com/88250/lute/html"
|
|
||||||
"github.com/88250/lute/parse"
|
"github.com/88250/lute/parse"
|
||||||
"github.com/jinzhu/copier"
|
"github.com/jinzhu/copier"
|
||||||
"github.com/siyuan-note/siyuan/kernel/conf"
|
"github.com/siyuan-note/siyuan/kernel/conf"
|
||||||
|
|
@ -249,12 +248,12 @@ func fullTextSearchRefBlock(keyword string, beforeLen int) (ret []*Block) {
|
||||||
}
|
}
|
||||||
|
|
||||||
projections := "id, parent_id, root_id, hash, box, path, " +
|
projections := "id, parent_id, root_id, hash, box, path, " +
|
||||||
"highlight(" + table + ", 6, '__@mark__', '__mark@__') AS hpath, " +
|
"snippet(" + table + ", 6, '<mark>__r', '</mark>', '...', 64) AS hpath, " +
|
||||||
"highlight(" + table + ", 7, '__@mark__', '__mark@__') AS name, " +
|
"snippet(" + table + ", 7, '<mark>__r', '</mark>', '...', 64) AS name, " +
|
||||||
"highlight(" + table + ", 8, '__@mark__', '__mark@__') AS alias, " +
|
"snippet(" + table + ", 8, '<mark>__r', '</mark>', '...', 64) AS alias, " +
|
||||||
"highlight(" + table + ", 9, '__@mark__', '__mark@__') AS memo, " +
|
"snippet(" + table + ", 9, '<mark>__r', '</mark>', '...', 64) AS memo, " +
|
||||||
"tag, " +
|
"tag, " +
|
||||||
"highlight(" + table + ", 11, '__@mark__', '__mark@__') AS content, " +
|
"snippet(" + table + ", 11, '<mark>__r', '</mark>', '...', 64) AS content, " +
|
||||||
"fcontent, markdown, length, type, subtype, ial, sort, created, updated"
|
"fcontent, markdown, length, type, subtype, ial, sort, created, updated"
|
||||||
stmt := "SELECT " + projections + " FROM " + table + " WHERE " + table + " MATCH '" + columnFilter() + ":(" + quotedKeyword + ")' AND type IN " + Conf.Search.TypeFilter()
|
stmt := "SELECT " + projections + " FROM " + table + " WHERE " + table + " MATCH '" + columnFilter() + ":(" + quotedKeyword + ")' AND type IN " + Conf.Search.TypeFilter()
|
||||||
orderBy := ` order by case
|
orderBy := ` order by case
|
||||||
|
|
@ -383,19 +382,28 @@ func query2Stmt(queryStr string) (ret string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func markSearch(text string, keyword string, beforeLen int) (pos int, marked string, score float64) {
|
func markSearch(text string, keyword string, beforeLen int) (marked string, score float64) {
|
||||||
if 0 == len(keyword) {
|
if 0 == len(keyword) {
|
||||||
marked = text
|
marked = text
|
||||||
if maxLen := 5120; maxLen < utf8.RuneCountInString(marked) {
|
if maxLen := 5120; maxLen < utf8.RuneCountInString(marked) {
|
||||||
marked = gulu.Str.SubStr(marked, maxLen) + "..."
|
marked = gulu.Str.SubStr(marked, maxLen) + "..."
|
||||||
}
|
}
|
||||||
marked = html.EscapeString(marked)
|
|
||||||
marked = strings.ReplaceAll(marked, "__@mark__", "<mark>")
|
if strings.Contains(marked, "<mark>__r") { // 使用 FTS snippet() 处理过高亮片段,这里简单替换后就返回
|
||||||
marked = strings.ReplaceAll(marked, "__mark@__", "</mark>")
|
marked = strings.ReplaceAll(marked, "<mark>__r", "<mark>")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
pos, marked = search.MarkText(text, keyword, beforeLen, Conf.Search.CaseSensitive)
|
keywords := util.SubstringsBetween(marked, "__@mark__", "__mark@__")
|
||||||
|
keywords = util.RemoveDuplicatedElem(keywords)
|
||||||
|
keyword = strings.Join(keywords, search.TermSep)
|
||||||
|
marked = strings.ReplaceAll(marked, "__@mark__", "")
|
||||||
|
marked = strings.ReplaceAll(marked, "__mark@__", "")
|
||||||
|
_, marked = search.MarkText(marked, keyword, beforeLen, Conf.Search.CaseSensitive)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
pos, marked := search.MarkText(text, keyword, beforeLen, Conf.Search.CaseSensitive)
|
||||||
if -1 < pos {
|
if -1 < pos {
|
||||||
if 0 == pos {
|
if 0 == pos {
|
||||||
score = 1
|
score = 1
|
||||||
|
|
@ -424,7 +432,7 @@ func fromSQLBlock(sqlBlock *sql.Block, terms string, beforeLen int) (block *Bloc
|
||||||
content := sqlBlock.Content
|
content := sqlBlock.Content
|
||||||
p := sqlBlock.Path
|
p := sqlBlock.Path
|
||||||
|
|
||||||
_, content, _ = markSearch(content, terms, beforeLen)
|
content, _ = markSearch(content, terms, beforeLen)
|
||||||
markdown := maxContent(sqlBlock.Markdown, 5120)
|
markdown := maxContent(sqlBlock.Markdown, 5120)
|
||||||
content = maxContent(content, 5120)
|
content = maxContent(content, 5120)
|
||||||
|
|
||||||
|
|
@ -454,20 +462,20 @@ func fromSQLBlock(sqlBlock *sql.Block, terms string, beforeLen int) (block *Bloc
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, hPath, _ := markSearch(sqlBlock.HPath, terms, 18)
|
hPath, _ := markSearch(sqlBlock.HPath, terms, 18)
|
||||||
if !strings.HasPrefix(hPath, "/") {
|
if !strings.HasPrefix(hPath, "/") {
|
||||||
hPath = "/" + hPath
|
hPath = "/" + hPath
|
||||||
}
|
}
|
||||||
block.HPath = hPath
|
block.HPath = hPath
|
||||||
|
|
||||||
if "" != block.Name {
|
if "" != block.Name {
|
||||||
_, block.Name, _ = markSearch(block.Name, terms, 256)
|
block.Name, _ = markSearch(block.Name, terms, 256)
|
||||||
}
|
}
|
||||||
if "" != block.Alias {
|
if "" != block.Alias {
|
||||||
_, block.Alias, _ = markSearch(block.Alias, terms, 256)
|
block.Alias, _ = markSearch(block.Alias, terms, 256)
|
||||||
}
|
}
|
||||||
if "" != block.Memo {
|
if "" != block.Memo {
|
||||||
_, block.Memo, _ = markSearch(block.Memo, terms, 256)
|
block.Memo, _ = markSearch(block.Memo, terms, 256)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue