path.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. "os"
  20. "path"
  21. "path/filepath"
  22. "sort"
  23. "strings"
  24. "github.com/88250/lute/ast"
  25. "github.com/88250/lute/parse"
  26. "github.com/siyuan-note/logging"
  27. "github.com/siyuan-note/siyuan/kernel/search"
  28. "github.com/siyuan-note/siyuan/kernel/sql"
  29. "github.com/siyuan-note/siyuan/kernel/treenode"
  30. "github.com/siyuan-note/siyuan/kernel/util"
  31. )
  32. func createDocsByHPath(boxID, hPath, content, parentID, id string /* id 参数仅在 parentID 不为空的情况下使用 */) (retID string, err error) {
  33. hPath = strings.TrimSuffix(hPath, ".sy")
  34. if "" != parentID {
  35. retID = id
  36. // The save path is incorrect when creating a sub-doc by ref in a doc with the same name https://github.com/siyuan-note/siyuan/issues/8138
  37. // 在指定父文档 ID 的情况下优先查找父文档
  38. parentHPath, name := path.Split(hPath)
  39. parentHPath = strings.TrimSuffix(parentHPath, "/")
  40. preferredParent := treenode.GetBlockTreeRootByHPathPreferredParentID(boxID, parentHPath, parentID)
  41. if nil != preferredParent && preferredParent.ID == parentID {
  42. // 如果父文档存在且 ID 一致,则直接在父文档下创建
  43. p := strings.TrimSuffix(preferredParent.Path, ".sy") + "/" + id + ".sy"
  44. if _, err = createDoc(boxID, p, name, content); nil != err {
  45. logging.LogErrorf("create doc [%s] failed: %s", p, err)
  46. }
  47. return
  48. }
  49. } else {
  50. retID = ast.NewNodeID()
  51. if "" == id {
  52. id = retID
  53. }
  54. }
  55. root := treenode.GetBlockTreeRootByPath(boxID, hPath)
  56. if nil != root {
  57. retID = root.ID
  58. return
  59. }
  60. hPathBuilder := bytes.Buffer{}
  61. hpathBtMap := map[string]*treenode.BlockTree{}
  62. parts := strings.Split(hPath, "/")[1:]
  63. // The subdoc creation path is unstable when a parent doc with the same name exists https://github.com/siyuan-note/siyuan/issues/9322
  64. // 存在同名父文档时子文档创建路径不稳定,这里需要按照完整的 hpath 映射,不能在下面的循环中边构建 hpath 边构建 path,否则虽然 hpath 相同,但是会导致 path 组装错位
  65. for i, part := range parts {
  66. if i == len(parts)-1 {
  67. break
  68. }
  69. hPathBuilder.WriteString("/")
  70. hPathBuilder.WriteString(part)
  71. hp := hPathBuilder.String()
  72. root = treenode.GetBlockTreeRootByHPath(boxID, hp)
  73. if nil == root {
  74. break
  75. }
  76. hpathBtMap[hp] = root
  77. }
  78. pathBuilder := bytes.Buffer{}
  79. pathBuilder.WriteString("/")
  80. hPathBuilder = bytes.Buffer{}
  81. hPathBuilder.WriteString("/")
  82. for i, part := range parts {
  83. hPathBuilder.WriteString(part)
  84. hp := hPathBuilder.String()
  85. root = hpathBtMap[hp]
  86. isNotLast := i < len(parts)-1
  87. if nil == root {
  88. rootID := ast.NewNodeID()
  89. if i == len(parts)-1 {
  90. rootID = retID
  91. }
  92. pathBuilder.WriteString(rootID)
  93. docP := pathBuilder.String() + ".sy"
  94. if isNotLast {
  95. if _, err = createDoc(boxID, docP, part, ""); nil != err {
  96. return
  97. }
  98. } else {
  99. if _, err = createDoc(boxID, docP, part, content); nil != err {
  100. return
  101. }
  102. }
  103. if isNotLast {
  104. dirPath := filepath.Join(util.DataDir, boxID, pathBuilder.String())
  105. if err = os.MkdirAll(dirPath, 0755); nil != err {
  106. logging.LogErrorf("mkdir [%s] failed: %s", dirPath, err)
  107. return
  108. }
  109. }
  110. } else {
  111. pathBuilder.WriteString(root.ID)
  112. if !isNotLast {
  113. pathBuilder.WriteString(".sy")
  114. }
  115. }
  116. if isNotLast {
  117. pathBuilder.WriteString("/")
  118. hPathBuilder.WriteString("/")
  119. }
  120. }
  121. return
  122. }
  123. func toFlatTree(blocks []*Block, baseDepth int, typ string, tree *parse.Tree) (ret []*Path) {
  124. var blockRoots []*Block
  125. for _, block := range blocks {
  126. root := getBlockIn(blockRoots, block.RootID)
  127. if nil == root {
  128. root, _ = getBlock(block.RootID, tree)
  129. blockRoots = append(blockRoots, root)
  130. }
  131. if nil == root {
  132. return
  133. }
  134. block.Depth = baseDepth + 1
  135. block.Count = len(block.Children)
  136. root.Children = append(root.Children, block)
  137. }
  138. for _, root := range blockRoots {
  139. treeNode := &Path{
  140. ID: root.ID,
  141. Box: root.Box,
  142. Name: path.Base(root.HPath),
  143. NodeType: root.Type,
  144. Type: typ,
  145. SubType: root.SubType,
  146. Depth: baseDepth,
  147. Count: len(root.Children),
  148. Updated: root.IAL["updated"],
  149. Created: root.ID[:14],
  150. }
  151. for _, c := range root.Children {
  152. treeNode.Blocks = append(treeNode.Blocks, c)
  153. }
  154. ret = append(ret, treeNode)
  155. if "backlink" == typ {
  156. treeNode.HPath = root.HPath
  157. }
  158. }
  159. sort.Slice(ret, func(i, j int) bool {
  160. return ret[i].ID > ret[j].ID
  161. })
  162. return
  163. }
  164. func toSubTree(blocks []*Block, keyword string) (ret []*Path) {
  165. keyword = strings.TrimSpace(keyword)
  166. var blockRoots []*Block
  167. for _, block := range blocks {
  168. root := getBlockIn(blockRoots, block.RootID)
  169. if nil == root {
  170. root, _ = getBlock(block.RootID, nil)
  171. blockRoots = append(blockRoots, root)
  172. }
  173. block.Depth = 1
  174. block.Count = len(block.Children)
  175. root.Children = append(root.Children, block)
  176. }
  177. for _, root := range blockRoots {
  178. treeNode := &Path{
  179. ID: root.ID,
  180. Box: root.Box,
  181. Name: path.Base(root.HPath),
  182. Type: "backlink",
  183. NodeType: "NodeDocument",
  184. SubType: root.SubType,
  185. Depth: 0,
  186. Count: len(root.Children),
  187. }
  188. for _, c := range root.Children {
  189. if "NodeListItem" == c.Type {
  190. tree, _ := loadTreeByBlockID(c.RootID)
  191. li := treenode.GetNodeInTree(tree, c.ID)
  192. if nil == li || nil == li.FirstChild {
  193. // 反链面板拖拽到文档以后可能会出现这种情况 https://github.com/siyuan-note/siyuan/issues/5363
  194. continue
  195. }
  196. var first *sql.Block
  197. if 3 != li.ListData.Typ {
  198. first = sql.GetBlock(li.FirstChild.ID)
  199. } else {
  200. first = sql.GetBlock(li.FirstChild.Next.ID)
  201. }
  202. name := first.Content
  203. parentPos := 0
  204. if "" != keyword {
  205. parentPos, name = search.MarkText(name, keyword, 12, Conf.Search.CaseSensitive)
  206. }
  207. subRoot := &Path{
  208. ID: li.ID,
  209. Box: li.Box,
  210. Name: name,
  211. Type: "backlink",
  212. NodeType: li.Type.String(),
  213. SubType: c.SubType,
  214. Depth: 1,
  215. Count: 1,
  216. }
  217. unfold := true
  218. for liFirstBlockSpan := li.FirstChild.FirstChild; nil != liFirstBlockSpan; liFirstBlockSpan = liFirstBlockSpan.Next {
  219. if treenode.IsBlockRef(liFirstBlockSpan) {
  220. continue
  221. }
  222. if "" != strings.TrimSpace(liFirstBlockSpan.Text()) {
  223. unfold = false
  224. break
  225. }
  226. }
  227. for next := li.FirstChild.Next; nil != next; next = next.Next {
  228. subBlock, _ := getBlock(next.ID, tree)
  229. if unfold {
  230. if ast.NodeList == next.Type {
  231. for subLi := next.FirstChild; nil != subLi; subLi = subLi.Next {
  232. subLiBlock, _ := getBlock(subLi.ID, tree)
  233. var subFirst *sql.Block
  234. if 3 != subLi.ListData.Typ {
  235. subFirst = sql.GetBlock(subLi.FirstChild.ID)
  236. } else {
  237. subFirst = sql.GetBlock(subLi.FirstChild.Next.ID)
  238. }
  239. subPos := 0
  240. content := subFirst.Content
  241. if "" != keyword {
  242. subPos, content = search.MarkText(subFirst.Content, keyword, 12, Conf.Search.CaseSensitive)
  243. }
  244. if -1 < subPos {
  245. parentPos = 0 // 需要显示父级
  246. }
  247. subLiBlock.Content = content
  248. subLiBlock.Depth = 2
  249. subRoot.Blocks = append(subRoot.Blocks, subLiBlock)
  250. }
  251. } else if ast.NodeHeading == next.Type {
  252. subBlock.Depth = 2
  253. subRoot.Blocks = append(subRoot.Blocks, subBlock)
  254. headingChildren := treenode.HeadingChildren(next)
  255. var breakSub bool
  256. for _, n := range headingChildren {
  257. block, _ := getBlock(n.ID, tree)
  258. subPos := 0
  259. content := block.Content
  260. if "" != keyword {
  261. subPos, content = search.MarkText(block.Content, keyword, 12, Conf.Search.CaseSensitive)
  262. }
  263. if -1 < subPos {
  264. parentPos = 0
  265. }
  266. block.Content = content
  267. block.Depth = 3
  268. subRoot.Blocks = append(subRoot.Blocks, block)
  269. if ast.NodeHeading == n.Type {
  270. // 跳过子标题下面的块
  271. breakSub = true
  272. break
  273. }
  274. }
  275. if breakSub {
  276. break
  277. }
  278. } else {
  279. if nil == treenode.HeadingParent(next) {
  280. subBlock.Depth = 2
  281. subRoot.Blocks = append(subRoot.Blocks, subBlock)
  282. }
  283. }
  284. }
  285. }
  286. if -1 < parentPos {
  287. treeNode.Children = append(treeNode.Children, subRoot)
  288. }
  289. } else if "NodeHeading" == c.Type {
  290. tree, _ := loadTreeByBlockID(c.RootID)
  291. h := treenode.GetNodeInTree(tree, c.ID)
  292. if nil == h {
  293. continue
  294. }
  295. name := sql.GetBlock(h.ID).Content
  296. parentPos := 0
  297. if "" != keyword {
  298. parentPos, name = search.MarkText(name, keyword, 12, Conf.Search.CaseSensitive)
  299. }
  300. subRoot := &Path{
  301. ID: h.ID,
  302. Box: h.Box,
  303. Name: name,
  304. Type: "backlink",
  305. NodeType: h.Type.String(),
  306. SubType: c.SubType,
  307. Depth: 1,
  308. Count: 1,
  309. }
  310. unfold := true
  311. for headingFirstSpan := h.FirstChild; nil != headingFirstSpan; headingFirstSpan = headingFirstSpan.Next {
  312. if treenode.IsBlockRef(headingFirstSpan) {
  313. continue
  314. }
  315. if "" != strings.TrimSpace(headingFirstSpan.Text()) {
  316. unfold = false
  317. break
  318. }
  319. }
  320. if unfold {
  321. headingChildren := treenode.HeadingChildren(h)
  322. for _, headingChild := range headingChildren {
  323. if ast.NodeList == headingChild.Type {
  324. for subLi := headingChild.FirstChild; nil != subLi; subLi = subLi.Next {
  325. subLiBlock, _ := getBlock(subLi.ID, tree)
  326. var subFirst *sql.Block
  327. if 3 != subLi.ListData.Typ {
  328. subFirst = sql.GetBlock(subLi.FirstChild.ID)
  329. } else {
  330. subFirst = sql.GetBlock(subLi.FirstChild.Next.ID)
  331. }
  332. subPos := 0
  333. content := subFirst.Content
  334. if "" != keyword {
  335. subPos, content = search.MarkText(content, keyword, 12, Conf.Search.CaseSensitive)
  336. }
  337. if -1 < subPos {
  338. parentPos = 0
  339. }
  340. subLiBlock.Content = subFirst.Content
  341. subLiBlock.Depth = 2
  342. subRoot.Blocks = append(subRoot.Blocks, subLiBlock)
  343. }
  344. } else {
  345. subBlock, _ := getBlock(headingChild.ID, tree)
  346. subBlock.Depth = 2
  347. subRoot.Blocks = append(subRoot.Blocks, subBlock)
  348. }
  349. }
  350. }
  351. if -1 < parentPos {
  352. treeNode.Children = append(treeNode.Children, subRoot)
  353. }
  354. } else {
  355. pos := 0
  356. content := c.Content
  357. if "" != keyword {
  358. pos, content = search.MarkText(content, keyword, 12, Conf.Search.CaseSensitive)
  359. }
  360. if -1 < pos {
  361. treeNode.Blocks = append(treeNode.Blocks, c)
  362. }
  363. }
  364. }
  365. rootPos := -1
  366. var rootContent string
  367. if "" != keyword {
  368. rootPos, rootContent = search.MarkText(treeNode.Name, keyword, 12, Conf.Search.CaseSensitive)
  369. treeNode.Name = rootContent
  370. }
  371. if 0 < len(treeNode.Children) || 0 < len(treeNode.Blocks) || (-1 < rootPos && "" != keyword) {
  372. ret = append(ret, treeNode)
  373. }
  374. }
  375. sort.Slice(ret, func(i, j int) bool {
  376. return ret[i].ID > ret[j].ID
  377. })
  378. return
  379. }
  380. func getBlockIn(blocks []*Block, id string) *Block {
  381. if "" == id {
  382. return nil
  383. }
  384. for _, block := range blocks {
  385. if block.ID == id {
  386. return block
  387. }
  388. }
  389. return nil
  390. }