From 371c64c47192fcc5d7f656504150f9905b1d4b9f Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Wed, 20 Nov 2024 10:33:42 +0800 Subject: [PATCH] :art: Improve kernel API `/api/block/getBlockKramdown` https://github.com/siyuan-note/siyuan/issues/13183 --- kernel/api/block.go | 15 ++++++++++++++- kernel/model/block.go | 11 +++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/kernel/api/block.go b/kernel/api/block.go index b77db1ed5..02c6cdf4e 100644 --- a/kernel/api/block.go +++ b/kernel/api/block.go @@ -603,7 +603,20 @@ func getBlockKramdown(c *gin.Context) { return } - kramdown := model.GetBlockKramdown(id) + // md:Markdown 标记符模式,使用标记符导出 + // textmark:文本标记模式,使用 span 标签导出 + // https://github.com/siyuan-note/siyuan/issues/13183 + mode := "md" + if modeArg := arg["mode"]; nil != modeArg { + mode = modeArg.(string) + if "md" != mode && "textmark" != mode { + ret.Code = -1 + ret.Msg = "Invalid mode" + return + } + } + + kramdown := model.GetBlockKramdown(id, mode) ret.Data = map[string]string{ "id": id, "kramdown": kramdown, diff --git a/kernel/model/block.go b/kernel/model/block.go index 1a41f0b4a..0f44f189b 100644 --- a/kernel/model/block.go +++ b/kernel/model/block.go @@ -20,6 +20,7 @@ import ( "bytes" "errors" "fmt" + "github.com/88250/lute/render" "strconv" "strings" "time" @@ -617,7 +618,7 @@ func GetBlockDOM(id string) (ret string) { return } -func GetBlockKramdown(id string) (ret string) { +func GetBlockKramdown(id, mode string) (ret string) { if "" == id { return } @@ -633,7 +634,13 @@ func GetBlockKramdown(id string) (ret string) { root.AppendChild(node.Next) // IAL root.PrependChild(node) luteEngine := NewLute() - ret = treenode.ExportNodeStdMd(root, luteEngine) + if "md" == mode { + ret = treenode.ExportNodeStdMd(root, luteEngine) + } else { + tree.Root = root + formatRenderer := render.NewFormatRenderer(tree, luteEngine.RenderOptions) + ret = string(formatRenderer.Render()) + } return }