tree.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // SiYuan - Build Your Eternal Digital Garden
  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. "errors"
  19. "io/fs"
  20. "path"
  21. "path/filepath"
  22. "strings"
  23. "time"
  24. "github.com/88250/lute"
  25. "github.com/88250/lute/ast"
  26. "github.com/88250/lute/parse"
  27. "github.com/siyuan-note/filelock"
  28. "github.com/siyuan-note/logging"
  29. "github.com/siyuan-note/siyuan/kernel/filesys"
  30. "github.com/siyuan-note/siyuan/kernel/treenode"
  31. "github.com/siyuan-note/siyuan/kernel/util"
  32. )
  33. func resetTree(tree *parse.Tree, titleSuffix string) {
  34. tree.ID = ast.NewNodeID()
  35. tree.Root.ID = tree.ID
  36. if t, parseErr := time.Parse("20060102150405", util.TimeFromID(tree.ID)); nil == parseErr {
  37. titleSuffix += " " + t.Format("2006-01-02 15:04:05")
  38. } else {
  39. titleSuffix = "Duplicated " + time.Now().Format("2006-01-02 15:04:05")
  40. }
  41. titleSuffix = "(" + titleSuffix + ")"
  42. tree.Root.SetIALAttr("id", tree.ID)
  43. tree.Root.SetIALAttr("title", tree.Root.IALAttr("title")+" "+titleSuffix)
  44. tree.Root.RemoveIALAttr("scroll")
  45. p := path.Join(path.Dir(tree.Path), tree.ID) + ".sy"
  46. tree.Path = p
  47. tree.HPath = tree.HPath + " " + titleSuffix
  48. // 收集所有引用
  49. refIDs := map[string]string{}
  50. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  51. if !entering || !treenode.IsBlockRef(n) {
  52. return ast.WalkContinue
  53. }
  54. defID, _, _ := treenode.GetBlockRef(n)
  55. if "" == defID {
  56. return ast.WalkContinue
  57. }
  58. refIDs[defID] = "1"
  59. return ast.WalkContinue
  60. })
  61. // 重置块 ID
  62. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  63. if !entering || ast.NodeDocument == n.Type {
  64. return ast.WalkContinue
  65. }
  66. if n.IsBlock() && "" != n.ID {
  67. newID := ast.NewNodeID()
  68. if "1" == refIDs[n.ID] {
  69. // 如果是文档自身的内部引用
  70. refIDs[n.ID] = newID
  71. }
  72. n.ID = newID
  73. n.SetIALAttr("id", n.ID)
  74. }
  75. return ast.WalkContinue
  76. })
  77. // 重置内部引用
  78. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  79. if !entering || !treenode.IsBlockRef(n) {
  80. return ast.WalkContinue
  81. }
  82. defID, _, _ := treenode.GetBlockRef(n)
  83. if "" == defID {
  84. return ast.WalkContinue
  85. }
  86. if "1" != refIDs[defID] {
  87. if ast.NodeTextMark == n.Type {
  88. n.TextMarkBlockRefID = refIDs[defID]
  89. }
  90. }
  91. return ast.WalkContinue
  92. })
  93. }
  94. func pagedPaths(localPath string, pageSize int) (ret map[int][]string) {
  95. ret = map[int][]string{}
  96. page := 1
  97. filepath.Walk(localPath, func(path string, info fs.FileInfo, err error) error {
  98. if info.IsDir() && strings.HasPrefix(info.Name(), ".") {
  99. return filepath.SkipDir
  100. }
  101. if !strings.HasSuffix(info.Name(), ".sy") {
  102. return nil
  103. }
  104. ret[page] = append(ret[page], path)
  105. if pageSize <= len(ret[page]) {
  106. page++
  107. }
  108. return nil
  109. })
  110. return
  111. }
  112. func loadTree(localPath string, luteEngine *lute.Lute) (ret *parse.Tree, err error) {
  113. data, err := filelock.ReadFile(localPath)
  114. if nil != err {
  115. logging.LogErrorf("get data [path=%s] failed: %s", localPath, err)
  116. return
  117. }
  118. ret, err = parse.ParseJSONWithoutFix(data, luteEngine.ParseOptions)
  119. if nil != err {
  120. logging.LogErrorf("parse json to tree [%s] failed: %s", localPath, err)
  121. return
  122. }
  123. return
  124. }
  125. var ErrBoxNotFound = errors.New("notebook not found")
  126. var ErrBlockNotFound = errors.New("block not found")
  127. var ErrTreeNotFound = errors.New("tree not found")
  128. func loadTreeByBlockID(id string) (ret *parse.Tree, err error) {
  129. if "" == id {
  130. return nil, ErrTreeNotFound
  131. }
  132. bt := treenode.GetBlockTree(id)
  133. if nil == bt {
  134. return nil, ErrBlockNotFound
  135. }
  136. ret, err = LoadTree(bt.BoxID, bt.Path)
  137. if nil != err {
  138. return
  139. }
  140. return
  141. }
  142. func LoadTree(boxID, p string) (*parse.Tree, error) {
  143. luteEngine := NewLute()
  144. tree, err := filesys.LoadTree(boxID, p, luteEngine)
  145. if nil != err {
  146. logging.LogErrorf("load tree [%s] failed: %s", boxID+p, err)
  147. return nil, err
  148. }
  149. return tree, nil
  150. }