🎨 遗留的 Markdown 嵌套节点迁移为平铺的文本标记节点

This commit is contained in:
Liang Ding 2022-09-19 23:58:00 +08:00
parent 6851656c09
commit 7ede0cbf52
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
3 changed files with 6 additions and 97 deletions

View file

@ -105,7 +105,7 @@ func WriteTree(tree *parse.Tree) (err error) {
filePath := filepath.Join(util.DataDir, tree.Box, tree.Path) filePath := filepath.Join(util.DataDir, tree.Box, tree.Path)
if oldSpec := tree.Root.Spec; "" == oldSpec { if oldSpec := tree.Root.Spec; "" == oldSpec {
treenode.NestedInlines2FlattedSpans(tree) luteEngine.NestedInlines2FlattedSpans(tree)
tree.Root.Spec = "1" tree.Root.Spec = "1"
logging.LogInfof("migrated tree [%s] from spec [%s] to [%s]", filePath, oldSpec, tree.Root.Spec) logging.LogInfof("migrated tree [%s] from spec [%s] to [%s]", filePath, oldSpec, tree.Root.Spec)
} }
@ -190,7 +190,7 @@ func parseJSON2Tree(boxID, p string, jsonData []byte, luteEngine *lute.Lute) (re
filePath := filepath.Join(util.DataDir, ret.Box, ret.Path) filePath := filepath.Join(util.DataDir, ret.Box, ret.Path)
if oldSpec := ret.Root.Spec; "" == oldSpec { if oldSpec := ret.Root.Spec; "" == oldSpec {
treenode.NestedInlines2FlattedSpans(ret) luteEngine.NestedInlines2FlattedSpans(ret)
ret.Root.Spec = "1" ret.Root.Spec = "1"
needFix = true needFix = true
logging.LogInfof("migrated tree [%s] from spec [%s] to [%s]", filePath, oldSpec, ret.Root.Spec) logging.LogInfof("migrated tree [%s] from spec [%s] to [%s]", filePath, oldSpec, ret.Root.Spec)

View file

@ -150,7 +150,7 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {
for _, tree := range trees { for _, tree := range trees {
syPath := filepath.Join(unzipRootPath, tree.Path) syPath := filepath.Join(unzipRootPath, tree.Path)
if "" == tree.Root.Spec { if "" == tree.Root.Spec {
treenode.NestedInlines2FlattedSpans(tree) luteEngine.NestedInlines2FlattedSpans(tree)
tree.Root.Spec = "1" tree.Root.Spec = "1"
} }
renderer := render.NewJSONRenderer(tree, luteEngine.RenderOptions) renderer := render.NewJSONRenderer(tree, luteEngine.RenderOptions)
@ -367,6 +367,7 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
} }
boxLocalPath = filepath.Join(util.DataDir, boxID) boxLocalPath = filepath.Join(util.DataDir, boxID)
luteEngine := NewLute()
if gulu.File.IsDir(localPath) { if gulu.File.IsDir(localPath) {
// 收集所有资源文件 // 收集所有资源文件
assets := map[string]string{} assets := map[string]string{}
@ -459,7 +460,7 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
targetPaths[curRelPath] = targetPath targetPaths[curRelPath] = targetPath
tree.HPath = hPath tree.HPath = hPath
tree.Root.Spec = "1" tree.Root.Spec = "1"
treenode.NestedInlines2FlattedSpans(tree) luteEngine.NestedInlines2FlattedSpans(tree)
docDirLocalPath := filepath.Dir(filepath.Join(boxLocalPath, targetPath)) docDirLocalPath := filepath.Dir(filepath.Join(boxLocalPath, targetPath))
assetDirPath := getAssetsDir(boxLocalPath, docDirLocalPath) assetDirPath := getAssetsDir(boxLocalPath, docDirLocalPath)
@ -547,7 +548,7 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
tree.Box = boxID tree.Box = boxID
tree.Path = targetPath tree.Path = targetPath
tree.HPath = path.Join(baseHPath, title) tree.HPath = path.Join(baseHPath, title)
treenode.NestedInlines2FlattedSpans(tree) luteEngine.NestedInlines2FlattedSpans(tree)
docDirLocalPath := filepath.Dir(filepath.Join(boxLocalPath, targetPath)) docDirLocalPath := filepath.Dir(filepath.Join(boxLocalPath, targetPath))
assetDirPath := getAssetsDir(boxLocalPath, docDirLocalPath) assetDirPath := getAssetsDir(boxLocalPath, docDirLocalPath)

View file

@ -75,98 +75,6 @@ func IsBlockRef(n *ast.Node) bool {
return false return false
} }
// 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(html.EscapeHTML(n.Tokens))}
if ast.NodeInlineMathContent == n.Type {
span.TextMarkTextContent = ""
span.TextMarkInlineMathContent = string(html.EscapeHTML(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) { func NodeStaticMdContent(node *ast.Node, luteEngine *lute.Lute) (md, content string) {
md = ExportNodeStdMd(node, luteEngine) md = ExportNodeStdMd(node, luteEngine)
content = NodeStaticContent(node) content = NodeStaticContent(node)