json_parser.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. case ast.NodeCodeBlock:
  132. if 4 > len(node.Children) {
  133. // https://ld246.com/article/1713689223067
  134. existCode := false
  135. for _, child := range node.Children {
  136. if ast.NodeCodeBlockCode.String() == child.TypeStr {
  137. existCode = true
  138. break
  139. }
  140. }
  141. if !existCode {
  142. *needFix = true
  143. return // 忽略空代码块
  144. }
  145. }
  146. }
  147. fixLegacyData(tree.Context.Tip, node, idMap, needFix, needMigrate2Spec1)
  148. }
  149. tree.Context.Tip.AppendChild(node)
  150. tree.Context.Tip = node
  151. defer tree.Context.ParentTip()
  152. if nil == node.Children {
  153. return
  154. }
  155. for _, child := range node.Children {
  156. genTreeByJSON(child, tree, idMap, needFix, needMigrate2Spec1, ignoreFix)
  157. }
  158. node.Children = nil
  159. }
  160. func fixLegacyData(tip, node *ast.Node, idMap *map[string]bool, needFix, needMigrate2Spec1 *bool) {
  161. if node.IsBlock() {
  162. if "" == node.ID {
  163. node.ID = ast.NewNodeID()
  164. node.SetIALAttr("id", node.ID)
  165. *needFix = true
  166. }
  167. if 0 < len(node.Children) && ast.NodeBr.String() == node.Children[len(node.Children)-1].TypeStr {
  168. // 剔除块尾多余的软换行 https://github.com/siyuan-note/siyuan/issues/6191
  169. node.Children = node.Children[:len(node.Children)-1]
  170. *needFix = true
  171. }
  172. for _, kv := range node.KramdownIAL {
  173. if strings.Contains(kv[0], "custom-av-key-") {
  174. // TODO: 数据库正式上线以后移除这里的修复
  175. // 删除数据库属性键值对 https://github.com/siyuan-note/siyuan/issues/9293
  176. node.RemoveIALAttr(kv[0])
  177. *needFix = true
  178. }
  179. }
  180. }
  181. if "" != node.ID {
  182. if _, ok := (*idMap)[node.ID]; ok {
  183. node.ID = ast.NewNodeID()
  184. node.SetIALAttr("id", node.ID)
  185. *needFix = true
  186. }
  187. (*idMap)[node.ID] = true
  188. }
  189. switch node.Type {
  190. case ast.NodeIFrame:
  191. if bytes.Contains(node.Tokens, gulu.Str.ToBytes("iframe-content")) {
  192. start := bytes.Index(node.Tokens, gulu.Str.ToBytes("<iframe"))
  193. end := bytes.Index(node.Tokens, gulu.Str.ToBytes("</iframe>"))
  194. node.Tokens = node.Tokens[start : end+9]
  195. *needFix = true
  196. }
  197. case ast.NodeWidget:
  198. if bytes.Contains(node.Tokens, gulu.Str.ToBytes("http://127.0.0.1:6806")) {
  199. node.Tokens = bytes.ReplaceAll(node.Tokens, []byte("http://127.0.0.1:6806"), nil)
  200. *needFix = true
  201. }
  202. case ast.NodeList:
  203. if nil != node.ListData && 3 != node.ListData.Typ && 0 < len(node.Children) &&
  204. nil != node.Children[0].ListData && 3 == node.Children[0].ListData.Typ {
  205. node.ListData.Typ = 3
  206. *needFix = true
  207. }
  208. case ast.NodeMark:
  209. if 3 == len(node.Children) && "NodeText" == node.Children[1].TypeStr {
  210. if strings.HasPrefix(node.Children[1].Data, " ") || strings.HasSuffix(node.Children[1].Data, " ") {
  211. node.Children[1].Data = strings.TrimSpace(node.Children[1].Data)
  212. *needFix = true
  213. }
  214. }
  215. case ast.NodeHeading:
  216. if 6 < node.HeadingLevel {
  217. node.HeadingLevel = 6
  218. *needFix = true
  219. }
  220. case ast.NodeLinkDest:
  221. if bytes.HasPrefix(node.Tokens, []byte("assets/")) && bytes.HasSuffix(node.Tokens, []byte(" ")) {
  222. node.Tokens = bytes.TrimSpace(node.Tokens)
  223. *needFix = true
  224. }
  225. case ast.NodeText:
  226. if nil != tip.LastChild && ast.NodeTagOpenMarker == tip.LastChild.Type && 1 > len(node.Tokens) {
  227. node.Tokens = []byte("Untitled")
  228. *needFix = true
  229. }
  230. case ast.NodeTagCloseMarker:
  231. if nil != tip.LastChild {
  232. if ast.NodeTagOpenMarker == tip.LastChild.Type {
  233. tip.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte("Untitled")})
  234. *needFix = true
  235. } else if "" == tip.LastChild.Text() {
  236. tip.LastChild.Type = ast.NodeText
  237. tip.LastChild.Tokens = []byte("Untitled")
  238. *needFix = true
  239. }
  240. }
  241. case ast.NodeBlockRef:
  242. // 建立索引时无法解析 `v2.2.0-` 版本的块引用 https://github.com/siyuan-note/siyuan/issues/6889
  243. // 早先的迁移程序有缺陷,漏迁移了块引用节点,这里检测到块引用节点后标识需要迁移
  244. *needMigrate2Spec1 = true
  245. case ast.NodeInlineHTML:
  246. *needFix = true
  247. node.Type = ast.NodeHTMLBlock
  248. }
  249. for _, kv := range node.KramdownIAL {
  250. if strings.Contains(kv[1], "\n") {
  251. val := kv[1]
  252. val = strings.ReplaceAll(val, "\n", editor.IALValEscNewLine)
  253. node.SetIALAttr(kv[0], val)
  254. *needFix = true
  255. }
  256. }
  257. }