Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:technologist: Add kernel API `/api/lute/md2html` (#17697)

Jeffrey Chen04915380b1cd
diff --git a/kernel/api/lute.go b/kernel/api/lute.go
index be3af2cdc4..d4ae9c7de5 100644
--- a/kernel/api/lute.go
+++ b/kernel/api/lute.go
@@ -221,3 +221,38 @@ func spinBlockDOM(c *gin.Context) {
"dom": dom,
}
}
+
+// md2HTML 将 Markdown 转换为 HTML。
+func md2HTML(c *gin.Context) {
+ ret := gulu.Ret.NewResult()
+ defer c.JSON(http.StatusOK, ret)
+
+ arg, ok := util.JsonArg(c, ret)
+ if !ok {
+ return
+ }
+
+ var markdown, mode string
+ if !util.ParseJsonArgs(arg, ret,
+ util.BindJsonArg("markdown", &markdown, true, false),
+ util.BindJsonArg("mode", &mode, false, false),
+ ) {
+ return
+ }
+
+ var html string
+ switch mode {
+ case "protyle-preview":
+ html = model.MarkdownToProtylePreviewHTML(markdown)
+ case "":
+ html = model.MarkdownToMarkdownStrHTML(markdown)
+ default:
+ ret.Code = -1
+ ret.Msg = "unknown [mode]"
+ return
+ }
+
+ ret.Data = map[string]any{
+ "html": html,
+ }
+}
diff --git a/kernel/api/router.go b/kernel/api/router.go
index af3c2dff21..38033cb65a 100644
--- a/kernel/api/router.go
+++ b/kernel/api/router.go
@@ -181,6 +181,7 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/lute/spinBlockDOM", model.CheckAuth, spinBlockDOM) // 未测试
ginServer.Handle("POST", "/api/lute/html2BlockDOM", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, html2BlockDOM)
ginServer.Handle("POST", "/api/lute/copyStdMarkdown", model.CheckAuth, copyStdMarkdown)
+ ginServer.Handle("POST", "/api/lute/md2html", model.CheckAuth, md2HTML)
ginServer.Handle("POST", "/api/query/sql", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, SQL)
ginServer.Handle("POST", "/api/sqlite/flushTransaction", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, flushTransaction)
diff --git a/kernel/model/md2html.go b/kernel/model/md2html.go
new file mode 100644
index 0000000000..6cd197e97e
--- /dev/null
+++ b/kernel/model/md2html.go
@@ -0,0 +1,60 @@
+// SiYuan - Refactor your thinking
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+package model
+
+import (
+ "sync"
+
+ "github.com/88250/lute"
+ "github.com/88250/lute/parse"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+var md2HTMLLutePool sync.Pool
+
+func getMd2HTMLLuteFromPool() *lute.Lute {
+ v := md2HTMLLutePool.Get()
+ if v == nil {
+ e := util.NewLute()
+ // 与 Conf 解耦
+ e.SetCodeSyntaxHighlightLineNum(false)
+ e.SetChineseParagraphBeginningSpace(false)
+ e.SetProtyleMarkNetImg(false)
+ e.SetSpellcheck(false)
+ e.SetFootnotes(true)
+ enableLuteInlineSyntax(e)
+ return e
+ }
+ return v.(*lute.Lute)
+}
+
+func MarkdownToProtylePreviewHTML(markdown string) string {
+ eng := getMd2HTMLLuteFromPool()
+ defer md2HTMLLutePool.Put(eng)
+
+ tree := parse.Parse("", []byte(markdown), eng.ParseOptions)
+ if nil == tree || nil == tree.Root {
+ return ""
+ }
+ return eng.ProtylePreview(tree, eng.RenderOptions, eng.ParseOptions)
+}
+
+func MarkdownToMarkdownStrHTML(markdown string) string {
+ eng := getMd2HTMLLuteFromPool()
+ defer md2HTMLLutePool.Put(eng)
+ return eng.MarkdownStr("", markdown)
+}