2023-06-24 20:39:55 +08:00
|
|
|
// SiYuan - Refactor your thinking
|
2022-10-26 09:24:07 +08:00
|
|
|
// Copyright (c) 2020-present, b3log.org
|
|
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
import (
|
2023-02-16 13:14:15 +08:00
|
|
|
"bytes"
|
2023-02-16 13:41:34 +08:00
|
|
|
"regexp"
|
|
|
|
|
"sort"
|
|
|
|
|
"strings"
|
2023-02-17 09:35:02 +08:00
|
|
|
"time"
|
2023-02-16 13:41:34 +08:00
|
|
|
|
2022-10-26 09:24:07 +08:00
|
|
|
"github.com/88250/gulu"
|
2022-10-26 09:50:56 +08:00
|
|
|
"github.com/88250/lute"
|
|
|
|
|
"github.com/88250/lute/ast"
|
2025-07-03 10:01:04 +08:00
|
|
|
"github.com/88250/lute/editor"
|
2022-10-26 09:50:56 +08:00
|
|
|
"github.com/88250/lute/parse"
|
2023-02-17 15:26:09 +08:00
|
|
|
"github.com/ClarkThan/ahocorasick"
|
2023-02-16 13:14:15 +08:00
|
|
|
"github.com/dgraph-io/ristretto"
|
|
|
|
|
"github.com/siyuan-note/siyuan/kernel/search"
|
2022-10-26 09:24:07 +08:00
|
|
|
"github.com/siyuan-note/siyuan/kernel/sql"
|
2024-05-12 22:45:04 +08:00
|
|
|
"github.com/siyuan-note/siyuan/kernel/task"
|
2022-10-26 09:50:56 +08:00
|
|
|
"github.com/siyuan-note/siyuan/kernel/treenode"
|
2024-11-27 20:13:22 +08:00
|
|
|
"github.com/siyuan-note/siyuan/kernel/util"
|
2022-10-26 09:24:07 +08:00
|
|
|
)
|
|
|
|
|
|
2023-02-16 13:14:15 +08:00
|
|
|
// virtualBlockRefCache 用于保存块关联的虚拟引用关键字。
|
|
|
|
|
// 改进打开虚拟引用后加载文档的性能 https://github.com/siyuan-note/siyuan/issues/7378
|
2025-04-25 12:21:24 +08:00
|
|
|
var virtualBlockRefCache, _ = ristretto.NewCache(&ristretto.Config{
|
2023-02-16 13:44:08 +08:00
|
|
|
NumCounters: 102400,
|
|
|
|
|
MaxCost: 10240,
|
2023-02-16 13:14:15 +08:00
|
|
|
BufferItems: 64,
|
|
|
|
|
})
|
|
|
|
|
|
2025-11-15 17:16:23 +08:00
|
|
|
// newlineRegexp 用于匹配连续或单个换行符的正则表达式
|
|
|
|
|
var newlineRegexp = regexp.MustCompile(`[\r\n]+`)
|
|
|
|
|
|
2023-02-16 13:14:15 +08:00
|
|
|
func getBlockVirtualRefKeywords(root *ast.Node) (ret []string) {
|
2023-02-16 13:41:34 +08:00
|
|
|
val, ok := virtualBlockRefCache.Get(root.ID)
|
|
|
|
|
if !ok {
|
|
|
|
|
buf := bytes.Buffer{}
|
|
|
|
|
ast.Walk(root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
|
|
|
|
if !entering || !n.IsBlock() {
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 23:31:54 +08:00
|
|
|
content := sql.NodeStaticContent(n, nil, false, false, false)
|
2025-07-03 10:01:04 +08:00
|
|
|
content = strings.ReplaceAll(content, editor.Zwsp, "")
|
2023-02-16 13:41:34 +08:00
|
|
|
buf.WriteString(content)
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
})
|
|
|
|
|
content := buf.String()
|
2023-09-17 11:15:12 +08:00
|
|
|
ret = putBlockVirtualRefKeywords(content, root)
|
2023-02-16 13:34:27 +08:00
|
|
|
return
|
2023-02-16 13:14:15 +08:00
|
|
|
}
|
2025-04-25 12:21:24 +08:00
|
|
|
ret = val.([]string)
|
2023-02-16 13:14:15 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-17 11:15:12 +08:00
|
|
|
func putBlockVirtualRefKeywords(blockContent string, root *ast.Node) (ret []string) {
|
|
|
|
|
keywords := getVirtualRefKeywords(root)
|
2023-02-16 13:14:15 +08:00
|
|
|
if 1 > len(keywords) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
contentTmp := blockContent
|
2023-02-17 15:17:25 +08:00
|
|
|
var keywordsTmp []string
|
2023-02-16 13:14:15 +08:00
|
|
|
if !Conf.Search.CaseSensitive {
|
|
|
|
|
contentTmp = strings.ToLower(blockContent)
|
2023-02-17 14:43:03 +08:00
|
|
|
for _, keyword := range keywords {
|
2023-02-17 15:17:25 +08:00
|
|
|
keywordsTmp = append(keywordsTmp, strings.ToLower(keyword))
|
2023-02-17 14:43:03 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
for _, keyword := range keywords {
|
2023-02-17 15:17:25 +08:00
|
|
|
keywordsTmp = append(keywordsTmp, keyword)
|
2023-02-16 13:14:15 +08:00
|
|
|
}
|
2023-02-17 14:23:06 +08:00
|
|
|
}
|
2023-02-16 13:14:15 +08:00
|
|
|
|
2023-02-17 15:25:59 +08:00
|
|
|
m := ahocorasick.NewMatcher()
|
|
|
|
|
m.BuildWithPatterns(keywordsTmp)
|
|
|
|
|
hits := m.Search(contentTmp)
|
2023-02-17 15:17:25 +08:00
|
|
|
for _, hit := range hits {
|
2023-02-17 15:25:59 +08:00
|
|
|
ret = append(ret, hit)
|
2023-02-16 13:14:15 +08:00
|
|
|
}
|
|
|
|
|
|
2023-02-16 13:43:53 +08:00
|
|
|
if 1 > len(ret) {
|
2023-02-16 13:14:15 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-16 13:43:53 +08:00
|
|
|
ret = gulu.Str.RemoveDuplicatedElem(ret)
|
2023-09-17 11:15:12 +08:00
|
|
|
virtualBlockRefCache.SetWithTTL(root.ID, ret, 1, 10*time.Minute)
|
2023-02-16 13:43:53 +08:00
|
|
|
return
|
2023-02-16 13:14:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func CacheVirtualBlockRefJob() {
|
2024-05-12 22:45:04 +08:00
|
|
|
if !Conf.Editor.VirtualBlockRef {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
task.AppendTask(task.CacheVirtualBlockRef, ResetVirtualBlockRefCache)
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-12 23:04:00 +08:00
|
|
|
func ResetVirtualBlockRefCache() {
|
|
|
|
|
virtualBlockRefCache.Clear()
|
2023-02-16 13:14:15 +08:00
|
|
|
if !Conf.Editor.VirtualBlockRef {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-28 22:13:23 +08:00
|
|
|
searchIgnoreLines := getSearchIgnoreLines()
|
|
|
|
|
refSearchIgnoreLines := getRefSearchIgnoreLines()
|
|
|
|
|
keywords := sql.QueryVirtualRefKeywords(Conf.Search.VirtualRefName, Conf.Search.VirtualRefAlias, Conf.Search.VirtualRefAnchor, Conf.Search.VirtualRefDoc, searchIgnoreLines, refSearchIgnoreLines)
|
2023-02-16 13:14:15 +08:00
|
|
|
virtualBlockRefCache.Set("virtual_ref", keywords, 1)
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-15 17:16:23 +08:00
|
|
|
// addNewKeywords 将新关键字添加到虚拟引用关键字列表中,如果不存在则追加,保留空白字符
|
|
|
|
|
func addNewKeywords(keywordsStr string, newKeywords []string) string {
|
|
|
|
|
keywordsStr = strings.TrimSpace(keywordsStr)
|
|
|
|
|
if 0 == len(newKeywords) {
|
|
|
|
|
return keywordsStr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var builder strings.Builder
|
|
|
|
|
if "" != keywordsStr {
|
|
|
|
|
if !strings.HasSuffix(keywordsStr, "\\,") {
|
|
|
|
|
keywordsStr = strings.TrimSuffix(keywordsStr, ",")
|
|
|
|
|
}
|
|
|
|
|
builder.WriteString(keywordsStr)
|
|
|
|
|
builder.WriteString(",")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
keywords := gulu.Str.RemoveDuplicatedElem(parseKeywords(keywordsStr))
|
|
|
|
|
newKeywords = gulu.Str.RemoveDuplicatedElem(newKeywords)
|
|
|
|
|
allKeys := make(map[string]bool)
|
|
|
|
|
|
|
|
|
|
// 添加新关键字
|
|
|
|
|
for _, keyword := range newKeywords {
|
|
|
|
|
keywordTrimmed := strings.TrimSpace(keyword)
|
|
|
|
|
if "" == keywordTrimmed {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if gulu.Str.Contains(keywordTrimmed, keywords) {
|
|
|
|
|
// 剔除已存在的关键字
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if _, value := allKeys[keywordTrimmed]; value {
|
|
|
|
|
// 剔除重复的关键字
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
allKeys[keywordTrimmed] = true
|
|
|
|
|
builder.WriteString(strings.ReplaceAll(keyword, ",", "\\,")) // 字符串切片转换为字符串,需要转义逗号
|
|
|
|
|
builder.WriteString(",")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return strings.TrimSuffix(builder.String(), ",")
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-17 22:34:35 +08:00
|
|
|
func AddVirtualBlockRefInclude(keyword []string) {
|
|
|
|
|
if 1 > len(keyword) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-11-15 17:16:23 +08:00
|
|
|
Conf.Editor.VirtualBlockRefInclude = addNewKeywords(Conf.Editor.VirtualBlockRefInclude, keyword)
|
2023-12-17 22:34:35 +08:00
|
|
|
Conf.Save()
|
|
|
|
|
ResetVirtualBlockRefCache()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func AddVirtualBlockRefExclude(keyword []string) {
|
|
|
|
|
if 1 > len(keyword) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-11-15 17:16:23 +08:00
|
|
|
Conf.Editor.VirtualBlockRefExclude = addNewKeywords(Conf.Editor.VirtualBlockRefExclude, keyword)
|
2023-12-17 22:34:35 +08:00
|
|
|
Conf.Save()
|
|
|
|
|
ResetVirtualBlockRefCache()
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-26 09:50:56 +08:00
|
|
|
func processVirtualRef(n *ast.Node, unlinks *[]*ast.Node, virtualBlockRefKeywords []string, refCount map[string]int, luteEngine *lute.Lute) bool {
|
2024-04-09 23:12:26 +08:00
|
|
|
if !Conf.Editor.VirtualBlockRef || 1 > len(virtualBlockRefKeywords) {
|
2022-10-26 09:50:56 +08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-25 11:06:23 +08:00
|
|
|
if ast.NodeText != n.Type {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-26 09:50:56 +08:00
|
|
|
parentBlock := treenode.ParentBlock(n)
|
2024-04-09 23:12:26 +08:00
|
|
|
if nil == parentBlock {
|
2022-10-26 09:50:56 +08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-09 23:12:26 +08:00
|
|
|
if 0 < refCount[parentBlock.ID] {
|
|
|
|
|
// 如果块被引用过,则将其自身的文本排除在虚拟引用关键字之外
|
|
|
|
|
// Referenced blocks support rendering virtual references https://github.com/siyuan-note/siyuan/issues/10960
|
|
|
|
|
parentText := getNodeRefText(parentBlock)
|
|
|
|
|
virtualBlockRefKeywords = gulu.Str.RemoveElem(virtualBlockRefKeywords, parentText)
|
2023-02-16 13:14:15 +08:00
|
|
|
}
|
|
|
|
|
|
2022-10-26 09:50:56 +08:00
|
|
|
content := string(n.Tokens)
|
2024-11-27 20:13:22 +08:00
|
|
|
tmp := util.RemoveInvalid(content)
|
2023-02-16 13:14:15 +08:00
|
|
|
tmp = strings.TrimSpace(tmp)
|
|
|
|
|
if "" == tmp {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newContent := markReplaceSpanWithSplit(content, virtualBlockRefKeywords, search.GetMarkSpanStart(search.VirtualBlockRefDataType), search.GetMarkSpanEnd())
|
2022-10-26 09:50:56 +08:00
|
|
|
if content != newContent {
|
|
|
|
|
// 虚拟引用排除命中自身块命名和别名的情况 https://github.com/siyuan-note/siyuan/issues/3185
|
|
|
|
|
var blockKeys []string
|
|
|
|
|
if name := parentBlock.IALAttr("name"); "" != name {
|
|
|
|
|
blockKeys = append(blockKeys, name)
|
|
|
|
|
}
|
|
|
|
|
if alias := parentBlock.IALAttr("alias"); "" != alias {
|
|
|
|
|
blockKeys = append(blockKeys, alias)
|
|
|
|
|
}
|
|
|
|
|
if 0 < len(blockKeys) {
|
2023-02-16 13:14:15 +08:00
|
|
|
keys := gulu.Str.SubstringsBetween(newContent, search.GetMarkSpanStart(search.VirtualBlockRefDataType), search.GetMarkSpanEnd())
|
2022-10-26 09:50:56 +08:00
|
|
|
for _, k := range keys {
|
|
|
|
|
if gulu.Str.Contains(k, blockKeys) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
n.Tokens = []byte(newContent)
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-15 17:16:23 +08:00
|
|
|
// parseKeywords 将字符串转换为关键字切片,并剔除前后的空白字符
|
|
|
|
|
func parseKeywords(keywordsStr string) (keywords []string) {
|
|
|
|
|
keywords = []string{}
|
|
|
|
|
keywordsStr = strings.TrimSpace(keywordsStr)
|
|
|
|
|
if "" == keywordsStr {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// 先处理转义的逗号
|
|
|
|
|
keywordsStr = strings.ReplaceAll(keywordsStr, "\\,", "__comma@sep__")
|
|
|
|
|
// 再将连续或单个换行符替换为一个逗号,避免把 `\\\n` 转换为 `\,`
|
|
|
|
|
keywordsStr = newlineRegexp.ReplaceAllString(keywordsStr, ",")
|
|
|
|
|
// 按逗号分隔
|
|
|
|
|
for part := range strings.SplitSeq(keywordsStr, ",") {
|
|
|
|
|
part = strings.TrimSpace(part) // 剔除前后的空白字符
|
|
|
|
|
if "" == part {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
// 恢复转义的逗号
|
|
|
|
|
part = strings.ReplaceAll(part, "__comma@sep__", ",")
|
|
|
|
|
keywords = append(keywords, part)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-17 11:15:12 +08:00
|
|
|
func getVirtualRefKeywords(root *ast.Node) (ret []string) {
|
2022-10-26 09:50:56 +08:00
|
|
|
if !Conf.Editor.VirtualBlockRef {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-16 13:14:15 +08:00
|
|
|
if val, ok := virtualBlockRefCache.Get("virtual_ref"); ok {
|
2025-04-25 12:21:24 +08:00
|
|
|
ret = val.([]string)
|
2023-02-16 13:14:15 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-15 17:16:23 +08:00
|
|
|
includes := parseKeywords(Conf.Editor.VirtualBlockRefInclude)
|
|
|
|
|
if 0 < len(includes) {
|
2022-10-26 09:24:07 +08:00
|
|
|
ret = append(ret, includes...)
|
|
|
|
|
ret = gulu.Str.RemoveDuplicatedElem(ret)
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-15 17:16:23 +08:00
|
|
|
excludes := parseKeywords(Conf.Editor.VirtualBlockRefExclude)
|
|
|
|
|
if 0 < len(excludes) {
|
2022-11-24 12:15:08 +08:00
|
|
|
var tmp, regexps []string
|
2022-10-26 09:24:07 +08:00
|
|
|
for _, e := range excludes {
|
2022-11-24 12:15:08 +08:00
|
|
|
if strings.HasPrefix(e, "/") && strings.HasSuffix(e, "/") {
|
|
|
|
|
regexps = append(regexps, e[1:len(e)-1])
|
|
|
|
|
} else {
|
|
|
|
|
tmp = append(tmp, e)
|
|
|
|
|
}
|
2022-10-26 09:24:07 +08:00
|
|
|
}
|
|
|
|
|
excludes = tmp
|
|
|
|
|
ret = gulu.Str.ExcludeElem(ret, excludes)
|
2022-11-24 12:15:08 +08:00
|
|
|
if 0 < len(regexps) {
|
|
|
|
|
tmp = nil
|
2022-11-26 10:48:27 +08:00
|
|
|
for _, str := range ret {
|
2025-07-08 20:38:59 +08:00
|
|
|
matchExclude := false
|
2022-11-26 10:48:27 +08:00
|
|
|
for _, re := range regexps {
|
2025-07-08 20:38:59 +08:00
|
|
|
if ok, _ := regexp.MatchString(re, str); ok {
|
|
|
|
|
matchExclude = true
|
2022-11-24 12:15:08 +08:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-08 20:38:59 +08:00
|
|
|
if !matchExclude {
|
|
|
|
|
tmp = append(tmp, str)
|
|
|
|
|
}
|
2022-11-24 12:15:08 +08:00
|
|
|
}
|
|
|
|
|
ret = tmp
|
|
|
|
|
}
|
2022-10-26 09:24:07 +08:00
|
|
|
}
|
2022-10-26 09:50:56 +08:00
|
|
|
|
|
|
|
|
// 虚拟引用排除当前文档名 https://github.com/siyuan-note/siyuan/issues/4537
|
2023-09-17 11:15:12 +08:00
|
|
|
// Virtual references exclude the name and aliases from the current document https://github.com/siyuan-note/siyuan/issues/9204
|
|
|
|
|
title := root.IALAttr("title")
|
|
|
|
|
ret = gulu.Str.ExcludeElem(ret, []string{title})
|
|
|
|
|
if name := root.IALAttr("name"); "" != name {
|
|
|
|
|
ret = gulu.Str.ExcludeElem(ret, []string{name})
|
|
|
|
|
}
|
|
|
|
|
if alias := root.IALAttr("alias"); "" != alias {
|
|
|
|
|
for _, a := range strings.Split(alias, ",") {
|
|
|
|
|
ret = gulu.Str.ExcludeElem(ret, []string{a})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-26 09:50:56 +08:00
|
|
|
ret = prepareMarkKeywords(ret)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func prepareMarkKeywords(keywords []string) (ret []string) {
|
2022-11-16 10:33:30 +08:00
|
|
|
ret = gulu.Str.RemoveDuplicatedElem(keywords)
|
2023-05-01 14:15:00 +08:00
|
|
|
var tmp []string
|
|
|
|
|
for _, k := range ret {
|
2024-04-04 21:11:12 +08:00
|
|
|
if "" != k && "*" != k { // 提及和虚引排除 * Ignore `*` back mentions and virtual references https://github.com/siyuan-note/siyuan/issues/10873
|
2023-05-01 14:15:00 +08:00
|
|
|
tmp = append(tmp, k)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ret = tmp
|
|
|
|
|
|
2022-10-26 09:50:56 +08:00
|
|
|
sort.SliceStable(ret, func(i, j int) bool {
|
2022-11-16 10:33:30 +08:00
|
|
|
return len(ret[i]) > len(ret[j])
|
2022-10-26 09:50:56 +08:00
|
|
|
})
|
2022-10-26 09:24:07 +08:00
|
|
|
return
|
|
|
|
|
}
|