json_parser.go 8.0 KB

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