json_parser.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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, false)
  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. for _, kv := range node.KramdownIAL {
  156. if strings.Contains(kv[0], "custom-av-key-") {
  157. // 删除数据库属性键值对 https://github.com/siyuan-note/siyuan/issues/9293
  158. node.RemoveIALAttr(kv[0])
  159. *needFix = true
  160. }
  161. }
  162. }
  163. if "" != node.ID {
  164. if _, ok := (*idMap)[node.ID]; ok {
  165. node.ID = ast.NewNodeID()
  166. node.SetIALAttr("id", node.ID)
  167. *needFix = true
  168. }
  169. (*idMap)[node.ID] = true
  170. }
  171. switch node.Type {
  172. case ast.NodeIFrame:
  173. if bytes.Contains(node.Tokens, gulu.Str.ToBytes("iframe-content")) {
  174. start := bytes.Index(node.Tokens, gulu.Str.ToBytes("<iframe"))
  175. end := bytes.Index(node.Tokens, gulu.Str.ToBytes("</iframe>"))
  176. node.Tokens = node.Tokens[start : end+9]
  177. *needFix = true
  178. }
  179. case ast.NodeWidget:
  180. if bytes.Contains(node.Tokens, gulu.Str.ToBytes("http://127.0.0.1:6806")) {
  181. node.Tokens = bytes.ReplaceAll(node.Tokens, []byte("http://127.0.0.1:6806"), nil)
  182. *needFix = true
  183. }
  184. case ast.NodeList:
  185. if nil != node.ListData && 3 != node.ListData.Typ && 0 < len(node.Children) &&
  186. nil != node.Children[0].ListData && 3 == node.Children[0].ListData.Typ {
  187. node.ListData.Typ = 3
  188. *needFix = true
  189. }
  190. case ast.NodeMark:
  191. if 3 == len(node.Children) && "NodeText" == node.Children[1].TypeStr {
  192. if strings.HasPrefix(node.Children[1].Data, " ") || strings.HasSuffix(node.Children[1].Data, " ") {
  193. node.Children[1].Data = strings.TrimSpace(node.Children[1].Data)
  194. *needFix = true
  195. }
  196. }
  197. case ast.NodeHeading:
  198. if 6 < node.HeadingLevel {
  199. node.HeadingLevel = 6
  200. *needFix = true
  201. }
  202. case ast.NodeLinkDest:
  203. if bytes.HasPrefix(node.Tokens, []byte("assets/")) && bytes.HasSuffix(node.Tokens, []byte(" ")) {
  204. node.Tokens = bytes.TrimSpace(node.Tokens)
  205. *needFix = true
  206. }
  207. case ast.NodeText:
  208. if nil != tip.LastChild && ast.NodeTagOpenMarker == tip.LastChild.Type && 1 > len(node.Tokens) {
  209. node.Tokens = []byte("Untitled")
  210. *needFix = true
  211. }
  212. case ast.NodeTagCloseMarker:
  213. if nil != tip.LastChild {
  214. if ast.NodeTagOpenMarker == tip.LastChild.Type {
  215. tip.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte("Untitled")})
  216. *needFix = true
  217. } else if "" == tip.LastChild.Text() {
  218. tip.LastChild.Type = ast.NodeText
  219. tip.LastChild.Tokens = []byte("Untitled")
  220. *needFix = true
  221. }
  222. }
  223. case ast.NodeBlockRef:
  224. // 建立索引时无法解析 `v2.2.0-` 版本的块引用 https://github.com/siyuan-note/siyuan/issues/6889
  225. // 早先的迁移程序有缺陷,漏迁移了块引用节点,这里检测到块引用节点后标识需要迁移
  226. *needMigrate2Spec1 = true
  227. case ast.NodeInlineHTML:
  228. *needFix = true
  229. node.Type = ast.NodeHTMLBlock
  230. }
  231. for _, kv := range node.KramdownIAL {
  232. if strings.Contains(kv[1], "\n") {
  233. val := kv[1]
  234. val = strings.ReplaceAll(val, "\n", editor.IALValEscNewLine)
  235. node.SetIALAttr(kv[0], val)
  236. *needFix = true
  237. }
  238. }
  239. }