Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Vanessa 2024-05-21 00:23:18 +08:00
commit 8db6015709
6 changed files with 60 additions and 23 deletions

View file

@ -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)
}

View file

@ -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()

View file

@ -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 {

View file

@ -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()
@ -1666,7 +1669,7 @@ func RenameDoc(boxID, p, title string) (err error) {
return
}
title = gulu.Str.RemoveInvisible(title)
title = removeInvisibleCharsInTitle(title)
if 512 < utf8.RuneCountInString(title) {
// 限制笔记本名和文档名最大长度为 `512` https://github.com/siyuan-note/siyuan/issues/6299
return errors.New(Conf.Language(106))
@ -1706,7 +1709,7 @@ func RenameDoc(boxID, p, title string) (err error) {
}
func createDoc(boxID, p, title, dom string) (tree *parse.Tree, err error) {
title = gulu.Str.RemoveInvisible(title)
title = removeInvisibleCharsInTitle(title)
if 512 < utf8.RuneCountInString(title) {
// 限制笔记本名和文档名最大长度为 `512` https://github.com/siyuan-note/siyuan/issues/6299
err = errors.New(Conf.Language(106))
@ -1819,6 +1822,14 @@ func createDoc(boxID, p, title, dom string) (tree *parse.Tree, err error) {
return
}
func removeInvisibleCharsInTitle(title string) string {
// 不要踢掉 零宽连字符,否则有的 Emoji 会变形 https://github.com/siyuan-note/siyuan/issues/11480
title = strings.ReplaceAll(title, string(gulu.ZWJ), "__@ZWJ@__")
title = gulu.Str.RemoveInvisible(title)
title = strings.ReplaceAll(title, "__@ZWJ@__", string(gulu.ZWJ))
return title
}
func moveSorts(rootID, fromBox, toBox string) {
root := treenode.GetBlockTree(rootID)
if nil == root {

View file

@ -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
}

View file

@ -451,6 +451,7 @@ func InitBlockTree(force bool) {
return
}
loadErr := atomic.Bool{}
size := atomic.Int64{}
waitGroup := &sync.WaitGroup{}
p, _ := ants.NewPoolWithFunc(4, func(arg interface{}) {
@ -462,14 +463,15 @@ func InitBlockTree(force bool) {
f, err := os.OpenFile(p, os.O_RDONLY, 0644)
if nil != err {
logging.LogErrorf("open block tree failed: %s", err)
os.Exit(logging.ExitCodeFileSysErr)
loadErr.Store(true)
return
}
defer f.Close()
info, err := f.Stat()
if nil != err {
logging.LogErrorf("stat block tree failed: %s", err)
os.Exit(logging.ExitCodeFileSysErr)
loadErr.Store(true)
return
}
size.Add(info.Size())
@ -477,16 +479,7 @@ func InitBlockTree(force bool) {
sliceData := map[string]*BlockTree{}
if err = msgpack.NewDecoder(f).Decode(&sliceData); nil != err {
logging.LogErrorf("unmarshal block tree failed: %s", err)
if err = os.RemoveAll(util.BlockTreePath); nil != err {
logging.LogErrorf("removed corrupted block tree failed: %s", err)
}
os.Exit(logging.ExitCodeFileSysErr)
return
}
if err = f.Close(); nil != err {
logging.LogErrorf("close block tree failed: %s", err)
os.Exit(logging.ExitCodeFileSysErr)
loadErr.Store(true)
return
}
@ -505,6 +498,17 @@ func InitBlockTree(force bool) {
waitGroup.Wait()
p.Release()
if loadErr.Load() {
logging.LogInfof("cause block tree load error, remove block tree file")
if removeErr := os.RemoveAll(util.BlockTreePath); nil != removeErr {
logging.LogErrorf("remove block tree file failed: %s", removeErr)
os.Exit(logging.ExitCodeFileSysErr)
return
}
blockTrees = &sync.Map{}
return
}
elapsed := time.Since(start).Seconds()
logging.LogInfof("read block tree [%s] to [%s], elapsed [%.2fs]", humanize.BytesCustomCeil(uint64(size.Load()), 2), util.BlockTreePath, elapsed)
return