From 2d1b364dfee21444d3ab91c7c063b9ab23577f56 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sun, 9 Oct 2022 22:07:29 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=B5=8F=E8=A7=88=E5=99=A8=E5=89=AA?= =?UTF-8?q?=E8=97=8F=E6=89=A9=E5=B1=95=E6=94=B9=E8=BF=9B=20https://github.?= =?UTF-8?q?com/siyuan-note/siyuan/issues/6124?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/api/extension.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/kernel/api/extension.go b/kernel/api/extension.go index 8124903cd..deeb65353 100644 --- a/kernel/api/extension.go +++ b/kernel/api/extension.go @@ -27,7 +27,9 @@ import ( "strings" "github.com/88250/gulu" + "github.com/88250/lute" "github.com/88250/lute/ast" + "github.com/88250/lute/parse" "github.com/gin-gonic/gin" "github.com/siyuan-note/filelock" "github.com/siyuan-note/logging" @@ -58,6 +60,35 @@ func extensionCopy(c *gin.Context) { luteEngine := model.NewLute() md := luteEngine.HTML2Md(dom) md = strings.TrimSpace(md) + + var unlinks []*ast.Node + tree := parse.Parse("", []byte(md), luteEngine.ParseOptions) + ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus { + if !entering { + return ast.WalkContinue + } + + // 浏览器剪藏扩展改进 https://github.com/siyuan-note/siyuan/issues/6124 + if ast.NodeInlineMath == n.Type { + // $ 转义 + text := &ast.Node{Type: ast.NodeText} + text.Tokens = []byte("\\$" + string(n.ChildByType(ast.NodeInlineMathContent).Tokens) + "\\$") + n.InsertBefore(text) + unlinks = append(unlinks, n) + return ast.WalkSkipChildren + } else if ast.NodeText == n.Type { + // 剔除行首空白 + if ast.NodeParagraph == n.Parent.Type && n.Parent.FirstChild == n { + n.Tokens = bytes.TrimLeft(n.Tokens, " \t\n") + } + } + return ast.WalkContinue + }) + for _, unlink := range unlinks { + unlink.Unlink() + } + + md, _ = lute.FormatNodeSync(tree.Root, luteEngine.ParseOptions, luteEngine.RenderOptions) ret.Data = map[string]interface{}{ "md": md, }