json_parser.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 filesys
  17. import (
  18. "bytes"
  19. "strings"
  20. "github.com/88250/gulu"
  21. "github.com/88250/lute/ast"
  22. "github.com/88250/lute/editor"
  23. "github.com/88250/lute/parse"
  24. "github.com/siyuan-note/siyuan/kernel/treenode"
  25. )
  26. func ParseJSONWithoutFix(jsonData []byte, options *parse.Options) (ret *parse.Tree, err error) {
  27. root := &ast.Node{}
  28. err = unmarshalJSON(jsonData, root)
  29. if nil != err {
  30. return
  31. }
  32. ret = &parse.Tree{Name: "", ID: root.ID, Root: &ast.Node{Type: ast.NodeDocument, ID: root.ID, Spec: root.Spec}, Context: &parse.Context{ParseOption: options}}
  33. ret.Root.KramdownIAL = parse.Map2IAL(root.Properties)
  34. ret.Context.Tip = ret.Root
  35. if nil == root.Children {
  36. return
  37. }
  38. idMap := map[string]bool{}
  39. for _, child := range root.Children {
  40. genTreeByJSON(child, ret, &idMap, nil, nil, true)
  41. }
  42. return
  43. }
  44. func ParseJSON(jsonData []byte, options *parse.Options) (ret *parse.Tree, needFix bool, err error) {
  45. root := &ast.Node{}
  46. err = unmarshalJSON(jsonData, root)
  47. if nil != err {
  48. return
  49. }
  50. ret = &parse.Tree{Name: "", ID: root.ID, Root: &ast.Node{Type: ast.NodeDocument, ID: root.ID, Spec: root.Spec}, Context: &parse.Context{ParseOption: options}}
  51. ret.Root.KramdownIAL = parse.Map2IAL(root.Properties)
  52. for _, kv := range ret.Root.KramdownIAL {
  53. if strings.Contains(kv[1], "\n") {
  54. val := kv[1]
  55. val = strings.ReplaceAll(val, "\n", editor.IALValEscNewLine)
  56. ret.Root.SetIALAttr(kv[0], val)
  57. needFix = true
  58. }
  59. }
  60. ret.Context.Tip = ret.Root
  61. if nil == root.Children {
  62. newPara := &ast.Node{Type: ast.NodeParagraph, ID: ast.NewNodeID()}
  63. newPara.SetIALAttr("id", newPara.ID)
  64. ret.Root.AppendChild(newPara)
  65. needFix = true
  66. return
  67. }
  68. needMigrate2Spec1 := false
  69. idMap := map[string]bool{}
  70. for _, child := range root.Children {
  71. genTreeByJSON(child, ret, &idMap, &needFix, &needMigrate2Spec1, false)
  72. }
  73. if nil == ret.Root.FirstChild {
  74. // 如果是空文档的话挂一个空段落上去
  75. newP := treenode.NewParagraph()
  76. ret.Root.AppendChild(newP)
  77. ret.Root.SetIALAttr("updated", newP.ID[:14])
  78. }
  79. if needMigrate2Spec1 {
  80. parse.NestedInlines2FlattedSpans(ret)
  81. needFix = true
  82. }
  83. return
  84. }
  85. func genTreeByJSON(node *ast.Node, tree *parse.Tree, idMap *map[string]bool, needFix, needMigrate2Spec1 *bool, ignoreFix bool) {
  86. node.Tokens, node.Type = gulu.Str.ToBytes(node.Data), ast.Str2NodeType(node.TypeStr)
  87. node.Data, node.TypeStr = "", ""
  88. node.KramdownIAL = parse.Map2IAL(node.Properties)
  89. node.Properties = nil
  90. if !ignoreFix {
  91. // 历史数据订正
  92. if -1 == node.Type {
  93. *needFix = true
  94. node.Type = ast.NodeParagraph
  95. node.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: node.Tokens})
  96. node.Children = nil
  97. }
  98. switch node.Type {
  99. case ast.NodeList:
  100. if 1 > len(node.Children) {
  101. *needFix = true
  102. return // 忽略空列表
  103. }
  104. case ast.NodeListItem:
  105. if 1 > len(node.Children) {
  106. *needFix = true
  107. return // 忽略空列表项
  108. }
  109. case ast.NodeBlockquote:
  110. if 2 > len(node.Children) {
  111. *needFix = true
  112. return // 忽略空引述
  113. }
  114. case ast.NodeSuperBlock:
  115. if 4 > len(node.Children) {
  116. *needFix = true
  117. return // 忽略空超级块
  118. }
  119. case ast.NodeMathBlock:
  120. if 1 > len(node.Children) {
  121. *needFix = true
  122. return // 忽略空公式
  123. }
  124. case ast.NodeBlockQueryEmbed:
  125. if 1 > len(node.Children) {
  126. *needFix = true
  127. return // 忽略空查询嵌入块
  128. }
  129. }
  130. fixLegacyData(tree.Context.Tip, node, idMap, needFix, needMigrate2Spec1)
  131. }
  132. tree.Context.Tip.AppendChild(node)
  133. tree.Context.Tip = node
  134. defer tree.Context.ParentTip()
  135. if nil == node.Children {
  136. return
  137. }
  138. for _, child := range node.Children {
  139. genTreeByJSON(child, tree, idMap, needFix, needMigrate2Spec1, ignoreFix)
  140. }
  141. node.Children = nil
  142. }
  143. func fixLegacyData(tip, node *ast.Node, idMap *map[string]bool, needFix, needMigrate2Spec1 *bool) {
  144. if node.IsBlock() {
  145. if "" == node.ID {
  146. node.ID = ast.NewNodeID()
  147. node.SetIALAttr("id", node.ID)
  148. *needFix = true
  149. }
  150. if 0 < len(node.Children) && ast.NodeBr.String() == node.Children[len(node.Children)-1].TypeStr {
  151. // 剔除块尾多余的软换行 https://github.com/siyuan-note/siyuan/issues/6191
  152. node.Children = node.Children[:len(node.Children)-1]
  153. *needFix = true
  154. }
  155. }
  156. if "" != node.ID {
  157. if _, ok := (*idMap)[node.ID]; ok {
  158. node.ID = ast.NewNodeID()
  159. node.SetIALAttr("id", node.ID)
  160. *needFix = true
  161. }
  162. (*idMap)[node.ID] = true
  163. }
  164. switch node.Type {
  165. case ast.NodeIFrame:
  166. if bytes.Contains(node.Tokens, gulu.Str.ToBytes("iframe-content")) {
  167. start := bytes.Index(node.Tokens, gulu.Str.ToBytes("<iframe"))
  168. end := bytes.Index(node.Tokens, gulu.Str.ToBytes("</iframe>"))
  169. node.Tokens = node.Tokens[start : end+9]
  170. *needFix = true
  171. }
  172. case ast.NodeWidget:
  173. if bytes.Contains(node.Tokens, gulu.Str.ToBytes("http://127.0.0.1:6806")) {
  174. node.Tokens = bytes.ReplaceAll(node.Tokens, []byte("http://127.0.0.1:6806"), nil)
  175. *needFix = true
  176. }
  177. case ast.NodeList:
  178. if nil != node.ListData && 3 != node.ListData.Typ && 0 < len(node.Children) &&
  179. nil != node.Children[0].ListData && 3 == node.Children[0].ListData.Typ {
  180. node.ListData.Typ = 3
  181. *needFix = true
  182. }
  183. case ast.NodeMark:
  184. if 3 == len(node.Children) && "NodeText" == node.Children[1].TypeStr {
  185. if strings.HasPrefix(node.Children[1].Data, " ") || strings.HasSuffix(node.Children[1].Data, " ") {
  186. node.Children[1].Data = strings.TrimSpace(node.Children[1].Data)
  187. *needFix = true
  188. }
  189. }
  190. case ast.NodeHeading:
  191. if 6 < node.HeadingLevel {
  192. node.HeadingLevel = 6
  193. *needFix = true
  194. }
  195. case ast.NodeLinkDest:
  196. if bytes.HasPrefix(node.Tokens, []byte("assets/")) && bytes.HasSuffix(node.Tokens, []byte(" ")) {
  197. node.Tokens = bytes.TrimSpace(node.Tokens)
  198. *needFix = true
  199. }
  200. case ast.NodeText:
  201. if nil != tip.LastChild && ast.NodeTagOpenMarker == tip.LastChild.Type && 1 > len(node.Tokens) {
  202. node.Tokens = []byte("Untitled")
  203. *needFix = true
  204. }
  205. case ast.NodeTagCloseMarker:
  206. if nil != tip.LastChild {
  207. if ast.NodeTagOpenMarker == tip.LastChild.Type {
  208. tip.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte("Untitled")})
  209. *needFix = true
  210. } else if "" == tip.LastChild.Text() {
  211. tip.LastChild.Type = ast.NodeText
  212. tip.LastChild.Tokens = []byte("Untitled")
  213. *needFix = true
  214. }
  215. }
  216. case ast.NodeBlockRef:
  217. // 建立索引时无法解析 `v2.2.0-` 版本的块引用 https://github.com/siyuan-note/siyuan/issues/6889
  218. // 早先的迁移程序有缺陷,漏迁移了块引用节点,这里检测到块引用节点后标识需要迁移
  219. *needMigrate2Spec1 = true
  220. case ast.NodeInlineHTML:
  221. *needFix = true
  222. node.Type = ast.NodeHTMLBlock
  223. }
  224. for _, kv := range node.KramdownIAL {
  225. if strings.Contains(kv[1], "\n") {
  226. val := kv[1]
  227. val = strings.ReplaceAll(val, "\n", editor.IALValEscNewLine)
  228. node.SetIALAttr(kv[0], val)
  229. *needFix = true
  230. }
  231. }
  232. }