lute.go 5.4 KB

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