mirror of
https://github.com/siyuan-note/siyuan.git
synced 2026-02-16 12:08:07 +01:00
🎨 Add "Remove ID from asset name" switch to export settings https://github.com/siyuan-note/siyuan/issues/16065
Signed-off-by: Daniel <845765@qq.com>
This commit is contained in:
parent
11115da3d0
commit
066a9b5192
21 changed files with 128 additions and 11 deletions
|
|
@ -1353,6 +1353,23 @@ func getAssetsLinkDests(node *ast.Node, includeServePath bool) (ret []string) {
|
|||
return
|
||||
}
|
||||
|
||||
func getAssetsLinkDestsInTree(tree *parse.Tree, includeServePath bool) (nodes []*ast.Node) {
|
||||
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
||||
if !entering {
|
||||
return ast.WalkContinue
|
||||
}
|
||||
|
||||
dests := getAssetsLinkDests(n, includeServePath)
|
||||
if 1 > len(dests) {
|
||||
return ast.WalkContinue
|
||||
}
|
||||
|
||||
nodes = append(nodes, n)
|
||||
return ast.WalkContinue
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func setAssetsLinkDest(node *ast.Node, oldDest, dest string) {
|
||||
if ast.NodeLinkDest == node.Type {
|
||||
if bytes.HasPrefix(node.Tokens, []byte("//")) {
|
||||
|
|
|
|||
|
|
@ -1449,7 +1449,9 @@ func processPDFLinkEmbedAssets(pdfCtx *model.Context, assetDests []string, remov
|
|||
now := types.StringLiteral(types.DateString(time.Now()))
|
||||
for _, link := range assetLinks {
|
||||
link.URI = strings.ReplaceAll(link.URI, "http://"+util.LocalHost+":"+util.ServerPort+"/export/temp/", "")
|
||||
link.URI = strings.ReplaceAll(link.URI, "http://"+util.LocalHost+":6806/export/temp/", "")
|
||||
link.URI = strings.ReplaceAll(link.URI, "http://"+util.LocalHost+":"+util.ServerPort+"/", "") // Exporting PDF embedded asset files as attachments fails https://github.com/siyuan-note/siyuan/issues/7414#issuecomment-1704573557
|
||||
link.URI = strings.ReplaceAll(link.URI, "http://"+util.LocalHost+":6806/", "")
|
||||
link.URI, _ = url.PathUnescape(link.URI)
|
||||
if idx := strings.Index(link.URI, "?"); 0 < idx {
|
||||
link.URI = link.URI[:idx]
|
||||
|
|
@ -3324,6 +3326,7 @@ func exportPandocConvertZip(baseFolderName string, docPaths, defBlockIDs []strin
|
|||
return
|
||||
}
|
||||
|
||||
assetsOldNew, assetsNewOld := map[string]string{}, map[string]string{}
|
||||
luteEngine := util.NewLute()
|
||||
for i, p := range docPaths {
|
||||
id := util.GetTreeID(p)
|
||||
|
|
@ -3361,31 +3364,42 @@ func exportPandocConvertZip(baseFolderName string, docPaths, defBlockIDs []strin
|
|||
|
||||
// 解析导出后的标准 Markdown,汇总 assets
|
||||
tree = parse.Parse("", gulu.Str.ToBytes(md), luteEngine.ParseOptions)
|
||||
var assets []string
|
||||
assets = append(assets, getAssetsLinkDests(tree.Root, false)...)
|
||||
for _, asset := range assets {
|
||||
asset = string(html.DecodeDestination([]byte(asset)))
|
||||
if strings.Contains(asset, "?") {
|
||||
asset = asset[:strings.LastIndex(asset, "?")]
|
||||
removeAssetsID(tree, assetsOldNew, assetsNewOld)
|
||||
|
||||
newAssets := getAssetsLinkDests(tree.Root, false)
|
||||
for _, newAsset := range newAssets {
|
||||
newAsset = string(html.DecodeDestination([]byte(newAsset)))
|
||||
if strings.Contains(newAsset, "?") {
|
||||
newAsset = newAsset[:strings.LastIndex(newAsset, "?")]
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(asset, "assets/") {
|
||||
if !strings.HasPrefix(newAsset, "assets/") {
|
||||
continue
|
||||
}
|
||||
|
||||
srcPath := assetsPathMap[asset]
|
||||
oldAsset := assetsNewOld[newAsset]
|
||||
if "" == oldAsset {
|
||||
logging.LogWarnf("get asset old path for new asset [%s] failed", newAsset)
|
||||
continue
|
||||
}
|
||||
|
||||
srcPath := assetsPathMap[oldAsset]
|
||||
if "" == srcPath {
|
||||
logging.LogWarnf("get asset [%s] abs path failed", asset)
|
||||
logging.LogWarnf("get asset [%s] abs path failed", oldAsset)
|
||||
continue
|
||||
}
|
||||
|
||||
destPath := filepath.Join(writeFolder, asset)
|
||||
destPath := filepath.Join(writeFolder, newAsset)
|
||||
if copyErr := filelock.Copy(srcPath, destPath); copyErr != nil {
|
||||
logging.LogErrorf("copy asset from [%s] to [%s] failed: %s", srcPath, destPath, err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
for assetsOld, assetsNew := range assetsOldNew {
|
||||
md = strings.ReplaceAll(md, assetsOld, assetsNew)
|
||||
}
|
||||
|
||||
// 调用 Pandoc 进行格式转换
|
||||
pandocErr := util.Pandoc(pandocFrom, pandocTo, writePath, md)
|
||||
if pandocErr != nil {
|
||||
|
|
@ -3437,6 +3451,47 @@ func exportPandocConvertZip(baseFolderName string, docPaths, defBlockIDs []strin
|
|||
return
|
||||
}
|
||||
|
||||
func removeAssetsID(tree *parse.Tree, assetsOldNew, assetsNewOld map[string]string) {
|
||||
assetNodes := getAssetsLinkDestsInTree(tree, false)
|
||||
for _, node := range assetNodes {
|
||||
dests := getAssetsLinkDests(node, false)
|
||||
if 1 > len(dests) {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, dest := range dests {
|
||||
if !Conf.Export.RemoveAssetsID {
|
||||
assetsOldNew[dest] = dest
|
||||
assetsNewOld[dest] = dest
|
||||
continue
|
||||
}
|
||||
|
||||
if newDest := assetsOldNew[dest]; "" != newDest {
|
||||
setAssetsLinkDest(node, dest, newDest)
|
||||
continue
|
||||
}
|
||||
|
||||
name := path.Base(dest)
|
||||
name = util.RemoveID(name)
|
||||
newDest := "assets/" + name
|
||||
if existOld := assetsNewOld[newDest]; "" != existOld {
|
||||
if existOld == dest { // 已存在相同资源路径
|
||||
setAssetsLinkDest(node, dest, newDest)
|
||||
} else {
|
||||
// 存在同名但内容不同的资源文件,保留 ID
|
||||
assetsNewOld[dest] = dest
|
||||
assetsOldNew[dest] = dest
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
setAssetsLinkDest(node, dest, newDest)
|
||||
assetsOldNew[dest] = newDest
|
||||
assetsNewOld[newDest] = dest
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getExportBlockRefLinkText(blockRef *ast.Node, blockRefTextLeft, blockRefTextRight string) (defID, linkText string) {
|
||||
defID, linkText, _ = treenode.GetBlockRef(blockRef)
|
||||
if "" == linkText {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue