mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-01-10 18:58:50 +01:00
🎨 Supports exporting a code block as a file https://github.com/siyuan-note/siyuan/pull/16774
Signed-off-by: Daniel <845765@qq.com>
This commit is contained in:
parent
7221367334
commit
58bb751cd4
3 changed files with 68 additions and 0 deletions
|
|
@ -61,6 +61,50 @@ import (
|
|||
"github.com/siyuan-note/siyuan/kernel/util"
|
||||
)
|
||||
|
||||
func ExportCodeBlock(blockID string) (filePath string, err error) {
|
||||
// Supports exporting a code block as a file https://github.com/siyuan-note/siyuan/pull/16774
|
||||
|
||||
tree, _ := LoadTreeByBlockID(blockID)
|
||||
if nil == tree {
|
||||
err = ErrBlockNotFound
|
||||
return
|
||||
}
|
||||
|
||||
node := treenode.GetNodeInTree(tree, blockID)
|
||||
if nil == node {
|
||||
err = ErrBlockNotFound
|
||||
return
|
||||
}
|
||||
|
||||
if ast.NodeCodeBlock != node.Type {
|
||||
err = errors.New("not a code block")
|
||||
return
|
||||
}
|
||||
|
||||
code := node.ChildByType(ast.NodeCodeBlockCode)
|
||||
if nil == code {
|
||||
err = errors.New("code block has no code node")
|
||||
return
|
||||
}
|
||||
|
||||
name := tree.Root.IALAttr("title") + "-" + util.CurrentTimeSecondsStr() + ".txt"
|
||||
name = util.FilterFileName(name)
|
||||
exportFolder := filepath.Join(util.TempDir, "export", "code")
|
||||
if err = os.MkdirAll(exportFolder, 0755); err != nil {
|
||||
logging.LogErrorf("create export temp folder failed: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
writePath := filepath.Join(exportFolder, name)
|
||||
err = filelock.WriteFile(writePath, code.Tokens)
|
||||
if nil != err {
|
||||
return
|
||||
}
|
||||
|
||||
filePath = "/export/code/" + url.PathEscape(name)
|
||||
return
|
||||
}
|
||||
|
||||
func ExportAv2CSV(avID, blockID string) (zipPath string, err error) {
|
||||
// Database block supports export as CSV https://github.com/siyuan-note/siyuan/issues/10072
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue