heading.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 treenode
  17. import (
  18. "github.com/88250/lute/ast"
  19. "github.com/88250/lute/parse"
  20. )
  21. func MoveFoldHeading(updateNode, oldNode *ast.Node) {
  22. foldHeadings := map[string][]*ast.Node{}
  23. // 找到原有节点中所有折叠标题节点的下方节点
  24. ast.Walk(oldNode, func(n *ast.Node, entering bool) ast.WalkStatus {
  25. if !entering {
  26. return ast.WalkContinue
  27. }
  28. if ast.NodeHeading == n.Type && "1" == n.IALAttr("fold") {
  29. children := HeadingChildren(n)
  30. foldHeadings[n.ID] = children
  31. }
  32. return ast.WalkContinue
  33. })
  34. // 将原来所有折叠标题对应的下方节点移动到新节点下
  35. var updateFoldHeadings []*ast.Node
  36. ast.Walk(updateNode, func(n *ast.Node, entering bool) ast.WalkStatus {
  37. if !entering {
  38. return ast.WalkContinue
  39. }
  40. if ast.NodeHeading == n.Type && "1" == n.IALAttr("fold") {
  41. updateFoldHeadings = append(updateFoldHeadings, n)
  42. }
  43. return ast.WalkContinue
  44. })
  45. for _, h := range updateFoldHeadings {
  46. children := foldHeadings[h.ID]
  47. for i := len(children) - 1; 0 <= i; i-- {
  48. h.Next.InsertAfter(children[i]) // Next 是 Block IAL
  49. }
  50. }
  51. return
  52. }
  53. func IsInFoldedHeading(node, currentHeading *ast.Node) bool {
  54. heading := HeadingParent(node)
  55. if nil == heading {
  56. return false
  57. }
  58. if "1" == heading.IALAttr("heading-fold") || "1" == heading.IALAttr("fold") {
  59. return true
  60. }
  61. if heading == currentHeading {
  62. // node 就在当前标题层级下的话不递归继续查询,直接返回不折叠
  63. return false
  64. }
  65. return IsInFoldedHeading(heading, currentHeading)
  66. }
  67. func GetHeadingFold(nodes []*ast.Node) (ret []*ast.Node) {
  68. for _, n := range nodes {
  69. if "1" == n.IALAttr("heading-fold") {
  70. ret = append(ret, n)
  71. }
  72. }
  73. return
  74. }
  75. func HeadingChildren(heading *ast.Node) (ret []*ast.Node) {
  76. start := heading.Next
  77. if nil == start {
  78. return
  79. }
  80. if ast.NodeKramdownBlockIAL == start.Type {
  81. start = start.Next // 跳过 heading 的 IAL
  82. }
  83. currentLevel := heading.HeadingLevel
  84. for n := start; nil != n; n = n.Next {
  85. if ast.NodeHeading == n.Type {
  86. if currentLevel >= n.HeadingLevel {
  87. break
  88. }
  89. } else if ast.NodeSuperBlock == n.Type {
  90. if h := SuperBlockHeading(n); nil != h {
  91. if currentLevel >= h.HeadingLevel {
  92. break
  93. }
  94. }
  95. } else if ast.NodeSuperBlockCloseMarker == n.Type {
  96. continue
  97. }
  98. ret = append(ret, n)
  99. }
  100. return
  101. }
  102. func SuperBlockHeading(sb *ast.Node) *ast.Node {
  103. c := sb.FirstChild.Next.Next
  104. if nil == c {
  105. return nil
  106. }
  107. if ast.NodeHeading == c.Type {
  108. return c
  109. }
  110. if ast.NodeSuperBlock == c.Type {
  111. return SuperBlockHeading(c)
  112. }
  113. return nil
  114. }
  115. func SuperBlockLastHeading(sb *ast.Node) *ast.Node {
  116. headings := sb.ChildrenByType(ast.NodeHeading)
  117. if 0 < len(headings) {
  118. return headings[len(headings)-1]
  119. }
  120. return nil
  121. }
  122. func HeadingParent(node *ast.Node) *ast.Node {
  123. if nil == node {
  124. return nil
  125. }
  126. currentLevel := 16
  127. if ast.NodeHeading == node.Type {
  128. currentLevel = node.HeadingLevel
  129. }
  130. for n := node.Previous; nil != n; n = n.Previous {
  131. if ast.NodeHeading == n.Type && n.HeadingLevel < currentLevel {
  132. return n
  133. }
  134. }
  135. return node.Parent
  136. }
  137. func HeadingLevel(node *ast.Node) int {
  138. if nil == node {
  139. return 0
  140. }
  141. for n := node; nil != n; n = n.Previous {
  142. if ast.NodeHeading == n.Type {
  143. return n.HeadingLevel
  144. }
  145. }
  146. return 0
  147. }
  148. func TopHeadingLevel(tree *parse.Tree) (ret int) {
  149. ret = 7
  150. for n := tree.Root.FirstChild; nil != n; n = n.Next {
  151. if ast.NodeHeading == n.Type {
  152. if ret > n.HeadingLevel {
  153. ret = n.HeadingLevel
  154. }
  155. }
  156. }
  157. if 7 == ret { // 没有出现过标题时
  158. ret = 0
  159. }
  160. return
  161. }