virutalref.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // SiYuan - Refactor your thinking
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package model
  17. import (
  18. "bytes"
  19. "regexp"
  20. "sort"
  21. "strings"
  22. "time"
  23. "github.com/88250/gulu"
  24. "github.com/88250/lute"
  25. "github.com/88250/lute/ast"
  26. "github.com/88250/lute/parse"
  27. "github.com/ClarkThan/ahocorasick"
  28. "github.com/dgraph-io/ristretto"
  29. "github.com/siyuan-note/siyuan/kernel/search"
  30. "github.com/siyuan-note/siyuan/kernel/sql"
  31. "github.com/siyuan-note/siyuan/kernel/task"
  32. "github.com/siyuan-note/siyuan/kernel/treenode"
  33. )
  34. // virtualBlockRefCache 用于保存块关联的虚拟引用关键字。
  35. // 改进打开虚拟引用后加载文档的性能 https://github.com/siyuan-note/siyuan/issues/7378
  36. var virtualBlockRefCache, _ = ristretto.NewCache(&ristretto.Config{
  37. NumCounters: 102400,
  38. MaxCost: 10240,
  39. BufferItems: 64,
  40. })
  41. func getBlockVirtualRefKeywords(root *ast.Node) (ret []string) {
  42. val, ok := virtualBlockRefCache.Get(root.ID)
  43. if !ok {
  44. buf := bytes.Buffer{}
  45. ast.Walk(root, func(n *ast.Node, entering bool) ast.WalkStatus {
  46. if !entering || !n.IsBlock() {
  47. return ast.WalkContinue
  48. }
  49. content := sql.NodeStaticContent(n, nil, false, false, false, GetBlockAttrsWithoutWaitWriting)
  50. buf.WriteString(content)
  51. return ast.WalkContinue
  52. })
  53. content := buf.String()
  54. ret = putBlockVirtualRefKeywords(content, root)
  55. return
  56. }
  57. ret = val.([]string)
  58. return
  59. }
  60. func putBlockVirtualRefKeywords(blockContent string, root *ast.Node) (ret []string) {
  61. keywords := getVirtualRefKeywords(root)
  62. if 1 > len(keywords) {
  63. return
  64. }
  65. contentTmp := blockContent
  66. var keywordsTmp []string
  67. if !Conf.Search.CaseSensitive {
  68. contentTmp = strings.ToLower(blockContent)
  69. for _, keyword := range keywords {
  70. keywordsTmp = append(keywordsTmp, strings.ToLower(keyword))
  71. }
  72. } else {
  73. for _, keyword := range keywords {
  74. keywordsTmp = append(keywordsTmp, keyword)
  75. }
  76. }
  77. m := ahocorasick.NewMatcher()
  78. m.BuildWithPatterns(keywordsTmp)
  79. hits := m.Search(contentTmp)
  80. for _, hit := range hits {
  81. ret = append(ret, hit)
  82. }
  83. if 1 > len(ret) {
  84. return
  85. }
  86. ret = gulu.Str.RemoveDuplicatedElem(ret)
  87. virtualBlockRefCache.SetWithTTL(root.ID, ret, 1, 10*time.Minute)
  88. return
  89. }
  90. func CacheVirtualBlockRefJob() {
  91. if !Conf.Editor.VirtualBlockRef {
  92. return
  93. }
  94. task.AppendTask(task.CacheVirtualBlockRef, ResetVirtualBlockRefCache)
  95. }
  96. func ResetVirtualBlockRefCache() {
  97. virtualBlockRefCache.Clear()
  98. if !Conf.Editor.VirtualBlockRef {
  99. return
  100. }
  101. keywords := sql.QueryVirtualRefKeywords(Conf.Search.VirtualRefName, Conf.Search.VirtualRefAlias, Conf.Search.VirtualRefAnchor, Conf.Search.VirtualRefDoc)
  102. virtualBlockRefCache.Set("virtual_ref", keywords, 1)
  103. }
  104. func AddVirtualBlockRefInclude(keyword []string) {
  105. if 1 > len(keyword) {
  106. return
  107. }
  108. include := strings.ReplaceAll(Conf.Editor.VirtualBlockRefInclude, "\\,", "__comma@sep__")
  109. includes := strings.Split(include, ",")
  110. includes = append(includes, keyword...)
  111. includes = gulu.Str.RemoveDuplicatedElem(includes)
  112. Conf.Editor.VirtualBlockRefInclude = strings.Join(includes, ",")
  113. Conf.Save()
  114. ResetVirtualBlockRefCache()
  115. }
  116. func AddVirtualBlockRefExclude(keyword []string) {
  117. if 1 > len(keyword) {
  118. return
  119. }
  120. exclude := strings.ReplaceAll(Conf.Editor.VirtualBlockRefExclude, "\\,", "__comma@sep__")
  121. excludes := strings.Split(exclude, ",")
  122. excludes = append(excludes, keyword...)
  123. excludes = gulu.Str.RemoveDuplicatedElem(excludes)
  124. Conf.Editor.VirtualBlockRefExclude = strings.Join(excludes, ",")
  125. Conf.Save()
  126. ResetVirtualBlockRefCache()
  127. }
  128. func processVirtualRef(n *ast.Node, unlinks *[]*ast.Node, virtualBlockRefKeywords []string, refCount map[string]int, luteEngine *lute.Lute) bool {
  129. if !Conf.Editor.VirtualBlockRef || 1 > len(virtualBlockRefKeywords) {
  130. return false
  131. }
  132. if ast.NodeText != n.Type {
  133. return false
  134. }
  135. parentBlock := treenode.ParentBlock(n)
  136. if nil == parentBlock {
  137. return false
  138. }
  139. if 0 < refCount[parentBlock.ID] {
  140. // 如果块被引用过,则将其自身的文本排除在虚拟引用关键字之外
  141. // Referenced blocks support rendering virtual references https://github.com/siyuan-note/siyuan/issues/10960
  142. parentText := getNodeRefText(parentBlock)
  143. virtualBlockRefKeywords = gulu.Str.RemoveElem(virtualBlockRefKeywords, parentText)
  144. }
  145. content := string(n.Tokens)
  146. tmp := gulu.Str.RemoveInvisible(content)
  147. tmp = strings.TrimSpace(tmp)
  148. if "" == tmp {
  149. return false
  150. }
  151. newContent := markReplaceSpanWithSplit(content, virtualBlockRefKeywords, search.GetMarkSpanStart(search.VirtualBlockRefDataType), search.GetMarkSpanEnd())
  152. if content != newContent {
  153. // 虚拟引用排除命中自身块命名和别名的情况 https://github.com/siyuan-note/siyuan/issues/3185
  154. var blockKeys []string
  155. if name := parentBlock.IALAttr("name"); "" != name {
  156. blockKeys = append(blockKeys, name)
  157. }
  158. if alias := parentBlock.IALAttr("alias"); "" != alias {
  159. blockKeys = append(blockKeys, alias)
  160. }
  161. if 0 < len(blockKeys) {
  162. keys := gulu.Str.SubstringsBetween(newContent, search.GetMarkSpanStart(search.VirtualBlockRefDataType), search.GetMarkSpanEnd())
  163. for _, k := range keys {
  164. if gulu.Str.Contains(k, blockKeys) {
  165. return true
  166. }
  167. }
  168. }
  169. // Wrong parsing virtual reference with `\` before it https://github.com/siyuan-note/siyuan/issues/7821
  170. newContent = strings.ReplaceAll(newContent, "\\"+search.GetMarkSpanStart(search.VirtualBlockRefDataType), "\\\\"+search.GetMarkSpanStart(search.VirtualBlockRefDataType))
  171. n.Tokens = []byte(newContent)
  172. linkTree := parse.Inline("", n.Tokens, luteEngine.ParseOptions)
  173. var children []*ast.Node
  174. for c := linkTree.Root.FirstChild.FirstChild; nil != c; c = c.Next {
  175. children = append(children, c)
  176. }
  177. for _, c := range children {
  178. n.InsertBefore(c)
  179. }
  180. *unlinks = append(*unlinks, n)
  181. return true
  182. }
  183. return false
  184. }
  185. func getVirtualRefKeywords(root *ast.Node) (ret []string) {
  186. if !Conf.Editor.VirtualBlockRef {
  187. return
  188. }
  189. if val, ok := virtualBlockRefCache.Get("virtual_ref"); ok {
  190. ret = val.([]string)
  191. }
  192. if "" != strings.TrimSpace(Conf.Editor.VirtualBlockRefInclude) {
  193. include := strings.ReplaceAll(Conf.Editor.VirtualBlockRefInclude, "\\,", "__comma@sep__")
  194. includes := strings.Split(include, ",")
  195. var tmp []string
  196. for _, e := range includes {
  197. e = strings.ReplaceAll(e, "__comma@sep__", ",")
  198. tmp = append(tmp, e)
  199. }
  200. includes = tmp
  201. ret = append(ret, includes...)
  202. ret = gulu.Str.RemoveDuplicatedElem(ret)
  203. }
  204. if "" != strings.TrimSpace(Conf.Editor.VirtualBlockRefExclude) {
  205. exclude := strings.ReplaceAll(Conf.Editor.VirtualBlockRefExclude, "\\,", "__comma@sep__")
  206. excludes := strings.Split(exclude, ",")
  207. var tmp, regexps []string
  208. for _, e := range excludes {
  209. e = strings.ReplaceAll(e, "__comma@sep__", ",")
  210. if strings.HasPrefix(e, "/") && strings.HasSuffix(e, "/") {
  211. regexps = append(regexps, e[1:len(e)-1])
  212. } else {
  213. tmp = append(tmp, e)
  214. }
  215. }
  216. excludes = tmp
  217. ret = gulu.Str.ExcludeElem(ret, excludes)
  218. if 0 < len(regexps) {
  219. tmp = nil
  220. for _, str := range ret {
  221. for _, re := range regexps {
  222. if ok, regErr := regexp.MatchString(re, str); !ok && nil == regErr {
  223. tmp = append(tmp, str)
  224. break
  225. }
  226. }
  227. }
  228. ret = tmp
  229. }
  230. }
  231. // 虚拟引用排除当前文档名 https://github.com/siyuan-note/siyuan/issues/4537
  232. // Virtual references exclude the name and aliases from the current document https://github.com/siyuan-note/siyuan/issues/9204
  233. title := root.IALAttr("title")
  234. ret = gulu.Str.ExcludeElem(ret, []string{title})
  235. if name := root.IALAttr("name"); "" != name {
  236. ret = gulu.Str.ExcludeElem(ret, []string{name})
  237. }
  238. if alias := root.IALAttr("alias"); "" != alias {
  239. for _, a := range strings.Split(alias, ",") {
  240. ret = gulu.Str.ExcludeElem(ret, []string{a})
  241. }
  242. }
  243. ret = prepareMarkKeywords(ret)
  244. return
  245. }
  246. func prepareMarkKeywords(keywords []string) (ret []string) {
  247. ret = gulu.Str.RemoveDuplicatedElem(keywords)
  248. var tmp []string
  249. for _, k := range ret {
  250. if "" != k && "*" != k { // 提及和虚引排除 * Ignore `*` back mentions and virtual references https://github.com/siyuan-note/siyuan/issues/10873
  251. tmp = append(tmp, k)
  252. }
  253. }
  254. ret = tmp
  255. sort.SliceStable(ret, func(i, j int) bool {
  256. return len(ret[i]) > len(ret[j])
  257. })
  258. return
  259. }