block.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 sql
  17. import (
  18. "bytes"
  19. "database/sql"
  20. "strings"
  21. "github.com/88250/gulu"
  22. "github.com/88250/lute/ast"
  23. "github.com/88250/lute/html"
  24. "github.com/88250/lute/lex"
  25. "github.com/siyuan-note/siyuan/kernel/av"
  26. "github.com/siyuan-note/siyuan/kernel/cache"
  27. "github.com/siyuan-note/siyuan/kernel/filesys"
  28. "github.com/siyuan-note/siyuan/kernel/treenode"
  29. "github.com/siyuan-note/siyuan/kernel/util"
  30. )
  31. type Block struct {
  32. ID string
  33. ParentID string
  34. RootID string
  35. Hash string
  36. Box string
  37. Path string
  38. HPath string
  39. Name string
  40. Alias string
  41. Memo string
  42. Tag string
  43. Content string
  44. FContent string
  45. Markdown string
  46. Length int
  47. Type string
  48. SubType string
  49. IAL string
  50. Sort int
  51. Created string
  52. Updated string
  53. }
  54. func updateRootContent(tx *sql.Tx, content, updated, id string) (err error) {
  55. stmt := "UPDATE blocks SET content = ?, fcontent = ?, updated = ? WHERE id = ?"
  56. if err = execStmtTx(tx, stmt, content, content, updated, id); nil != err {
  57. return
  58. }
  59. stmt = "UPDATE blocks_fts SET content = ?, fcontent = ?, updated = ? WHERE id = ?"
  60. if err = execStmtTx(tx, stmt, content, content, updated, id); nil != err {
  61. return
  62. }
  63. if !caseSensitive {
  64. stmt = "UPDATE blocks_fts_case_insensitive SET content = ?, fcontent = ?, updated = ? WHERE id = ?"
  65. if err = execStmtTx(tx, stmt, content, content, updated, id); nil != err {
  66. return
  67. }
  68. }
  69. removeBlockCache(id)
  70. cache.RemoveBlockIAL(id)
  71. return
  72. }
  73. func updateBlockContent(tx *sql.Tx, block *Block) (err error) {
  74. stmt := "UPDATE blocks SET content = ? WHERE id = ?"
  75. if err = execStmtTx(tx, stmt, block.Content, block.ID); nil != err {
  76. tx.Rollback()
  77. return
  78. }
  79. stmt = "UPDATE blocks_fts SET content = ? WHERE id = ?"
  80. if err = execStmtTx(tx, stmt, block.Content, block.ID); nil != err {
  81. tx.Rollback()
  82. return
  83. }
  84. if !caseSensitive {
  85. stmt = "UPDATE blocks_fts_case_insensitive SET content = ? WHERE id = ?"
  86. if err = execStmtTx(tx, stmt, block.Content, block.ID); nil != err {
  87. tx.Rollback()
  88. return
  89. }
  90. }
  91. putBlockCache(block)
  92. return
  93. }
  94. func indexNode(tx *sql.Tx, id string) (err error) {
  95. bt := treenode.GetBlockTree(id)
  96. if nil == bt {
  97. return
  98. }
  99. luteEngine := util.NewLute()
  100. tree, _ := filesys.LoadTree(bt.BoxID, bt.Path, luteEngine)
  101. if nil == tree {
  102. return
  103. }
  104. node := treenode.GetNodeInTree(tree, id)
  105. if nil == node {
  106. return
  107. }
  108. content := NodeStaticContent(node, nil, true, indexAssetPath, true, nil)
  109. stmt := "UPDATE blocks SET content = ? WHERE id = ?"
  110. if err = execStmtTx(tx, stmt, content, id); nil != err {
  111. tx.Rollback()
  112. return
  113. }
  114. stmt = "UPDATE blocks_fts SET content = ? WHERE id = ?"
  115. if err = execStmtTx(tx, stmt, content, id); nil != err {
  116. tx.Rollback()
  117. return
  118. }
  119. if !caseSensitive {
  120. stmt = "UPDATE blocks_fts_case_insensitive SET content = ? WHERE id = ?"
  121. if err = execStmtTx(tx, stmt, content, id); nil != err {
  122. tx.Rollback()
  123. return
  124. }
  125. }
  126. return
  127. }
  128. func NodeStaticContent(node *ast.Node, excludeTypes []string, includeTextMarkATitleURL, includeAssetPath, fullAttrView bool,
  129. GetBlockAttrsWithoutWaitWriting func(id string) (ret map[string]string)) string {
  130. if nil == node {
  131. return ""
  132. }
  133. if ast.NodeAttributeView == node.Type {
  134. if fullAttrView {
  135. return getAttributeViewContent(node.AttributeViewID, GetBlockAttrsWithoutWaitWriting)
  136. }
  137. ret, _ := av.GetAttributeViewName(node.AttributeViewID)
  138. return ret
  139. }
  140. return nodeStaticContent(node, excludeTypes, includeTextMarkATitleURL, includeAssetPath)
  141. }
  142. func nodeStaticContent(node *ast.Node, excludeTypes []string, includeTextMarkATitleURL, includeAssetPath bool) string {
  143. if nil == node {
  144. return ""
  145. }
  146. if ast.NodeDocument == node.Type {
  147. return node.IALAttr("title")
  148. }
  149. if ast.NodeAttributeView == node.Type {
  150. return ""
  151. }
  152. buf := bytes.Buffer{}
  153. buf.Grow(4096)
  154. lastSpace := false
  155. ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
  156. if !entering {
  157. return ast.WalkContinue
  158. }
  159. if n.IsContainerBlock() {
  160. if !lastSpace {
  161. buf.WriteByte(' ')
  162. lastSpace = true
  163. }
  164. return ast.WalkContinue
  165. }
  166. if gulu.Str.Contains(n.Type.String(), excludeTypes) {
  167. return ast.WalkContinue
  168. }
  169. switch n.Type {
  170. case ast.NodeTableCell:
  171. // 表格块写入数据库表时在单元格之间添加空格 https://github.com/siyuan-note/siyuan/issues/7654
  172. if 0 < buf.Len() && ' ' != buf.Bytes()[buf.Len()-1] {
  173. buf.WriteByte(' ')
  174. }
  175. case ast.NodeImage:
  176. linkDest := n.ChildByType(ast.NodeLinkDest)
  177. var linkDestStr, ocrText string
  178. if nil != linkDest {
  179. linkDestStr = linkDest.TokensStr()
  180. ocrText = util.GetAssetText(linkDestStr)
  181. }
  182. linkText := n.ChildByType(ast.NodeLinkText)
  183. if nil != linkText {
  184. buf.Write(linkText.Tokens)
  185. buf.WriteByte(' ')
  186. }
  187. if "" != ocrText {
  188. buf.WriteString(ocrText)
  189. buf.WriteByte(' ')
  190. }
  191. if nil != linkDest {
  192. if !bytes.HasPrefix(linkDest.Tokens, []byte("assets/")) || includeAssetPath {
  193. buf.Write(linkDest.Tokens)
  194. buf.WriteByte(' ')
  195. }
  196. }
  197. if linkTitle := n.ChildByType(ast.NodeLinkTitle); nil != linkTitle {
  198. buf.Write(linkTitle.Tokens)
  199. }
  200. return ast.WalkSkipChildren
  201. case ast.NodeLinkText:
  202. buf.Write(n.Tokens)
  203. buf.WriteByte(' ')
  204. case ast.NodeLinkDest:
  205. buf.Write(n.Tokens)
  206. buf.WriteByte(' ')
  207. case ast.NodeLinkTitle:
  208. buf.Write(n.Tokens)
  209. case ast.NodeText, ast.NodeCodeBlockCode, ast.NodeMathBlockContent, ast.NodeHTMLBlock:
  210. tokens := n.Tokens
  211. if treenode.IsChartCodeBlockCode(n) {
  212. // 图表块的内容在数据库 `blocks` 表 `content` 字段中被转义 https://github.com/siyuan-note/siyuan/issues/6326
  213. tokens = html.UnescapeHTML(tokens)
  214. }
  215. buf.Write(tokens)
  216. case ast.NodeTextMark:
  217. for _, excludeType := range excludeTypes {
  218. if strings.HasPrefix(excludeType, "NodeTextMark-") {
  219. if n.IsTextMarkType(excludeType[len("NodeTextMark-"):]) {
  220. return ast.WalkContinue
  221. }
  222. }
  223. }
  224. if n.IsTextMarkType("tag") {
  225. buf.WriteByte('#')
  226. }
  227. buf.WriteString(n.Content())
  228. if n.IsTextMarkType("tag") {
  229. buf.WriteByte('#')
  230. }
  231. if n.IsTextMarkType("a") && includeTextMarkATitleURL {
  232. // 搜索不到超链接元素的 URL 和标题 https://github.com/siyuan-note/siyuan/issues/7352
  233. if "" != n.TextMarkATitle {
  234. buf.WriteString(" " + html.UnescapeHTMLStr(n.TextMarkATitle))
  235. }
  236. if !strings.HasPrefix(n.TextMarkAHref, "assets/") || includeAssetPath {
  237. buf.WriteString(" " + html.UnescapeHTMLStr(n.TextMarkAHref))
  238. }
  239. }
  240. case ast.NodeBackslash:
  241. buf.WriteByte(lex.ItemBackslash)
  242. case ast.NodeBackslashContent:
  243. buf.Write(n.Tokens)
  244. case ast.NodeAudio, ast.NodeVideo:
  245. buf.WriteString(treenode.GetNodeSrcTokens(n))
  246. buf.WriteByte(' ')
  247. }
  248. lastSpace = false
  249. return ast.WalkContinue
  250. })
  251. // 这里不要 trim,否则无法搜索首尾空格
  252. // Improve search and replace for spaces https://github.com/siyuan-note/siyuan/issues/10231
  253. return buf.String()
  254. }