virutalref.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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/treenode"
  32. )
  33. // virtualBlockRefCache 用于保存块关联的虚拟引用关键字。
  34. // 改进打开虚拟引用后加载文档的性能 https://github.com/siyuan-note/siyuan/issues/7378
  35. var virtualBlockRefCache, _ = ristretto.NewCache(&ristretto.Config{
  36. NumCounters: 102400,
  37. MaxCost: 10240,
  38. BufferItems: 64,
  39. })
  40. func getBlockVirtualRefKeywords(root *ast.Node) (ret []string) {
  41. val, ok := virtualBlockRefCache.Get(root.ID)
  42. if !ok {
  43. buf := bytes.Buffer{}
  44. ast.Walk(root, func(n *ast.Node, entering bool) ast.WalkStatus {
  45. if !entering || !n.IsBlock() {
  46. return ast.WalkContinue
  47. }
  48. content := treenode.NodeStaticContent(n, nil, false, false)
  49. buf.WriteString(content)
  50. return ast.WalkContinue
  51. })
  52. content := buf.String()
  53. ret = putBlockVirtualRefKeywords(content, root.ID, root.IALAttr("title"))
  54. return
  55. }
  56. ret = val.([]string)
  57. return
  58. }
  59. func putBlockVirtualRefKeywords(blockContent, blockID, docTitle string) (ret []string) {
  60. keywords := getVirtualRefKeywords(docTitle)
  61. if 1 > len(keywords) {
  62. return
  63. }
  64. contentTmp := blockContent
  65. var keywordsTmp []string
  66. if !Conf.Search.CaseSensitive {
  67. contentTmp = strings.ToLower(blockContent)
  68. for _, keyword := range keywords {
  69. keywordsTmp = append(keywordsTmp, strings.ToLower(keyword))
  70. }
  71. } else {
  72. for _, keyword := range keywords {
  73. keywordsTmp = append(keywordsTmp, keyword)
  74. }
  75. }
  76. m := ahocorasick.NewMatcher()
  77. m.BuildWithPatterns(keywordsTmp)
  78. hits := m.Search(contentTmp)
  79. for _, hit := range hits {
  80. ret = append(ret, hit)
  81. }
  82. if 1 > len(ret) {
  83. return
  84. }
  85. ret = gulu.Str.RemoveDuplicatedElem(ret)
  86. virtualBlockRefCache.SetWithTTL(blockID, ret, 1, 10*time.Minute)
  87. return
  88. }
  89. func CacheVirtualBlockRefJob() {
  90. virtualBlockRefCache.Del("virtual_ref")
  91. if !Conf.Editor.VirtualBlockRef {
  92. return
  93. }
  94. keywords := sql.QueryVirtualRefKeywords(Conf.Search.VirtualRefName, Conf.Search.VirtualRefAlias, Conf.Search.VirtualRefAnchor, Conf.Search.VirtualRefDoc)
  95. virtualBlockRefCache.Set("virtual_ref", keywords, 1)
  96. }
  97. func ResetVirtualBlockRefCache() {
  98. virtualBlockRefCache.Clear()
  99. CacheVirtualBlockRefJob()
  100. }
  101. func processVirtualRef(n *ast.Node, unlinks *[]*ast.Node, virtualBlockRefKeywords []string, refCount map[string]int, luteEngine *lute.Lute) bool {
  102. if !Conf.Editor.VirtualBlockRef {
  103. return false
  104. }
  105. if ast.NodeText != n.Type {
  106. return false
  107. }
  108. parentBlock := treenode.ParentBlock(n)
  109. if nil == parentBlock || 0 < refCount[parentBlock.ID] {
  110. return false
  111. }
  112. if 1 > len(virtualBlockRefKeywords) {
  113. return false
  114. }
  115. content := string(n.Tokens)
  116. tmp := gulu.Str.RemoveInvisible(content)
  117. tmp = strings.TrimSpace(tmp)
  118. if "" == tmp {
  119. return false
  120. }
  121. newContent := markReplaceSpanWithSplit(content, virtualBlockRefKeywords, search.GetMarkSpanStart(search.VirtualBlockRefDataType), search.GetMarkSpanEnd())
  122. if content != newContent {
  123. // 虚拟引用排除命中自身块命名和别名的情况 https://github.com/siyuan-note/siyuan/issues/3185
  124. var blockKeys []string
  125. if name := parentBlock.IALAttr("name"); "" != name {
  126. blockKeys = append(blockKeys, name)
  127. }
  128. if alias := parentBlock.IALAttr("alias"); "" != alias {
  129. blockKeys = append(blockKeys, alias)
  130. }
  131. if 0 < len(blockKeys) {
  132. keys := gulu.Str.SubstringsBetween(newContent, search.GetMarkSpanStart(search.VirtualBlockRefDataType), search.GetMarkSpanEnd())
  133. for _, k := range keys {
  134. if gulu.Str.Contains(k, blockKeys) {
  135. return true
  136. }
  137. }
  138. }
  139. // Wrong parsing virtual reference with `\` before it https://github.com/siyuan-note/siyuan/issues/7821
  140. newContent = strings.ReplaceAll(newContent, "\\"+search.GetMarkSpanStart(search.VirtualBlockRefDataType), "\\\\"+search.GetMarkSpanStart(search.VirtualBlockRefDataType))
  141. n.Tokens = []byte(newContent)
  142. linkTree := parse.Inline("", n.Tokens, luteEngine.ParseOptions)
  143. var children []*ast.Node
  144. for c := linkTree.Root.FirstChild.FirstChild; nil != c; c = c.Next {
  145. children = append(children, c)
  146. }
  147. for _, c := range children {
  148. n.InsertBefore(c)
  149. }
  150. *unlinks = append(*unlinks, n)
  151. return true
  152. }
  153. return false
  154. }
  155. func getVirtualRefKeywords(docName string) (ret []string) {
  156. if !Conf.Editor.VirtualBlockRef {
  157. return
  158. }
  159. if val, ok := virtualBlockRefCache.Get("virtual_ref"); ok {
  160. ret = val.([]string)
  161. }
  162. if "" != strings.TrimSpace(Conf.Editor.VirtualBlockRefInclude) {
  163. include := strings.ReplaceAll(Conf.Editor.VirtualBlockRefInclude, "\\,", "__comma@sep__")
  164. includes := strings.Split(include, ",")
  165. var tmp []string
  166. for _, e := range includes {
  167. e = strings.ReplaceAll(e, "__comma@sep__", ",")
  168. tmp = append(tmp, e)
  169. }
  170. includes = tmp
  171. ret = append(ret, includes...)
  172. ret = gulu.Str.RemoveDuplicatedElem(ret)
  173. }
  174. if "" != strings.TrimSpace(Conf.Editor.VirtualBlockRefExclude) {
  175. exclude := strings.ReplaceAll(Conf.Editor.VirtualBlockRefExclude, "\\,", "__comma@sep__")
  176. excludes := strings.Split(exclude, ",")
  177. var tmp, regexps []string
  178. for _, e := range excludes {
  179. e = strings.ReplaceAll(e, "__comma@sep__", ",")
  180. if strings.HasPrefix(e, "/") && strings.HasSuffix(e, "/") {
  181. regexps = append(regexps, e[1:len(e)-1])
  182. } else {
  183. tmp = append(tmp, e)
  184. }
  185. }
  186. excludes = tmp
  187. ret = gulu.Str.ExcludeElem(ret, excludes)
  188. if 0 < len(regexps) {
  189. tmp = nil
  190. for _, str := range ret {
  191. for _, re := range regexps {
  192. if ok, regErr := regexp.MatchString(re, str); !ok && nil == regErr {
  193. tmp = append(tmp, str)
  194. break
  195. }
  196. }
  197. }
  198. ret = tmp
  199. }
  200. }
  201. // 虚拟引用排除当前文档名 https://github.com/siyuan-note/siyuan/issues/4537
  202. ret = gulu.Str.ExcludeElem(ret, []string{docName})
  203. ret = prepareMarkKeywords(ret)
  204. return
  205. }
  206. func prepareMarkKeywords(keywords []string) (ret []string) {
  207. ret = gulu.Str.RemoveDuplicatedElem(keywords)
  208. var tmp []string
  209. for _, k := range ret {
  210. if "" != k {
  211. tmp = append(tmp, k)
  212. }
  213. }
  214. ret = tmp
  215. sort.SliceStable(ret, func(i, j int) bool {
  216. return len(ret[i]) > len(ret[j])
  217. })
  218. return
  219. }