mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-24 02:20:13 +01:00
🐛 Embed blocks are exported incorrectly https://github.com/siyuan-note/siyuan/issues/16547
Signed-off-by: Daniel <845765@qq.com>
This commit is contained in:
parent
ad48451d51
commit
a110f902d5
1 changed files with 138 additions and 135 deletions
|
|
@ -256,165 +256,168 @@ func resolveEmbedR(n *ast.Node, blockEmbedMode int, luteEngine *lute.Lute, resol
|
||||||
return ast.WalkContinue
|
return ast.WalkContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
if ast.NodeBlockQueryEmbed == n.Type {
|
if ast.NodeBlockQueryEmbed != n.Type {
|
||||||
if gulu.Str.Contains(n.ID, *resolved) {
|
return ast.WalkContinue
|
||||||
return ast.WalkContinue
|
}
|
||||||
|
|
||||||
|
if gulu.Str.Contains(n.ID, *resolved) {
|
||||||
|
return ast.WalkContinue
|
||||||
|
}
|
||||||
|
*resolved = append(*resolved, n.ID)
|
||||||
|
|
||||||
|
stmt := n.ChildByType(ast.NodeBlockQueryEmbedScript).TokensStr()
|
||||||
|
stmt = html.UnescapeString(stmt)
|
||||||
|
stmt = strings.ReplaceAll(stmt, editor.IALValEscNewLine, "\n")
|
||||||
|
sqlBlocks := sql.SelectBlocksRawStmt(stmt, 1, Conf.Search.Limit)
|
||||||
|
for _, sqlBlock := range sqlBlocks {
|
||||||
|
if "query_embed" == sqlBlock.Type {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
*resolved = append(*resolved, n.ID)
|
|
||||||
|
|
||||||
stmt := n.ChildByType(ast.NodeBlockQueryEmbedScript).TokensStr()
|
subTree, _ := LoadTreeByBlockID(sqlBlock.ID)
|
||||||
stmt = html.UnescapeString(stmt)
|
if nil == subTree {
|
||||||
stmt = strings.ReplaceAll(stmt, editor.IALValEscNewLine, "\n")
|
continue
|
||||||
sqlBlocks := sql.SelectBlocksRawStmt(stmt, 1, Conf.Search.Limit)
|
}
|
||||||
for _, sqlBlock := range sqlBlocks {
|
|
||||||
if "query_embed" == sqlBlock.Type {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
subTree, _ := LoadTreeByBlockID(sqlBlock.ID)
|
var md string
|
||||||
if nil == subTree {
|
if "d" == sqlBlock.Type {
|
||||||
continue
|
if 0 == blockEmbedMode {
|
||||||
}
|
// 嵌入块中出现了大于等于上方非嵌入块的标题时需要降低嵌入块中的标题级别
|
||||||
|
// Improve export of heading levels in embedded blocks https://github.com/siyuan-note/siyuan/issues/12233 https://github.com/siyuan-note/siyuan/issues/12741
|
||||||
var md string
|
embedTopLevel := 0
|
||||||
if "d" == sqlBlock.Type {
|
ast.Walk(subTree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
||||||
if 0 == blockEmbedMode {
|
if !entering || ast.NodeHeading != n.Type {
|
||||||
// 嵌入块中出现了大于等于上方非嵌入块的标题时需要降低嵌入块中的标题级别
|
|
||||||
// Improve export of heading levels in embedded blocks https://github.com/siyuan-note/siyuan/issues/12233 https://github.com/siyuan-note/siyuan/issues/12741
|
|
||||||
embedTopLevel := 0
|
|
||||||
ast.Walk(subTree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
|
||||||
if !entering || ast.NodeHeading != n.Type {
|
|
||||||
return ast.WalkContinue
|
|
||||||
}
|
|
||||||
|
|
||||||
embedTopLevel = n.HeadingLevel
|
|
||||||
if parentHeadingLevel >= embedTopLevel {
|
|
||||||
n.HeadingLevel += parentHeadingLevel - embedTopLevel + 1
|
|
||||||
if 6 < n.HeadingLevel {
|
|
||||||
n.HeadingLevel = 6
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ast.WalkContinue
|
return ast.WalkContinue
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
md, _ = lute.FormatNodeSync(subTree.Root, luteEngine.ParseOptions, luteEngine.RenderOptions)
|
|
||||||
} else if "h" == sqlBlock.Type {
|
|
||||||
h := treenode.GetNodeInTree(subTree, sqlBlock.ID)
|
|
||||||
var hChildren []*ast.Node
|
|
||||||
|
|
||||||
// 从嵌入块的 IAL 属性中解析 custom-heading-mode,使用全局配置作为默认值
|
|
||||||
blockHeadingMode := Conf.Editor.HeadingEmbedMode
|
|
||||||
if customHeadingMode := n.IALAttr("custom-heading-mode"); "" != customHeadingMode {
|
|
||||||
if mode, err := strconv.Atoi(customHeadingMode); nil == err && (mode == 0 || mode == 1 || mode == 2) {
|
|
||||||
blockHeadingMode = mode
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 根据 blockHeadingMode 处理标题块的显示
|
embedTopLevel = n.HeadingLevel
|
||||||
// blockHeadingMode: 0=显示标题与下方的块,1=仅显示标题,2=仅显示标题下方的块
|
if parentHeadingLevel >= embedTopLevel {
|
||||||
if 1 == blockHeadingMode {
|
n.HeadingLevel += parentHeadingLevel - embedTopLevel + 1
|
||||||
// 仅显示标题
|
if 6 < n.HeadingLevel {
|
||||||
hChildren = append(hChildren, h)
|
n.HeadingLevel = 6
|
||||||
} else if 2 == blockHeadingMode {
|
|
||||||
// 仅显示标题下方的块(默认行为)
|
|
||||||
if "1" != h.IALAttr("fold") {
|
|
||||||
children := treenode.HeadingChildren(h)
|
|
||||||
for _, c := range children {
|
|
||||||
if "1" == c.IALAttr("heading-fold") {
|
|
||||||
// 嵌入块包含折叠标题时不应该显示其下方块 https://github.com/siyuan-note/siyuan/issues/4765
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
hChildren = append(hChildren, c)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
return ast.WalkContinue
|
||||||
// 0: 显示标题与下方的块
|
})
|
||||||
hChildren = append(hChildren, h)
|
}
|
||||||
hChildren = append(hChildren, treenode.HeadingChildren(h)...)
|
|
||||||
|
md, _ = lute.FormatNodeSync(subTree.Root, luteEngine.ParseOptions, luteEngine.RenderOptions)
|
||||||
|
} else if "h" == sqlBlock.Type {
|
||||||
|
h := treenode.GetNodeInTree(subTree, sqlBlock.ID)
|
||||||
|
var hChildren []*ast.Node
|
||||||
|
|
||||||
|
// 从嵌入块的 IAL 属性中解析 custom-heading-mode,使用全局配置作为默认值
|
||||||
|
blockHeadingMode := Conf.Editor.HeadingEmbedMode
|
||||||
|
if customHeadingMode := n.IALAttr("custom-heading-mode"); "" != customHeadingMode {
|
||||||
|
if mode, err := strconv.Atoi(customHeadingMode); nil == err && (mode == 0 || mode == 1 || mode == 2) {
|
||||||
|
blockHeadingMode = mode
|
||||||
}
|
}
|
||||||
if 0 == blockEmbedMode {
|
}
|
||||||
embedTopLevel := 0
|
|
||||||
|
// 根据 blockHeadingMode 处理标题块的显示
|
||||||
|
// blockHeadingMode: 0=显示标题与下方的块,1=仅显示标题,2=仅显示标题下方的块
|
||||||
|
if 1 == blockHeadingMode {
|
||||||
|
// 仅显示标题
|
||||||
|
hChildren = append(hChildren, h)
|
||||||
|
} else if 2 == blockHeadingMode {
|
||||||
|
// 仅显示标题下方的块(默认行为)
|
||||||
|
if "1" != h.IALAttr("fold") {
|
||||||
|
children := treenode.HeadingChildren(h)
|
||||||
|
for _, c := range children {
|
||||||
|
if "1" == c.IALAttr("heading-fold") {
|
||||||
|
// 嵌入块包含折叠标题时不应该显示其下方块 https://github.com/siyuan-note/siyuan/issues/4765
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
hChildren = append(hChildren, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 0: 显示标题与下方的块
|
||||||
|
hChildren = append(hChildren, h)
|
||||||
|
hChildren = append(hChildren, treenode.HeadingChildren(h)...)
|
||||||
|
}
|
||||||
|
if 0 == blockEmbedMode {
|
||||||
|
embedTopLevel := 0
|
||||||
|
for _, hChild := range hChildren {
|
||||||
|
if ast.NodeHeading == hChild.Type {
|
||||||
|
embedTopLevel = hChild.HeadingLevel
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if parentHeadingLevel >= embedTopLevel {
|
||||||
for _, hChild := range hChildren {
|
for _, hChild := range hChildren {
|
||||||
if ast.NodeHeading == hChild.Type {
|
if ast.NodeHeading == hChild.Type {
|
||||||
embedTopLevel = hChild.HeadingLevel
|
hChild.HeadingLevel += parentHeadingLevel - embedTopLevel + 1
|
||||||
break
|
if 6 < hChild.HeadingLevel {
|
||||||
}
|
hChild.HeadingLevel = 6
|
||||||
}
|
|
||||||
if parentHeadingLevel >= embedTopLevel {
|
|
||||||
for _, hChild := range hChildren {
|
|
||||||
if ast.NodeHeading == hChild.Type {
|
|
||||||
hChild.HeadingLevel += parentHeadingLevel - embedTopLevel + 1
|
|
||||||
if 6 < hChild.HeadingLevel {
|
|
||||||
hChild.HeadingLevel = 6
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mdBuf := &bytes.Buffer{}
|
|
||||||
for _, hChild := range hChildren {
|
|
||||||
md, _ = lute.FormatNodeSync(hChild, luteEngine.ParseOptions, luteEngine.RenderOptions)
|
|
||||||
mdBuf.WriteString(md)
|
|
||||||
mdBuf.WriteString("\n\n")
|
|
||||||
}
|
|
||||||
md = mdBuf.String()
|
|
||||||
} else {
|
|
||||||
node := treenode.GetNodeInTree(subTree, sqlBlock.ID)
|
|
||||||
md, _ = lute.FormatNodeSync(node, luteEngine.ParseOptions, luteEngine.RenderOptions)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := &bytes.Buffer{}
|
mdBuf := &bytes.Buffer{}
|
||||||
lines := strings.Split(md, "\n")
|
for _, hChild := range hChildren {
|
||||||
for i, line := range lines {
|
md, _ = lute.FormatNodeSync(hChild, luteEngine.ParseOptions, luteEngine.RenderOptions)
|
||||||
if 0 == blockEmbedMode { // 使用原始文本
|
mdBuf.WriteString(md)
|
||||||
buf.WriteString(line)
|
mdBuf.WriteString("\n\n")
|
||||||
} else { // 使用引述块
|
|
||||||
buf.WriteString("> " + line)
|
|
||||||
}
|
|
||||||
if i < len(lines)-1 {
|
|
||||||
buf.WriteString("\n")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
buf.WriteString("\n\n")
|
md = mdBuf.String()
|
||||||
|
} else {
|
||||||
|
node := treenode.GetNodeInTree(subTree, sqlBlock.ID)
|
||||||
|
md, _ = lute.FormatNodeSync(node, luteEngine.ParseOptions, luteEngine.RenderOptions)
|
||||||
|
}
|
||||||
|
|
||||||
subTree = parse.Parse("", buf.Bytes(), luteEngine.ParseOptions)
|
buf := &bytes.Buffer{}
|
||||||
var inserts []*ast.Node
|
lines := strings.Split(md, "\n")
|
||||||
for subNode := subTree.Root.FirstChild; nil != subNode; subNode = subNode.Next {
|
for i, line := range lines {
|
||||||
if ast.NodeKramdownBlockIAL != subNode.Type {
|
if 0 == blockEmbedMode { // 使用原始文本
|
||||||
inserts = append(inserts, subNode)
|
buf.WriteString(line)
|
||||||
}
|
} else { // 使用引述块
|
||||||
|
buf.WriteString("> " + line)
|
||||||
}
|
}
|
||||||
if 2 < len(n.KramdownIAL) && 0 < len(inserts) {
|
if i < len(lines)-1 {
|
||||||
if bookmark := n.IALAttr("bookmark"); "" != bookmark {
|
buf.WriteString("\n")
|
||||||
inserts[0].SetIALAttr("bookmark", bookmark)
|
|
||||||
}
|
|
||||||
if name := n.IALAttr("name"); "" != name {
|
|
||||||
inserts[0].SetIALAttr("name", name)
|
|
||||||
}
|
|
||||||
if alias := n.IALAttr("alias"); "" != alias {
|
|
||||||
inserts[0].SetIALAttr("alias", alias)
|
|
||||||
}
|
|
||||||
if memo := n.IALAttr("memo"); "" != memo {
|
|
||||||
inserts[0].SetIALAttr("memo", memo)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, insert := range inserts {
|
|
||||||
n.InsertBefore(insert)
|
|
||||||
|
|
||||||
if gulu.Str.Contains(sqlBlock.ID, *resolved) {
|
|
||||||
return ast.WalkContinue
|
|
||||||
}
|
|
||||||
|
|
||||||
resolveEmbedR(insert, blockEmbedMode, luteEngine, resolved, depth)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unlinks = append(unlinks, n)
|
buf.WriteString("\n\n")
|
||||||
return ast.WalkSkipChildren
|
|
||||||
|
subTree = parse.Parse("", buf.Bytes(), luteEngine.ParseOptions)
|
||||||
|
var inserts []*ast.Node
|
||||||
|
for subNode := subTree.Root.FirstChild; nil != subNode; subNode = subNode.Next {
|
||||||
|
if ast.NodeKramdownBlockIAL != subNode.Type {
|
||||||
|
inserts = append(inserts, subNode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if 2 < len(n.KramdownIAL) && 0 < len(inserts) {
|
||||||
|
if bookmark := n.IALAttr("bookmark"); "" != bookmark {
|
||||||
|
inserts[0].SetIALAttr("bookmark", bookmark)
|
||||||
|
}
|
||||||
|
if name := n.IALAttr("name"); "" != name {
|
||||||
|
inserts[0].SetIALAttr("name", name)
|
||||||
|
}
|
||||||
|
if alias := n.IALAttr("alias"); "" != alias {
|
||||||
|
inserts[0].SetIALAttr("alias", alias)
|
||||||
|
}
|
||||||
|
if memo := n.IALAttr("memo"); "" != memo {
|
||||||
|
inserts[0].SetIALAttr("memo", memo)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, insert := range inserts {
|
||||||
|
n.InsertBefore(insert)
|
||||||
|
|
||||||
|
if gulu.Str.Contains(sqlBlock.ID, *resolved) {
|
||||||
|
return ast.WalkContinue
|
||||||
|
}
|
||||||
|
|
||||||
|
resolveEmbedR(insert, blockEmbedMode, luteEngine, resolved, depth)
|
||||||
|
*depth--
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return ast.WalkContinue
|
unlinks = append(unlinks, n)
|
||||||
|
return ast.WalkSkipChildren
|
||||||
})
|
})
|
||||||
|
|
||||||
for _, unlink := range unlinks {
|
for _, unlink := range unlinks {
|
||||||
unlink.Unlink()
|
unlink.Unlink()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue