lute.go 5.7 KB

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