mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-23 18:10:12 +01:00
🎨 Improve HTML clipping https://github.com/siyuan-note/siyuan/issues/13125#issuecomment-2475461284
This commit is contained in:
parent
ab147598e1
commit
e408546be5
3 changed files with 40 additions and 22 deletions
|
|
@ -123,7 +123,12 @@ func extensionCopy(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
luteEngine := util.NewLute()
|
luteEngine := util.NewLute()
|
||||||
luteEngine.SetHTMLTag2TextMark(true)
|
luteEngine.SetSup(true)
|
||||||
|
luteEngine.SetSub(true)
|
||||||
|
luteEngine.SetMark(true)
|
||||||
|
luteEngine.SetGFMStrikethrough(true)
|
||||||
|
luteEngine.SetInlineAsterisk(true)
|
||||||
|
luteEngine.SetInlineUnderscore(true)
|
||||||
var md string
|
var md string
|
||||||
var withMath bool
|
var withMath bool
|
||||||
if nil != form.Value["href"] {
|
if nil != form.Value["href"] {
|
||||||
|
|
@ -175,16 +180,19 @@ func extensionCopy(c *gin.Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var tree *parse.Tree
|
||||||
if "" == md {
|
if "" == md {
|
||||||
|
tree, withMath = model.HTML2Tree(dom, luteEngine)
|
||||||
|
if nil == tree {
|
||||||
md, withMath, _ = model.HTML2Markdown(dom, luteEngine)
|
md, withMath, _ = model.HTML2Markdown(dom, luteEngine)
|
||||||
}
|
|
||||||
|
|
||||||
md = strings.TrimSpace(md)
|
|
||||||
if withMath {
|
if withMath {
|
||||||
luteEngine.SetInlineMath(true)
|
luteEngine.SetInlineMath(true)
|
||||||
}
|
}
|
||||||
|
tree = parse.Parse("", []byte(md), luteEngine.ParseOptions)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var unlinks []*ast.Node
|
var unlinks []*ast.Node
|
||||||
tree := parse.Parse("", []byte(md), luteEngine.ParseOptions)
|
|
||||||
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
||||||
if !entering {
|
if !entering {
|
||||||
return ast.WalkContinue
|
return ast.WalkContinue
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,12 @@ func html2BlockDOM(c *gin.Context) {
|
||||||
|
|
||||||
dom := arg["dom"].(string)
|
dom := arg["dom"].(string)
|
||||||
luteEngine := util.NewLute()
|
luteEngine := util.NewLute()
|
||||||
luteEngine.SetHTMLTag2TextMark(true)
|
luteEngine.SetSup(true)
|
||||||
|
luteEngine.SetSub(true)
|
||||||
|
luteEngine.SetMark(true)
|
||||||
|
luteEngine.SetGFMStrikethrough(true)
|
||||||
|
luteEngine.SetInlineAsterisk(true)
|
||||||
|
luteEngine.SetInlineUnderscore(true)
|
||||||
markdown, withMath, err := model.HTML2Markdown(dom, luteEngine)
|
markdown, withMath, err := model.HTML2Markdown(dom, luteEngine)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ret.Data = "Failed to convert"
|
ret.Data = "Failed to convert"
|
||||||
|
|
|
||||||
|
|
@ -54,8 +54,21 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func HTML2Markdown(htmlStr string, luteEngine *lute.Lute) (markdown string, withMath bool, err error) {
|
func HTML2Markdown(htmlStr string, luteEngine *lute.Lute) (markdown string, withMath bool, err error) {
|
||||||
|
tree, withMath := HTML2Tree(htmlStr, luteEngine)
|
||||||
|
|
||||||
|
var formatted []byte
|
||||||
|
renderer := render.NewFormatRenderer(tree, luteEngine.RenderOptions)
|
||||||
|
for nodeType, rendererFunc := range luteEngine.HTML2MdRendererFuncs {
|
||||||
|
renderer.ExtRendererFuncs[nodeType] = rendererFunc
|
||||||
|
}
|
||||||
|
formatted = renderer.Render()
|
||||||
|
markdown = gulu.Str.FromBytes(formatted)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func HTML2Tree(htmlStr string, luteEngine *lute.Lute) (tree *parse.Tree, withMath bool) {
|
||||||
assetDirPath := filepath.Join(util.DataDir, "assets")
|
assetDirPath := filepath.Join(util.DataDir, "assets")
|
||||||
tree := luteEngine.HTML2Tree(htmlStr)
|
tree = luteEngine.HTML2Tree(htmlStr)
|
||||||
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
||||||
if !entering {
|
if !entering {
|
||||||
return ast.WalkContinue
|
return ast.WalkContinue
|
||||||
|
|
@ -79,19 +92,11 @@ func HTML2Markdown(htmlStr string, luteEngine *lute.Lute) (markdown string, with
|
||||||
|
|
||||||
dest := n.TokensStr()
|
dest := n.TokensStr()
|
||||||
if strings.HasPrefix(dest, "data:image") && strings.Contains(dest, ";base64,") {
|
if strings.HasPrefix(dest, "data:image") && strings.Contains(dest, ";base64,") {
|
||||||
processBase64Img(n, dest, assetDirPath, err)
|
processBase64Img(n, dest, assetDirPath)
|
||||||
return ast.WalkContinue
|
return ast.WalkContinue
|
||||||
}
|
}
|
||||||
return ast.WalkContinue
|
return ast.WalkContinue
|
||||||
})
|
})
|
||||||
|
|
||||||
var formatted []byte
|
|
||||||
renderer := render.NewFormatRenderer(tree, luteEngine.RenderOptions)
|
|
||||||
for nodeType, rendererFunc := range luteEngine.HTML2MdRendererFuncs {
|
|
||||||
renderer.ExtRendererFuncs[nodeType] = rendererFunc
|
|
||||||
}
|
|
||||||
formatted = renderer.Render()
|
|
||||||
markdown = gulu.Str.FromBytes(formatted)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -836,7 +841,7 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(dest, "data:image") && strings.Contains(dest, ";base64,") {
|
if strings.HasPrefix(dest, "data:image") && strings.Contains(dest, ";base64,") {
|
||||||
processBase64Img(n, dest, assetDirPath, err)
|
processBase64Img(n, dest, assetDirPath)
|
||||||
return ast.WalkContinue
|
return ast.WalkContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -953,7 +958,7 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(dest, "data:image") && strings.Contains(dest, ";base64,") {
|
if strings.HasPrefix(dest, "data:image") && strings.Contains(dest, ";base64,") {
|
||||||
processBase64Img(n, dest, assetDirPath, err)
|
processBase64Img(n, dest, assetDirPath)
|
||||||
return ast.WalkContinue
|
return ast.WalkContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1046,7 +1051,7 @@ func parseStdMd(markdown []byte) (ret *parse.Tree, yfmRootID, yfmTitle, yfmUpdat
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func processBase64Img(n *ast.Node, dest string, assetDirPath string, err error) {
|
func processBase64Img(n *ast.Node, dest string, assetDirPath string) {
|
||||||
base64TmpDir := filepath.Join(util.TempDir, "base64")
|
base64TmpDir := filepath.Join(util.TempDir, "base64")
|
||||||
os.MkdirAll(base64TmpDir, 0755)
|
os.MkdirAll(base64TmpDir, 0755)
|
||||||
|
|
||||||
|
|
@ -1110,7 +1115,7 @@ func processBase64Img(n *ast.Node, dest string, assetDirPath string, err error)
|
||||||
tmpFile.Close()
|
tmpFile.Close()
|
||||||
|
|
||||||
assetTargetPath := filepath.Join(assetDirPath, name)
|
assetTargetPath := filepath.Join(assetDirPath, name)
|
||||||
if err = filelock.Copy(tmp, assetTargetPath); err != nil {
|
if err := filelock.Copy(tmp, assetTargetPath); err != nil {
|
||||||
logging.LogErrorf("copy asset from [%s] to [%s] failed: %s", tmp, assetTargetPath, err)
|
logging.LogErrorf("copy asset from [%s] to [%s] failed: %s", tmp, assetTargetPath, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue