Bläddra i källkod

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

Vanessa 2 år sedan
förälder
incheckning
d7320c5f17

+ 1 - 0
app/appearance/langs/en_US.json

@@ -573,6 +573,7 @@
   "md4": "Automatically indent the beginning of paragraphs to conform to traditional Chinese typesetting habits",
   "md4": "Automatically indent the beginning of paragraphs to conform to traditional Chinese typesetting habits",
   "md7": "Whether to display the network picture corner mark",
   "md7": "Whether to display the network picture corner mark",
   "md8": "After enabling, if the picture is a web file (non-local asset file), it will display a corner mark",
   "md8": "After enabling, if the picture is a web file (non-local asset file), it will display a corner mark",
+  "md9": "Virtual Reference keyword inclusion list",
   "md12": "Whether to display bookmark, name, alias, memo and reference count",
   "md12": "Whether to display bookmark, name, alias, memo and reference count",
   "md16": "After enabling, if there is such information, it will be displayed on the upper right side of the block",
   "md16": "After enabling, if there is such information, it will be displayed on the upper right side of the block",
   "md27": "The code block shows the line number",
   "md27": "The code block shows the line number",

+ 1 - 0
app/appearance/langs/es_ES.json

@@ -573,6 +573,7 @@
   "md4": "Sangria automáticamente el comienzo de los párrafos para ajustarse a los hábitos tipográficos tradicionales chinos",
   "md4": "Sangria automáticamente el comienzo de los párrafos para ajustarse a los hábitos tipográficos tradicionales chinos",
   "md7": "Mostrar la marca de esquina de la imagen de red",
   "md7": "Mostrar la marca de esquina de la imagen de red",
   "md8": "Después de habilitarlo, si la imagen es un archivo de red (archivo de activos no locales), mostrará una marca de esquina",
   "md8": "Después de habilitarlo, si la imagen es un archivo de red (archivo de activos no locales), mostrará una marca de esquina",
+  "md9": "Lista de inclusión de palabras clave de referencia virtual",
   "md12": "Mostrar el marcador, el nombre, el alias, el memo y el recuento de referencias",
   "md12": "Mostrar el marcador, el nombre, el alias, el memo y el recuento de referencias",
   "md16": "Una vez habilitado, si existe dicha información, se mostrará en la parte superior derecha del bloque",
   "md16": "Una vez habilitado, si existe dicha información, se mostrará en la parte superior derecha del bloque",
   "md27": "El bloque de código muestra el número de línea",
   "md27": "El bloque de código muestra el número de línea",

+ 1 - 0
app/appearance/langs/fr_FR.json

@@ -573,6 +573,7 @@
   "md4": "indenter automatiquement le début des paragraphes pour se conformer aux habitudes de composition traditionnelles chinoises.",
   "md4": "indenter automatiquement le début des paragraphes pour se conformer aux habitudes de composition traditionnelles chinoises.",
   "md7": "Affichage ou non de la marque d'angle de l'image réseau",
   "md7": "Affichage ou non de la marque d'angle de l'image réseau",
   "md8": "Après l'activation, si l'image est un fichier web (fichier d'actif non local), une marque d'angle apparaîtra.",
   "md8": "Après l'activation, si l'image est un fichier web (fichier d'actif non local), une marque d'angle apparaîtra.",
+  "md9": "Liste d'inclusion de mots-clés de référence virtuelle",
   "md12": "Affichage ou non du nombre de signets, de noms, d'alias, de mémos et de références.",
   "md12": "Affichage ou non du nombre de signets, de noms, d'alias, de mémos et de références.",
   "md16": "Après l'activation, si de telles informations existent, elles seront affichées dans la partie haute à droite du bloc.",
   "md16": "Après l'activation, si de telles informations existent, elles seront affichées dans la partie haute à droite du bloc.",
   "md27": "Le bloc de code indique le numéro de ligne",
   "md27": "Le bloc de code indique le numéro de ligne",

+ 1 - 0
app/appearance/langs/zh_CHT.json

@@ -573,6 +573,7 @@
   "md4": "自動進行段首縮進,以符合傳統中文排版習慣",
   "md4": "自動進行段首縮進,以符合傳統中文排版習慣",
   "md7": "是否顯示網路圖片角標",
   "md7": "是否顯示網路圖片角標",
   "md8": "啟用後如果圖片是網路檔(非本地資源檔)則會顯示角標",
   "md8": "啟用後如果圖片是網路檔(非本地資源檔)則會顯示角標",
+  "md9": "虛擬引用關鍵字包含列表",
   "md12": "是否顯示書簽、命名、別名、備註和引用計數",
   "md12": "是否顯示書簽、命名、別名、備註和引用計數",
   "md16": "啟用後如果存在這些資訊則將在塊的右上側進行顯示",
   "md16": "啟用後如果存在這些資訊則將在塊的右上側進行顯示",
   "md27": "代碼塊顯示行號",
   "md27": "代碼塊顯示行號",

+ 1 - 0
app/appearance/langs/zh_CN.json

@@ -573,6 +573,7 @@
   "md4": "自动进行段首缩进,以符合传统中文排版习惯",
   "md4": "自动进行段首缩进,以符合传统中文排版习惯",
   "md7": "是否显示网络图片角标",
   "md7": "是否显示网络图片角标",
   "md8": "启用后如果图片是网络文件(非本地资源文件)则会显示角标",
   "md8": "启用后如果图片是网络文件(非本地资源文件)则会显示角标",
+  "md9": "虚拟引用关键字包含列表",
   "md12": "是否显示书签、命名、别名、备注和引用计数",
   "md12": "是否显示书签、命名、别名、备注和引用计数",
   "md16": "启用后如果存在这些信息则将在块的右上侧进行显示",
   "md16": "启用后如果存在这些信息则将在块的右上侧进行显示",
   "md27": "代码块显示行号",
   "md27": "代码块显示行号",

+ 9 - 0
app/src/config/editor.ts

@@ -140,6 +140,14 @@ export const editor = {
     <span class="fn__space"></span>
     <span class="fn__space"></span>
     <input class="b3-switch fn__flex-center" id="virtualBlockRef" type="checkbox"${window.siyuan.config.editor.virtualBlockRef ? " checked" : ""}/>
     <input class="b3-switch fn__flex-center" id="virtualBlockRef" type="checkbox"${window.siyuan.config.editor.virtualBlockRef ? " checked" : ""}/>
 </label>
 </label>
+<label class="fn__flex b3-label">
+    <div class="fn__flex-1">
+        ${window.siyuan.languages.md9}
+        <div class="b3-label__text">${window.siyuan.languages.md36}</div>
+    </div>
+    <span class="fn__space"></span>
+    <input class="b3-text-field fn__flex-center fn__size200" id="virtualBlockRefInclude" value="${window.siyuan.config.editor.virtualBlockRefInclude}" />
+</label>
 <label class="fn__flex b3-label">
 <label class="fn__flex b3-label">
     <div class="fn__flex-1">
     <div class="fn__flex-1">
         ${window.siyuan.languages.md35}
         ${window.siyuan.languages.md35}
@@ -248,6 +256,7 @@ export const editor = {
                 katexMacros: (editor.element.querySelector("#katexMacros") as HTMLTextAreaElement).value,
                 katexMacros: (editor.element.querySelector("#katexMacros") as HTMLTextAreaElement).value,
                 codeLineWrap: (editor.element.querySelector("#codeLineWrap") as HTMLInputElement).checked,
                 codeLineWrap: (editor.element.querySelector("#codeLineWrap") as HTMLInputElement).checked,
                 virtualBlockRef: (editor.element.querySelector("#virtualBlockRef") as HTMLInputElement).checked,
                 virtualBlockRef: (editor.element.querySelector("#virtualBlockRef") as HTMLInputElement).checked,
+                virtualBlockRefInclude: (editor.element.querySelector("#virtualBlockRefInclude") as HTMLInputElement).value,
                 virtualBlockRefExclude: (editor.element.querySelector("#virtualBlockRefExclude") as HTMLInputElement).value,
                 virtualBlockRefExclude: (editor.element.querySelector("#virtualBlockRefExclude") as HTMLInputElement).value,
                 blockRefDynamicAnchorTextMaxLen: parseInt((editor.element.querySelector("#blockRefDynamicAnchorTextMaxLen") as HTMLInputElement).value),
                 blockRefDynamicAnchorTextMaxLen: parseInt((editor.element.querySelector("#blockRefDynamicAnchorTextMaxLen") as HTMLInputElement).value),
                 codeLigatures: (editor.element.querySelector("#codeLigatures") as HTMLInputElement).checked,
                 codeLigatures: (editor.element.querySelector("#codeLigatures") as HTMLInputElement).checked,

+ 1 - 0
app/src/types/index.d.ts

@@ -249,6 +249,7 @@ declare interface IEditor {
     fontFamily: string;
     fontFamily: string;
     virtualBlockRef: string;
     virtualBlockRef: string;
     virtualBlockRefExclude: string;
     virtualBlockRefExclude: string;
+    virtualBlockRefInclude: string;
     blockRefDynamicAnchorTextMaxLen: number;
     blockRefDynamicAnchorTextMaxLen: number;
 
 
     emoji: string[];
     emoji: string[];

+ 1 - 0
kernel/conf/editor.go

@@ -30,6 +30,7 @@ type Editor struct {
 	Emoji                           []string `json:"emoji"`                           // 常用表情
 	Emoji                           []string `json:"emoji"`                           // 常用表情
 	VirtualBlockRef                 bool     `json:"virtualBlockRef"`                 // 是否启用虚拟引用
 	VirtualBlockRef                 bool     `json:"virtualBlockRef"`                 // 是否启用虚拟引用
 	VirtualBlockRefExclude          string   `json:"virtualBlockRefExclude"`          // 虚拟引用关键字排除列表
 	VirtualBlockRefExclude          string   `json:"virtualBlockRefExclude"`          // 虚拟引用关键字排除列表
+	VirtualBlockRefInclude          string   `json:"virtualBlockRefInclude"`          // 虚拟引用关键字包含列表
 	BlockRefDynamicAnchorTextMaxLen int      `json:"blockRefDynamicAnchorTextMaxLen"` // 块引动态锚文本最大长度
 	BlockRefDynamicAnchorTextMaxLen int      `json:"blockRefDynamicAnchorTextMaxLen"` // 块引动态锚文本最大长度
 	PlantUMLServePath               string   `json:"plantUMLServePath"`               // PlantUML 伺服地址
 	PlantUMLServePath               string   `json:"plantUMLServePath"`               // PlantUML 伺服地址
 	FullWidth                       bool     `json:"fullWidth"`                       // 是否使用最大宽度
 	FullWidth                       bool     `json:"fullWidth"`                       // 是否使用最大宽度

+ 2 - 3
kernel/model/backlink.go

@@ -301,14 +301,13 @@ 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 := markReplaceSpan(text, mentionKeywords, searchMarkSpanStart, searchMarkSpanEnd)
+					newText := markReplaceSpanWithSplit(text, mentionKeywords, searchMarkSpanStart, searchMarkSpanEnd)
 					if text == newText {
 					if text == newText {
 						return ast.WalkContinue
 						return ast.WalkContinue
 					}
 					}
 
 
 					n.Tokens = gulu.Str.ToBytes(newText)
 					n.Tokens = gulu.Str.ToBytes(newText)
 					if bytes.Contains(n.Tokens, []byte("search-mark")) {
 					if bytes.Contains(n.Tokens, []byte("search-mark")) {
-						n.Tokens = bytes.ReplaceAll(n.Tokens, []byte("\\"+searchMarkSpanStart), []byte("\\\\"+searchMarkSpanEnd))
 						n.Tokens = lex.EscapeMarkers(n.Tokens)
 						n.Tokens = lex.EscapeMarkers(n.Tokens)
 						linkTree := parse.Inline("", n.Tokens, luteEngine.ParseOptions)
 						linkTree := parse.Inline("", n.Tokens, luteEngine.ParseOptions)
 						var children []*ast.Node
 						var children []*ast.Node
@@ -794,7 +793,7 @@ func searchBackmention(mentionKeywords []string, keyword string, excludeBacklink
 			continue
 			continue
 		}
 		}
 
 
-		newText := markReplaceSpan(text, mentionKeywords, searchMarkSpanStart, searchMarkSpanEnd)
+		newText := markReplaceSpanWithSplit(text, mentionKeywords, searchMarkSpanStart, searchMarkSpanEnd)
 		if text != newText {
 		if text != newText {
 			tmp = append(tmp, b)
 			tmp = append(tmp, b)
 		}
 		}

+ 5 - 76
kernel/model/file.go

@@ -17,7 +17,6 @@
 package model
 package model
 
 
 import (
 import (
-	"bytes"
 	"errors"
 	"errors"
 	"fmt"
 	"fmt"
 	"io/fs"
 	"io/fs"
@@ -34,7 +33,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/html"
-	"github.com/88250/lute/lex"
 	"github.com/88250/lute/parse"
 	"github.com/88250/lute/parse"
 	util2 "github.com/88250/lute/util"
 	util2 "github.com/88250/lute/util"
 	"github.com/dustin/go-humanize"
 	"github.com/dustin/go-humanize"
@@ -598,27 +596,9 @@ func GetDoc(startID, endID, id string, index int, keyword string, mode int, size
 	} else {
 	} else {
 		nodes, eof = loadNodesByMode(node, inputIndex, mode, size, isDoc, isHeading)
 		nodes, eof = loadNodesByMode(node, inputIndex, mode, size, isDoc, isHeading)
 	}
 	}
-	refCount := sql.QueryRootChildrenRefCount(rootID)
 
 
-	var virtualBlockRefKeywords []string
-	if Conf.Editor.VirtualBlockRef {
-		virtualBlockRefKeywords = sql.QueryVirtualRefKeywords(Conf.Search.VirtualRefName, Conf.Search.VirtualRefAlias, Conf.Search.VirtualRefAnchor, Conf.Search.VirtualRefDoc)
-		if "" != strings.TrimSpace(Conf.Editor.VirtualBlockRefExclude) {
-			exclude := strings.ReplaceAll(Conf.Editor.VirtualBlockRefExclude, "\\,", "__comma@sep__")
-			excludes := strings.Split(exclude, ",")
-			var tmp []string
-			for _, e := range excludes {
-				e = strings.ReplaceAll(e, "__comma@sep__", ",")
-				tmp = append(tmp, e)
-			}
-			excludes = tmp
-			virtualBlockRefKeywords = gulu.Str.ExcludeElem(virtualBlockRefKeywords, excludes)
-		}
-
-		// 虚拟引用排除当前文档名 https://github.com/siyuan-note/siyuan/issues/4537
-		virtualBlockRefKeywords = gulu.Str.ExcludeElem(virtualBlockRefKeywords, []string{tree.Root.IALAttr("title")})
-		virtualBlockRefKeywords = prepareMarkKeywords(virtualBlockRefKeywords)
-	}
+	refCount := sql.QueryRootChildrenRefCount(rootID)
+	virtualBlockRefKeywords := getVirtualRefKeywords(tree.Root.IALAttr("title"))
 
 
 	subTree := &parse.Tree{ID: rootID, Root: &ast.Node{Type: ast.NodeDocument}, Marks: tree.Marks}
 	subTree := &parse.Tree{ID: rootID, Root: &ast.Node{Type: ast.NodeDocument}, Marks: tree.Marks}
 	keyword = strings.Join(strings.Split(keyword, " "), search.TermSep)
 	keyword = strings.Join(strings.Split(keyword, " "), search.TermSep)
@@ -660,65 +640,14 @@ func GetDoc(startID, endID, id string, index int, keyword string, mode int, size
 						}
 						}
 					}
 					}
 					if hitBlock {
 					if hitBlock {
-						// 搜索高亮
-						text := string(n.Tokens)
-						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 = bytes.ReplaceAll(n.Tokens, []byte("\\"+searchMarkSpanStart), []byte("\\\\"+searchMarkSpanEnd))
-							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)
+						if markReplaceSpan(n, &unlinks, string(n.Tokens), keywords, searchMarkSpanStart, searchMarkSpanEnd, luteEngine) {
 							return ast.WalkContinue
 							return ast.WalkContinue
 						}
 						}
 					}
 					}
 				}
 				}
 
 
-				// 虚拟引用
-				if Conf.Editor.VirtualBlockRef && 0 < len(virtualBlockRefKeywords) {
-					parentBlock := treenode.ParentBlock(n)
-					if nil != parentBlock && 1 > refCount[parentBlock.ID] {
-						content := string(n.Tokens)
-						newContent := markReplaceSpan(content, virtualBlockRefKeywords, virtualBlockRefSpanStart, virtualBlockRefSpanEnd)
-						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) {
-								keys := gulu.Str.SubstringsBetween(newContent, virtualBlockRefSpanStart, virtualBlockRefSpanEnd)
-								for _, k := range keys {
-									if gulu.Str.Contains(k, blockKeys) {
-										return ast.WalkContinue
-									}
-								}
-							}
-
-							n.Tokens = []byte(newContent)
-							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 ast.WalkContinue
-						}
-					}
+				if processVirtualRef(n, &unlinks, virtualBlockRefKeywords, refCount, luteEngine) {
+					return ast.WalkContinue
 				}
 				}
 			}
 			}
 			return ast.WalkContinue
 			return ast.WalkContinue

+ 1 - 18
kernel/model/history.go

@@ -17,7 +17,6 @@
 package model
 package model
 
 
 import (
 import (
-	"bytes"
 	"encoding/json"
 	"encoding/json"
 	"fmt"
 	"fmt"
 	"io/fs"
 	"io/fs"
@@ -32,7 +31,6 @@ import (
 	"github.com/88250/gulu"
 	"github.com/88250/gulu"
 	"github.com/88250/lute"
 	"github.com/88250/lute"
 	"github.com/88250/lute/ast"
 	"github.com/88250/lute/ast"
-	"github.com/88250/lute/lex"
 	"github.com/88250/lute/parse"
 	"github.com/88250/lute/parse"
 	"github.com/88250/lute/render"
 	"github.com/88250/lute/render"
 	"github.com/siyuan-note/filelock"
 	"github.com/siyuan-note/filelock"
@@ -181,22 +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) {
-					// 搜索高亮
-					text := string(n.Tokens)
-					text = search.EncloseHighlighting(text, keywords, searchMarkSpanStart, searchMarkSpanEnd, false)
-					n.Tokens = gulu.Str.ToBytes(text)
-					if bytes.Contains(n.Tokens, []byte("search-mark")) {
-						n.Tokens = bytes.ReplaceAll(n.Tokens, []byte("\\"+searchMarkSpanStart), []byte("\\\\"+searchMarkSpanStart))
-						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)
+					if markReplaceSpan(n, &unlinks, string(n.Tokens), keywords, searchMarkSpanStart, searchMarkSpanEnd, luteEngine) {
 						return ast.WalkContinue
 						return ast.WalkContinue
 					}
 					}
 				}
 				}

+ 20 - 13
kernel/model/search.go

@@ -19,15 +19,16 @@ package model
 import (
 import (
 	"bytes"
 	"bytes"
 	"path"
 	"path"
-	"sort"
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
 	"time"
 	"time"
 	"unicode/utf8"
 	"unicode/utf8"
 
 
 	"github.com/88250/gulu"
 	"github.com/88250/gulu"
+	"github.com/88250/lute"
 	"github.com/88250/lute/ast"
 	"github.com/88250/lute/ast"
 	"github.com/88250/lute/html"
 	"github.com/88250/lute/html"
+	"github.com/88250/lute/lex"
 	"github.com/88250/lute/parse"
 	"github.com/88250/lute/parse"
 	"github.com/jinzhu/copier"
 	"github.com/jinzhu/copier"
 	"github.com/siyuan-note/logging"
 	"github.com/siyuan-note/logging"
@@ -642,22 +643,28 @@ func stringQuery(query string) string {
 	return strings.TrimSpace(buf.String())
 	return strings.TrimSpace(buf.String())
 }
 }
 
 
-func prepareMarkKeywords(keywords []string) (ret []string) {
-	keywords = gulu.Str.RemoveDuplicatedElem(keywords)
-	for _, k := range keywords {
-		if strings.ContainsAny(k, "?*!@#$%^&()[]{}\\|;:'\",.<>~`") {
-			continue
+// 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)
+		}
+		for _, c := range children {
+			n.InsertBefore(c)
 		}
 		}
-		ret = append(ret, k)
+		*unlinks = append(*unlinks, n)
+		return true
 	}
 	}
-
-	sort.SliceStable(ret, func(i, j int) bool {
-		return len(ret[i]) < len(ret[j])
-	})
-	return
+	return false
 }
 }
 
 
-func markReplaceSpan(text string, keywords []string, replacementStart, replacementEnd string) (ret string) {
+// markReplaceSpanWithSplit 用于处理虚拟引用和反链提及高亮。
+func markReplaceSpanWithSplit(text string, keywords []string, replacementStart, replacementEnd string) (ret string) {
 	// 调用该函数前参数 keywords 必须使用 prepareMarkKeywords 函数进行预处理
 	// 调用该函数前参数 keywords 必须使用 prepareMarkKeywords 函数进行预处理
 
 
 	parts := strings.Split(text, " ")
 	parts := strings.Split(text, " ")

+ 128 - 0
kernel/model/virutalref.go

@@ -0,0 +1,128 @@
+// SiYuan - Build Your Eternal Digital Garden
+// 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 (
+	"sort"
+	"strings"
+
+	"github.com/88250/gulu"
+	"github.com/88250/lute"
+	"github.com/88250/lute/ast"
+	"github.com/88250/lute/lex"
+	"github.com/88250/lute/parse"
+	"github.com/siyuan-note/siyuan/kernel/sql"
+	"github.com/siyuan-note/siyuan/kernel/treenode"
+)
+
+func processVirtualRef(n *ast.Node, unlinks *[]*ast.Node, virtualBlockRefKeywords []string, refCount map[string]int, luteEngine *lute.Lute) bool {
+	if !Conf.Editor.VirtualBlockRef || 1 > len(virtualBlockRefKeywords) {
+		return false
+	}
+
+	parentBlock := treenode.ParentBlock(n)
+	if nil == parentBlock || 0 < refCount[parentBlock.ID] {
+		return false
+	}
+
+	content := string(n.Tokens)
+	newContent := markReplaceSpanWithSplit(content, virtualBlockRefKeywords, virtualBlockRefSpanStart, virtualBlockRefSpanEnd)
+	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) {
+			keys := gulu.Str.SubstringsBetween(newContent, virtualBlockRefSpanStart, virtualBlockRefSpanEnd)
+			for _, k := range keys {
+				if gulu.Str.Contains(k, blockKeys) {
+					return true
+				}
+			}
+		}
+
+		n.Tokens = []byte(newContent)
+		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
+	}
+	return false
+}
+
+func getVirtualRefKeywords(docName string) (ret []string) {
+	if !Conf.Editor.VirtualBlockRef {
+		return
+	}
+
+	ret = sql.QueryVirtualRefKeywords(Conf.Search.VirtualRefName, Conf.Search.VirtualRefAlias, Conf.Search.VirtualRefAnchor, Conf.Search.VirtualRefDoc)
+	if "" != strings.TrimSpace(Conf.Editor.VirtualBlockRefInclude) {
+		include := strings.ReplaceAll(Conf.Editor.VirtualBlockRefInclude, "\\,", "__comma@sep__")
+		includes := strings.Split(include, ",")
+		var tmp []string
+		for _, e := range includes {
+			e = strings.ReplaceAll(e, "__comma@sep__", ",")
+			tmp = append(tmp, e)
+		}
+		includes = tmp
+		ret = append(ret, includes...)
+		ret = gulu.Str.RemoveDuplicatedElem(ret)
+	}
+
+	if "" != strings.TrimSpace(Conf.Editor.VirtualBlockRefExclude) {
+		exclude := strings.ReplaceAll(Conf.Editor.VirtualBlockRefExclude, "\\,", "__comma@sep__")
+		excludes := strings.Split(exclude, ",")
+		var tmp []string
+		for _, e := range excludes {
+			e = strings.ReplaceAll(e, "__comma@sep__", ",")
+			tmp = append(tmp, e)
+		}
+		excludes = tmp
+		ret = gulu.Str.ExcludeElem(ret, excludes)
+	}
+
+	// 虚拟引用排除当前文档名 https://github.com/siyuan-note/siyuan/issues/4537
+	ret = gulu.Str.ExcludeElem(ret, []string{docName})
+	ret = prepareMarkKeywords(ret)
+	return
+}
+
+func prepareMarkKeywords(keywords []string) (ret []string) {
+	keywords = gulu.Str.RemoveDuplicatedElem(keywords)
+	for _, k := range keywords {
+		if strings.ContainsAny(k, "?*!@#$%^&()[]{}\\|;:'\",.<>~`") {
+			continue
+		}
+		ret = append(ret, k)
+	}
+
+	sort.SliceStable(ret, func(i, j int) bool {
+		return len(ret[i]) < len(ret[j])
+	})
+	return
+}