lute.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. "github.com/siyuan-note/siyuan/kernel/treenode"
  19. "net/http"
  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/util"
  31. )
  32. func copyStdMarkdown(c *gin.Context) {
  33. ret := gulu.Ret.NewResult()
  34. defer c.JSON(http.StatusOK, ret)
  35. arg, ok := util.JsonArg(c, ret)
  36. if !ok {
  37. return
  38. }
  39. id := arg["id"].(string)
  40. ret.Data = model.ExportStdMarkdown(id)
  41. }
  42. func html2BlockDOM(c *gin.Context) {
  43. ret := gulu.Ret.NewResult()
  44. defer c.JSON(http.StatusOK, ret)
  45. arg, ok := util.JsonArg(c, ret)
  46. if !ok {
  47. return
  48. }
  49. dom := arg["dom"].(string)
  50. markdown, err := model.HTML2Markdown(dom)
  51. if nil != err {
  52. ret.Data = "Failed to convert"
  53. return
  54. }
  55. luteEngine := util.NewLute()
  56. var unlinks []*ast.Node
  57. tree := parse.Parse("", []byte(markdown), luteEngine.ParseOptions)
  58. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  59. if !entering {
  60. return ast.WalkContinue
  61. }
  62. if ast.NodeListItem == n.Type && nil == n.FirstChild {
  63. newNode := treenode.NewParagraph()
  64. n.AppendChild(newNode)
  65. n.SetIALAttr("updated", util.TimeFromID(newNode.ID))
  66. return ast.WalkSkipChildren
  67. } else if ast.NodeBlockquote == n.Type && nil == n.FirstChild.Next {
  68. unlinks = append(unlinks, n)
  69. }
  70. return ast.WalkContinue
  71. })
  72. for _, n := range unlinks {
  73. n.Unlink()
  74. }
  75. if util.ContainerStd == model.Conf.System.Container {
  76. // 处理本地资源文件复制
  77. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  78. if !entering || ast.NodeLinkDest != n.Type {
  79. return ast.WalkContinue
  80. }
  81. if "" == n.TokensStr() {
  82. return ast.WalkContinue
  83. }
  84. localPath := n.TokensStr()
  85. if strings.HasPrefix(localPath, "http") {
  86. return ast.WalkContinue
  87. }
  88. localPath = strings.TrimPrefix(localPath, "file://")
  89. if gulu.OS.IsWindows() {
  90. localPath = strings.TrimPrefix(localPath, "/")
  91. }
  92. if !filepath.IsAbs(localPath) {
  93. // Kernel crash when copy-pasting from some browsers https://github.com/siyuan-note/siyuan/issues/9203
  94. return ast.WalkContinue
  95. }
  96. if !gulu.File.IsExist(localPath) {
  97. return ast.WalkContinue
  98. }
  99. name := filepath.Base(localPath)
  100. ext := filepath.Ext(name)
  101. name = name[0 : len(name)-len(ext)]
  102. name = name + "-" + ast.NewNodeID() + ext
  103. targetPath := filepath.Join(util.DataDir, "assets", name)
  104. if err = filelock.Copy(localPath, targetPath); nil != err {
  105. logging.LogErrorf("copy asset from [%s] to [%s] failed: %s", localPath, targetPath, err)
  106. return ast.WalkStop
  107. }
  108. n.Tokens = gulu.Str.ToBytes("assets/" + name)
  109. return ast.WalkContinue
  110. })
  111. }
  112. // 复制带超链接的图片无法保存到本地 https://github.com/siyuan-note/siyuan/issues/5993
  113. parse.NestedInlines2FlattedSpans(tree)
  114. renderer := render.NewProtyleRenderer(tree, luteEngine.RenderOptions)
  115. output := renderer.Render()
  116. ret.Data = gulu.Str.FromBytes(output)
  117. }
  118. func spinBlockDOM(c *gin.Context) {
  119. ret := gulu.Ret.NewResult()
  120. defer c.JSON(http.StatusOK, ret)
  121. arg, ok := util.JsonArg(c, ret)
  122. if !ok {
  123. return
  124. }
  125. dom := arg["dom"].(string)
  126. luteEngine := model.NewLute()
  127. dom = luteEngine.SpinBlockDOM(dom)
  128. ret.Data = map[string]interface{}{
  129. "dom": dom,
  130. }
  131. }