heading.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. if nil == node {
  55. return false
  56. }
  57. heading := HeadingParent(node)
  58. if nil == heading {
  59. return false
  60. }
  61. if "1" == heading.IALAttr("heading-fold") || "1" == heading.IALAttr("fold") {
  62. return true
  63. }
  64. if heading == currentHeading {
  65. // node 就在当前标题层级下的话不递归继续查询,直接返回不折叠
  66. return false
  67. }
  68. return IsInFoldedHeading(heading, currentHeading)
  69. }
  70. func GetHeadingFold(nodes []*ast.Node) (ret []*ast.Node) {
  71. for _, n := range nodes {
  72. if "1" == n.IALAttr("heading-fold") {
  73. ret = append(ret, n)
  74. }
  75. }
  76. return
  77. }
  78. func HeadingChildren(heading *ast.Node) (ret []*ast.Node) {
  79. start := heading.Next
  80. if nil == start {
  81. return
  82. }
  83. if ast.NodeKramdownBlockIAL == start.Type {
  84. start = start.Next // 跳过 heading 的 IAL
  85. }
  86. currentLevel := heading.HeadingLevel
  87. for n := start; nil != n; n = n.Next {
  88. if ast.NodeHeading == n.Type {
  89. if currentLevel >= n.HeadingLevel {
  90. break
  91. }
  92. }
  93. ret = append(ret, n)
  94. }
  95. return
  96. }
  97. func SuperBlockLastHeading(sb *ast.Node) *ast.Node {
  98. headings := sb.ChildrenByType(ast.NodeHeading)
  99. if 0 < len(headings) {
  100. return headings[len(headings)-1]
  101. }
  102. return nil
  103. }
  104. func HeadingParent(node *ast.Node) *ast.Node {
  105. if nil == node {
  106. return nil
  107. }
  108. currentLevel := 16
  109. if ast.NodeHeading == node.Type {
  110. currentLevel = node.HeadingLevel
  111. }
  112. for n := node.Previous; nil != n; n = n.Previous {
  113. if ast.NodeHeading == n.Type && n.HeadingLevel < currentLevel {
  114. return n
  115. }
  116. }
  117. return node.Parent
  118. }
  119. func HeadingLevel(node *ast.Node) int {
  120. if nil == node {
  121. return 0
  122. }
  123. for n := node; nil != n; n = n.Previous {
  124. if ast.NodeHeading == n.Type {
  125. return n.HeadingLevel
  126. }
  127. }
  128. return 0
  129. }
  130. func TopHeadingLevel(tree *parse.Tree) (ret int) {
  131. ret = 7
  132. for n := tree.Root.FirstChild; nil != n; n = n.Next {
  133. if ast.NodeHeading == n.Type {
  134. if ret > n.HeadingLevel {
  135. ret = n.HeadingLevel
  136. }
  137. }
  138. }
  139. if 7 == ret { // 没有出现过标题时
  140. ret = 0
  141. }
  142. return
  143. }