Преглед на файлове

:art: The browser extension supports CSDN formula https://github.com/siyuan-note/siyuan/issues/5624

Daniel преди 1 година
родител
ревизия
d3754bd630
променени са 5 файла, в които са добавени 34 реда и са изтрити 9 реда
  1. 7 4
      kernel/api/extension.go
  2. 7 1
      kernel/api/filetree.go
  3. 5 1
      kernel/api/lute.go
  4. 4 1
      kernel/model/file.go
  5. 11 2
      kernel/model/import.go

+ 7 - 4
kernel/api/extension.go

@@ -121,10 +121,12 @@ func extensionCopy(c *gin.Context) {
 		uploaded[oName] = "assets/" + fName
 	}
 
-	luteEngine := util.NewStdLute()
-	md, _ := model.HTML2Markdown(dom)
+	md, withMath, _ := model.HTML2Markdown(dom)
 	md = strings.TrimSpace(md)
-
+	luteEngine := util.NewStdLute()
+	if withMath {
+		luteEngine.SetInlineMath(true)
+	}
 	var unlinks []*ast.Node
 	tree := parse.Parse("", []byte(md), luteEngine.ParseOptions)
 	ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
@@ -153,7 +155,8 @@ func extensionCopy(c *gin.Context) {
 
 	md, _ = lute.FormatNodeSync(tree.Root, luteEngine.ParseOptions, luteEngine.RenderOptions)
 	ret.Data = map[string]interface{}{
-		"md": md,
+		"md":       md,
+		"withMath": withMath,
 	}
 	ret.Msg = model.Conf.Language(72)
 }

+ 7 - 1
kernel/api/filetree.go

@@ -677,7 +677,13 @@ func createDocWithMd(c *gin.Context) {
 		hPath = "/" + hPath
 	}
 
-	id, err := model.CreateWithMarkdown(notebook, hPath, markdown, parentID, id)
+	withMath := false
+	withMathArg := arg["withMath"]
+	if nil != withMathArg {
+		withMath = withMathArg.(bool)
+	}
+
+	id, err := model.CreateWithMarkdown(notebook, hPath, markdown, parentID, id, withMath)
 	if nil != err {
 		ret.Code = -1
 		ret.Msg = err.Error()

+ 5 - 1
kernel/api/lute.go

@@ -57,13 +57,17 @@ func html2BlockDOM(c *gin.Context) {
 	}
 
 	dom := arg["dom"].(string)
-	markdown, err := model.HTML2Markdown(dom)
+	markdown, withMath, err := model.HTML2Markdown(dom)
 	if nil != err {
 		ret.Data = "Failed to convert"
 		return
 	}
 
 	luteEngine := util.NewLute()
+	if withMath {
+		luteEngine.SetInlineMath(true)
+	}
+
 	var unlinks []*ast.Node
 	tree := parse.Parse("", []byte(markdown), luteEngine.ParseOptions)
 	ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {

+ 4 - 1
kernel/model/file.go

@@ -1138,7 +1138,7 @@ func CreateDocByMd(boxID, p, title, md string, sorts []string) (tree *parse.Tree
 	return
 }
 
-func CreateWithMarkdown(boxID, hPath, md, parentID, id string) (retID string, err error) {
+func CreateWithMarkdown(boxID, hPath, md, parentID, id string, withMath bool) (retID string, err error) {
 	createDocLock.Lock()
 	defer createDocLock.Unlock()
 
@@ -1150,6 +1150,9 @@ func CreateWithMarkdown(boxID, hPath, md, parentID, id string) (retID string, er
 
 	WaitForWritingFiles()
 	luteEngine := util.NewLute()
+	if withMath {
+		luteEngine.SetInlineMath(true)
+	}
 	dom := luteEngine.Md2BlockDOM(md, false)
 	retID, err = createDocsByHPath(box.ID, hPath, dom, parentID, id)
 	WaitForWritingFiles()

+ 11 - 2
kernel/model/import.go

@@ -55,12 +55,21 @@ import (
 	"github.com/siyuan-note/siyuan/kernel/util"
 )
 
-func HTML2Markdown(htmlStr string) (markdown string, err error) {
+func HTML2Markdown(htmlStr string) (markdown string, withMath bool, err error) {
 	assetDirPath := filepath.Join(util.DataDir, "assets")
 	luteEngine := util.NewLute()
 	tree := luteEngine.HTML2Tree(htmlStr)
 	ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
-		if !entering || ast.NodeLinkDest != n.Type {
+		if !entering {
+			return ast.WalkContinue
+		}
+
+		if ast.NodeInlineMath == n.Type {
+			withMath = true
+			return ast.WalkContinue
+		}
+
+		if ast.NodeLinkDest != n.Type {
 			return ast.WalkContinue
 		}