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

This commit is contained in:
Vanessa 2022-09-15 20:20:08 +08:00
commit f155b89c77
7 changed files with 121 additions and 9 deletions

File diff suppressed because one or more lines are too long

View file

@ -103,6 +103,12 @@ func WriteTree(tree *parse.Tree) (err error) {
treenode.ReindexBlockTree(tree)
}
filePath := filepath.Join(util.DataDir, tree.Box, tree.Path)
if oldSpec := tree.Root.Spec; "" == oldSpec {
treenode.NestedInlines2FlattedSpans(tree)
tree.Root.Spec = "1"
logging.LogInfof("migrated tree [%s] from spec [%s] to [%s]", filePath, oldSpec, tree.Root.Spec)
}
renderer := render.NewJSONRenderer(tree, luteEngine.RenderOptions)
output := renderer.Render()
@ -114,7 +120,6 @@ func WriteTree(tree *parse.Tree) (err error) {
}
output = buf.Bytes()
filePath := filepath.Join(util.DataDir, tree.Box, tree.Path)
if err = os.MkdirAll(filepath.Dir(filePath), 0755); nil != err {
return
}
@ -181,6 +186,14 @@ func parseJSON2Tree(boxID, p string, jsonData []byte, luteEngine *lute.Lute) (re
ret.Box = boxID
ret.Path = p
filePath := filepath.Join(util.DataDir, ret.Box, ret.Path)
if oldSpec := ret.Root.Spec; "" == oldSpec {
treenode.NestedInlines2FlattedSpans(ret)
ret.Root.Spec = "1"
needFix = true
logging.LogInfof("migrated tree [%s] from spec [%s] to [%s]", filePath, oldSpec, ret.Root.Spec)
}
if needFix {
renderer := render.NewJSONRenderer(ret, luteEngine.RenderOptions)
output := renderer.Render()
@ -192,7 +205,6 @@ func parseJSON2Tree(boxID, p string, jsonData []byte, luteEngine *lute.Lute) (re
}
output = buf.Bytes()
filePath := filepath.Join(util.DataDir, ret.Box, ret.Path)
if err = os.MkdirAll(filepath.Dir(filePath), 0755); nil != err {
return
}

View file

@ -7,7 +7,7 @@ require (
github.com/88250/css v0.1.2
github.com/88250/flock v0.8.2
github.com/88250/gulu v1.2.3-0.20220909041418-fdfa4d7380bf
github.com/88250/lute v1.7.5-0.20220915034356-e5dab9ee72c9
github.com/88250/lute v1.7.5-0.20220915112421-e006dc0c88af
github.com/88250/melody v0.0.0-20201115062536-c0b3394adcd1
github.com/88250/pdfcpu v0.3.13
github.com/88250/vitess-sqlparser v0.0.0-20210205111146-56a2ded2aba1

View file

@ -19,8 +19,8 @@ github.com/88250/go-sqlite3 v1.14.13-0.20220714142610-fbbda1ee84f5 h1:8HdZozCsXS
github.com/88250/go-sqlite3 v1.14.13-0.20220714142610-fbbda1ee84f5/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/88250/gulu v1.2.3-0.20220909041418-fdfa4d7380bf h1:q+iiBuKjdvUKj5dQQHCRs+g9oChTWkZmm7r1FMvtkfI=
github.com/88250/gulu v1.2.3-0.20220909041418-fdfa4d7380bf/go.mod h1:I1qBzsksFL2ciGSuqDE7R3XW4BUMrfDgOvSXEk7FsAI=
github.com/88250/lute v1.7.5-0.20220915034356-e5dab9ee72c9 h1:VIw3wMkTmA7nMYc2Tj7NGYrlAGRJZ2BADT4/k26tAQw=
github.com/88250/lute v1.7.5-0.20220915034356-e5dab9ee72c9/go.mod h1:cEoBGi0zArPqAsp0MdG9SKinvH/xxZZWXU7sRx8vHSA=
github.com/88250/lute v1.7.5-0.20220915112421-e006dc0c88af h1:Z74h1d4xMKVN7KnSSQll0UXqCxw+bYwpK1c4dsjaLCE=
github.com/88250/lute v1.7.5-0.20220915112421-e006dc0c88af/go.mod h1:cEoBGi0zArPqAsp0MdG9SKinvH/xxZZWXU7sRx8vHSA=
github.com/88250/melody v0.0.0-20201115062536-c0b3394adcd1 h1:9Cb+iN639vUI2OcIBc+4oGwml9/0J6bL6dWNb8Al+1s=
github.com/88250/melody v0.0.0-20201115062536-c0b3394adcd1/go.mod h1:jH6MMPr8G7AMzaVmWHXZQiB1DKO3giWbcWZ7UoJ1teI=
github.com/88250/pdfcpu v0.3.13 h1:touMWMZkCGalMIbEg9bxYp7rETM+zwb9hXjwhqi4I7Q=

View file

@ -614,7 +614,7 @@ func GetDoc(startID, endID, id string, index int, keyword string, mode int, size
}
}
subTree := &parse.Tree{Root: &ast.Node{Type: ast.NodeDocument}, Marks: tree.Marks}
subTree := &parse.Tree{ID: rootID, Root: &ast.Node{Type: ast.NodeDocument}, Marks: tree.Marks}
keyword = strings.Join(strings.Split(keyword, " "), search.TermSep)
keywords := search.SplitKeyword(keyword)
@ -1433,6 +1433,7 @@ func createDoc(boxID, p, title, dom string) (err error) {
tree.HPath = hPath
tree.ID = id
tree.Root.ID = id
tree.Root.Spec = "1"
updated := util.TimeFromID(id)
tree.Root.KramdownIAL = [][]string{{"id", id}, {"title", html.EscapeAttrVal(title)}, {"updated", updated}}
if nil == tree.Root.FirstChild {

View file

@ -144,6 +144,10 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {
// 写回 .sy
for _, tree := range trees {
syPath := filepath.Join(unzipRootPath, tree.Path)
if "" == tree.Root.Spec {
treenode.NestedInlines2FlattedSpans(tree)
tree.Root.Spec = "1"
}
renderer := render.NewJSONRenderer(tree, luteEngine.RenderOptions)
data := renderer.Render()
@ -451,6 +455,8 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
tree.Path = targetPath
targetPaths[curRelPath] = targetPath
tree.HPath = hPath
tree.Root.Spec = "1"
treenode.NestedInlines2FlattedSpans(tree)
docDirLocalPath := filepath.Dir(filepath.Join(boxLocalPath, targetPath))
assetDirPath := getAssetsDir(boxLocalPath, docDirLocalPath)
@ -538,6 +544,7 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
tree.Box = boxID
tree.Path = targetPath
tree.HPath = path.Join(baseHPath, title)
treenode.NestedInlines2FlattedSpans(tree)
docDirLocalPath := filepath.Dir(filepath.Join(boxLocalPath, targetPath))
assetDirPath := getAssetsDir(boxLocalPath, docDirLocalPath)

View file

@ -31,6 +31,98 @@ import (
"github.com/siyuan-note/logging"
)
// NestedInlines2FlattedSpans 将嵌套的行级节点转换为平铺的文本标记节点。
func NestedInlines2FlattedSpans(tree *parse.Tree) {
defer logging.Recover()
var tags []string
var unlinks []*ast.Node
var span *ast.Node
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
switch n.Type {
case ast.NodeCodeSpan:
processNestedNode(n, "code", &tags, &unlinks, entering)
case ast.NodeTag:
processNestedNode(n, "tag", &tags, &unlinks, entering)
case ast.NodeInlineMath:
processNestedNode(n, "inline-math", &tags, &unlinks, entering)
case ast.NodeEmphasis:
processNestedNode(n, "em", &tags, &unlinks, entering)
case ast.NodeStrong:
processNestedNode(n, "strong", &tags, &unlinks, entering)
case ast.NodeStrikethrough:
processNestedNode(n, "s", &tags, &unlinks, entering)
case ast.NodeMark:
processNestedNode(n, "mark", &tags, &unlinks, entering)
case ast.NodeUnderline:
processNestedNode(n, "u", &tags, &unlinks, entering)
case ast.NodeSub:
processNestedNode(n, "sub", &tags, &unlinks, entering)
case ast.NodeSup:
processNestedNode(n, "sup", &tags, &unlinks, entering)
case ast.NodeKbd:
processNestedNode(n, "kbd", &tags, &unlinks, entering)
case ast.NodeLink:
processNestedNode(n, "a", &tags, &unlinks, entering)
case ast.NodeText, ast.NodeCodeSpanContent, ast.NodeInlineMathContent, ast.NodeLinkText:
if 1 > len(tags) {
return ast.WalkContinue
}
if entering {
span = &ast.Node{Type: ast.NodeTextMark, TextMarkType: strings.Join(tags, " "), TextMarkTextContent: string(n.Tokens)}
if ast.NodeInlineMathContent == n.Type {
span.TextMarkTextContent = ""
span.TextMarkInlineMathContent = string(n.Tokens)
}
if ast.NodeLinkText == n.Type && !n.ParentIs(ast.NodeImage) {
var link *ast.Node
for p := n.Parent; nil != p; p = p.Parent {
if ast.NodeLink == p.Type {
link = p
break
}
}
if nil != link {
dest := link.ChildByType(ast.NodeLinkDest)
if nil != dest {
span.TextMarkAHref = string(dest.Tokens)
}
title := link.ChildByType(ast.NodeLinkTitle)
if nil != title {
span.TextMarkATitle = string(title.Tokens)
}
}
}
} else {
span.KramdownIAL = n.Parent.KramdownIAL
n.Parent.InsertBefore(span)
}
}
return ast.WalkContinue
})
for _, n := range unlinks {
n.Unlink()
}
}
func processNestedNode(n *ast.Node, tag string, tags *[]string, unlinks *[]*ast.Node, entering bool) {
if entering {
*tags = append(*tags, tag)
} else {
*tags = (*tags)[:len(*tags)-1]
*unlinks = append(*unlinks, n)
for c := n.FirstChild; nil != c; {
next := c.Next
if ast.NodeTextMark == c.Type {
n.InsertBefore(c)
}
c = next
}
}
}
func NodeStaticMdContent(node *ast.Node, luteEngine *lute.Lute) (md, content string) {
md = FormatNode(node, luteEngine)
content = NodeStaticContent(node)