outline.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. "time"
  19. "github.com/88250/lute/ast"
  20. "github.com/88250/lute/parse"
  21. "github.com/emirpasic/gods/stacks/linkedliststack"
  22. "github.com/siyuan-note/siyuan/kernel/treenode"
  23. "github.com/siyuan-note/siyuan/kernel/util"
  24. )
  25. func Outline(rootID string) (ret []*Path, err error) {
  26. time.Sleep(util.FrontendQueueInterval)
  27. WaitForWritingFiles()
  28. ret = []*Path{}
  29. tree, _ := loadTreeByBlockID(rootID)
  30. if nil == tree {
  31. return
  32. }
  33. ret = outline(tree)
  34. return
  35. }
  36. func outline(tree *parse.Tree) (ret []*Path) {
  37. luteEngine := NewLute()
  38. var headings []*Block
  39. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  40. if entering && ast.NodeHeading == n.Type && !n.ParentIs(ast.NodeBlockquote) {
  41. n.Box, n.Path = tree.Box, tree.Path
  42. block := &Block{
  43. RootID: tree.Root.ID,
  44. Depth: n.HeadingLevel,
  45. Box: n.Box,
  46. Path: n.Path,
  47. ID: n.ID,
  48. Content: renderOutline(n, luteEngine),
  49. Type: n.Type.String(),
  50. SubType: treenode.SubTypeAbbr(n),
  51. }
  52. headings = append(headings, block)
  53. return ast.WalkSkipChildren
  54. }
  55. return ast.WalkContinue
  56. })
  57. if 1 > len(headings) {
  58. return
  59. }
  60. var blocks []*Block
  61. stack := linkedliststack.New()
  62. for _, h := range headings {
  63. L:
  64. for ; ; stack.Pop() {
  65. cur, ok := stack.Peek()
  66. if !ok {
  67. blocks = append(blocks, h)
  68. stack.Push(h)
  69. break L
  70. }
  71. tip := cur.(*Block)
  72. if tip.Depth < h.Depth {
  73. tip.Children = append(tip.Children, h)
  74. stack.Push(h)
  75. break L
  76. }
  77. tip.Count = len(tip.Children)
  78. }
  79. }
  80. ret = toFlatTree(blocks, 0, "outline", tree)
  81. if 0 < len(ret) {
  82. children := ret[0].Blocks
  83. ret = nil
  84. for _, b := range children {
  85. resetDepth(b, 0)
  86. ret = append(ret, &Path{
  87. ID: b.ID,
  88. Box: b.Box,
  89. Name: b.Content,
  90. NodeType: b.Type,
  91. Type: "outline",
  92. SubType: b.SubType,
  93. Blocks: b.Children,
  94. Depth: 0,
  95. Count: b.Count,
  96. })
  97. }
  98. }
  99. return
  100. }
  101. func resetDepth(b *Block, depth int) {
  102. b.Depth = depth
  103. b.Count = len(b.Children)
  104. for _, c := range b.Children {
  105. resetDepth(c, depth+1)
  106. }
  107. }