lute.go 3.9 KB

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