lute.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 api
  17. import (
  18. "net/http"
  19. "net/url"
  20. "path/filepath"
  21. "strings"
  22. "github.com/88250/gulu"
  23. "github.com/88250/lute/ast"
  24. "github.com/88250/lute/parse"
  25. "github.com/88250/lute/render"
  26. "github.com/gin-gonic/gin"
  27. "github.com/siyuan-note/filelock"
  28. "github.com/siyuan-note/logging"
  29. "github.com/siyuan-note/siyuan/kernel/model"
  30. "github.com/siyuan-note/siyuan/kernel/treenode"
  31. "github.com/siyuan-note/siyuan/kernel/util"
  32. )
  33. func copyStdMarkdown(c *gin.Context) {
  34. ret := gulu.Ret.NewResult()
  35. defer c.JSON(http.StatusOK, ret)
  36. arg, ok := util.JsonArg(c, ret)
  37. if !ok {
  38. return
  39. }
  40. id := arg["id"].(string)
  41. ret.Data = model.ExportStdMarkdown(id)
  42. }
  43. func html2BlockDOM(c *gin.Context) {
  44. ret := gulu.Ret.NewResult()
  45. defer c.JSON(http.StatusOK, ret)
  46. arg, ok := util.JsonArg(c, ret)
  47. if !ok {
  48. return
  49. }
  50. dom := arg["dom"].(string)
  51. markdown, err := model.HTML2Markdown(dom)
  52. if nil != err {
  53. ret.Data = "Failed to convert"
  54. return
  55. }
  56. luteEngine := util.NewLute()
  57. var unlinks []*ast.Node
  58. tree := parse.Parse("", []byte(markdown), luteEngine.ParseOptions)
  59. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  60. if !entering {
  61. return ast.WalkContinue
  62. }
  63. if ast.NodeListItem == n.Type && nil == n.FirstChild {
  64. newNode := treenode.NewParagraph()
  65. n.AppendChild(newNode)
  66. n.SetIALAttr("updated", util.TimeFromID(newNode.ID))
  67. return ast.WalkSkipChildren
  68. } else if ast.NodeBlockquote == n.Type && nil == n.FirstChild.Next {
  69. unlinks = append(unlinks, n)
  70. }
  71. return ast.WalkContinue
  72. })
  73. for _, n := range unlinks {
  74. n.Unlink()
  75. }
  76. // 表格只包含一个单元格时,将其转换为段落
  77. // Copy one cell from Excel/HTML table and paste it using the cell's content https://github.com/siyuan-note/siyuan/issues/9614
  78. unlinks = nil
  79. if nil != tree.Root.FirstChild && ast.NodeTable == tree.Root.FirstChild.Type && (nil == tree.Root.FirstChild.Next ||
  80. (ast.NodeKramdownBlockIAL == tree.Root.FirstChild.Next.Type && nil == tree.Root.FirstChild.Next.Next)) {
  81. if nil != tree.Root.FirstChild.FirstChild && ast.NodeTableHead == tree.Root.FirstChild.FirstChild.Type {
  82. head := tree.Root.FirstChild.FirstChild
  83. if nil == head.Next && nil != head.FirstChild && nil == head.FirstChild.Next {
  84. row := head.FirstChild
  85. if nil != row.FirstChild && nil == row.FirstChild.Next {
  86. cell := row.FirstChild
  87. p := treenode.NewParagraph()
  88. var contents []*ast.Node
  89. for c := cell.FirstChild; nil != c; c = c.Next {
  90. contents = append(contents, c)
  91. }
  92. for _, c := range contents {
  93. p.AppendChild(c)
  94. }
  95. tree.Root.FirstChild.Unlink()
  96. tree.Root.PrependChild(p)
  97. }
  98. }
  99. }
  100. }
  101. if util.ContainerStd == model.Conf.System.Container {
  102. // 处理本地资源文件复制
  103. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  104. if !entering || ast.NodeLinkDest != n.Type {
  105. return ast.WalkContinue
  106. }
  107. if "" == n.TokensStr() {
  108. return ast.WalkContinue
  109. }
  110. localPath := n.TokensStr()
  111. if strings.HasPrefix(localPath, "http") {
  112. return ast.WalkContinue
  113. }
  114. localPath = strings.TrimPrefix(localPath, "file://")
  115. if gulu.OS.IsWindows() {
  116. localPath = strings.TrimPrefix(localPath, "/")
  117. }
  118. unescaped, _ := url.PathUnescape(localPath)
  119. if unescaped != localPath {
  120. // `Convert network images/assets to local` supports URL-encoded local file names https://github.com/siyuan-note/siyuan/issues/9929
  121. localPath = unescaped
  122. }
  123. if !filepath.IsAbs(localPath) {
  124. // Kernel crash when copy-pasting from some browsers https://github.com/siyuan-note/siyuan/issues/9203
  125. return ast.WalkContinue
  126. }
  127. if !gulu.File.IsExist(localPath) {
  128. return ast.WalkContinue
  129. }
  130. name := filepath.Base(localPath)
  131. ext := filepath.Ext(name)
  132. name = name[0 : len(name)-len(ext)]
  133. name = name + "-" + ast.NewNodeID() + ext
  134. targetPath := filepath.Join(util.DataDir, "assets", name)
  135. if err = filelock.Copy(localPath, targetPath); nil != err {
  136. logging.LogErrorf("copy asset from [%s] to [%s] failed: %s", localPath, targetPath, err)
  137. return ast.WalkStop
  138. }
  139. n.Tokens = gulu.Str.ToBytes("assets/" + name)
  140. return ast.WalkContinue
  141. })
  142. }
  143. // 复制带超链接的图片无法保存到本地 https://github.com/siyuan-note/siyuan/issues/5993
  144. parse.NestedInlines2FlattedSpans(tree, false)
  145. renderer := render.NewProtyleRenderer(tree, luteEngine.RenderOptions)
  146. output := renderer.Render()
  147. ret.Data = gulu.Str.FromBytes(output)
  148. }
  149. func spinBlockDOM(c *gin.Context) {
  150. ret := gulu.Ret.NewResult()
  151. defer c.JSON(http.StatusOK, ret)
  152. arg, ok := util.JsonArg(c, ret)
  153. if !ok {
  154. return
  155. }
  156. dom := arg["dom"].(string)
  157. luteEngine := model.NewLute()
  158. dom = luteEngine.SpinBlockDOM(dom)
  159. ret.Data = map[string]interface{}{
  160. "dom": dom,
  161. }
  162. }