🐛 Fix render template with database table view https://github.com/siyuan-note/insider/issues/1132

This commit is contained in:
Daniel 2024-01-08 12:04:03 +08:00
parent cfc3fd11e9
commit 9ede4c7361
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
2 changed files with 58 additions and 49 deletions

View file

@ -287,45 +287,48 @@ func renderTemplate(p, id string, preview bool) (string, error) {
logging.LogErrorf("parse attribute view [%s] failed: %s", n.AttributeViewID, parseErr)
} else {
cloned := attrView.ShallowClone()
if nil != cloned {
n.AttributeViewID = cloned.ID
if !preview {
// 非预览时持久化数据库
if saveErr := av.SaveAttributeView(cloned); nil != saveErr {
logging.LogErrorf("save attribute view [%s] failed: %s", cloned.ID, saveErr)
}
} else {
// 预览时使用简单表格渲染
view, getErr := attrView.GetCurrentView()
if nil != getErr {
logging.LogErrorf("get attribute view [%s] failed: %s", n.AttributeViewID, getErr)
return ast.WalkContinue
}
if nil == cloned {
logging.LogErrorf("clone attribute view [%s] failed", n.AttributeViewID)
return ast.WalkContinue
}
table, renderErr := renderAttributeViewTable(attrView, view)
if nil != renderErr {
logging.LogErrorf("render attribute view [%s] table failed: %s", n.AttributeViewID, renderErr)
return ast.WalkContinue
}
var aligns []int
for range table.Columns {
aligns = append(aligns, 0)
}
mdTable := &ast.Node{Type: ast.NodeTable, TableAligns: aligns}
mdTableHead := &ast.Node{Type: ast.NodeTableHead}
mdTable.AppendChild(mdTableHead)
mdTableHeadRow := &ast.Node{Type: ast.NodeTableRow, TableAligns: aligns}
mdTableHead.AppendChild(mdTableHeadRow)
for _, col := range table.Columns {
cell := &ast.Node{Type: ast.NodeTableCell}
cell.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(col.Name)})
mdTableHeadRow.AppendChild(cell)
}
n.InsertBefore(mdTable)
unlinks = append(unlinks, n)
n.AttributeViewID = cloned.ID
if !preview {
// 非预览时持久化数据库
if saveErr := av.SaveAttributeView(cloned); nil != saveErr {
logging.LogErrorf("save attribute view [%s] failed: %s", cloned.ID, saveErr)
}
} else {
// 预览时使用简单表格渲染
view, getErr := attrView.GetCurrentView()
if nil != getErr {
logging.LogErrorf("get attribute view [%s] failed: %s", n.AttributeViewID, getErr)
return ast.WalkContinue
}
table, renderErr := renderAttributeViewTable(attrView, view)
if nil != renderErr {
logging.LogErrorf("render attribute view [%s] table failed: %s", n.AttributeViewID, renderErr)
return ast.WalkContinue
}
var aligns []int
for range table.Columns {
aligns = append(aligns, 0)
}
mdTable := &ast.Node{Type: ast.NodeTable, TableAligns: aligns}
mdTableHead := &ast.Node{Type: ast.NodeTableHead}
mdTable.AppendChild(mdTableHead)
mdTableHeadRow := &ast.Node{Type: ast.NodeTableRow, TableAligns: aligns}
mdTableHead.AppendChild(mdTableHeadRow)
for _, col := range table.Columns {
cell := &ast.Node{Type: ast.NodeTableCell}
cell.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(col.Name)})
mdTableHeadRow.AppendChild(cell)
}
n.InsertBefore(mdTable)
unlinks = append(unlinks, n)
}
}
}