lute.go 3.7 KB

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