mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-05-03 07:01:12 +08:00
🎨 Improve asset src decoding when importing markdown https://github.com/siyuan-note/siyuan/issues/14117
This commit is contained in:
parent
2a4531194c
commit
4669752e14
@ -721,7 +721,7 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
|
||||
hPathsIDs := map[string]string{}
|
||||
idPaths := map[string]string{}
|
||||
moveIDs := map[string]string{}
|
||||
|
||||
assetsDone := map[string]string{}
|
||||
if gulu.File.IsDir(localPath) { // 导入文件夹
|
||||
// 收集所有资源文件
|
||||
assets := map[string]string{}
|
||||
@ -744,7 +744,6 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
|
||||
})
|
||||
|
||||
targetPaths := map[string]string{}
|
||||
assetsDone := map[string]string{}
|
||||
count := 0
|
||||
// md 转换 sy
|
||||
filelock.Walk(localPath, func(currentPath string, d fs.DirEntry, err error) error {
|
||||
@ -866,15 +865,11 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
|
||||
return ast.WalkContinue
|
||||
}
|
||||
|
||||
absolutePath := filepath.Join(currentDir, dest)
|
||||
newAbsolutePath := absolutePath
|
||||
|
||||
decodedDest := string(html.DecodeDestination([]byte(dest)))
|
||||
if decodedDest != dest {
|
||||
dest = decodedDest
|
||||
newAbsolutePath = filepath.Join(currentDir, dest)
|
||||
gulu.File.Copy(absolutePath, newAbsolutePath)
|
||||
}
|
||||
absolutePath := filepath.Join(currentDir, dest)
|
||||
|
||||
if ast.NodeLinkDest == n.Type {
|
||||
n.Tokens = []byte(dest)
|
||||
@ -889,28 +884,22 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
|
||||
return ast.WalkContinue
|
||||
}
|
||||
|
||||
if !gulu.File.IsExist(newAbsolutePath) {
|
||||
if !gulu.File.IsExist(absolutePath) {
|
||||
return ast.WalkContinue
|
||||
}
|
||||
|
||||
absDest := filepath.Join(currentDir, dest)
|
||||
fullPath, exist := assets[absDest]
|
||||
if !exist {
|
||||
absDest = filepath.Join(currentDir, string(html.DecodeDestination([]byte(dest))))
|
||||
}
|
||||
fullPath, exist = assets[absDest]
|
||||
if exist {
|
||||
existName := assetsDone[absDest]
|
||||
existName := assetsDone[absolutePath]
|
||||
var name string
|
||||
if "" == existName {
|
||||
name = filepath.Base(fullPath)
|
||||
name = filepath.Base(absolutePath)
|
||||
name = util.FilterUploadFileName(name)
|
||||
name = util.AssetName(name)
|
||||
assetTargetPath := filepath.Join(assetDirPath, name)
|
||||
if err = filelock.Copy(fullPath, assetTargetPath); err != nil {
|
||||
logging.LogErrorf("copy asset from [%s] to [%s] failed: %s", fullPath, assetTargetPath, err)
|
||||
if err = filelock.Copy(absolutePath, assetTargetPath); err != nil {
|
||||
logging.LogErrorf("copy asset from [%s] to [%s] failed: %s", absolutePath, assetTargetPath, err)
|
||||
return ast.WalkContinue
|
||||
}
|
||||
assetsDone[absDest] = name
|
||||
assetsDone[absolutePath] = name
|
||||
} else {
|
||||
name = existName
|
||||
}
|
||||
@ -919,7 +908,6 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
|
||||
} else {
|
||||
n.TextMarkAHref = "assets/" + name
|
||||
}
|
||||
}
|
||||
return ast.WalkContinue
|
||||
})
|
||||
|
||||
@ -1000,15 +988,11 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
|
||||
return ast.WalkContinue
|
||||
}
|
||||
|
||||
absolutePath := filepath.Join(filepath.Dir(localPath), dest)
|
||||
newAbsolutePath := absolutePath
|
||||
|
||||
decodedDest := string(html.DecodeDestination([]byte(dest)))
|
||||
if decodedDest != dest {
|
||||
dest = decodedDest
|
||||
newAbsolutePath = filepath.Join(filepath.Dir(localPath), dest)
|
||||
gulu.File.Copy(absolutePath, newAbsolutePath)
|
||||
}
|
||||
absolutePath := filepath.Join(filepath.Dir(localPath), dest)
|
||||
|
||||
if ast.NodeLinkDest == n.Type {
|
||||
n.Tokens = []byte(dest)
|
||||
@ -1023,17 +1007,25 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
|
||||
return ast.WalkContinue
|
||||
}
|
||||
|
||||
if !gulu.File.IsExist(newAbsolutePath) {
|
||||
if !gulu.File.IsExist(absolutePath) {
|
||||
return ast.WalkContinue
|
||||
}
|
||||
|
||||
name := filepath.Base(newAbsolutePath)
|
||||
existName := assetsDone[absolutePath]
|
||||
var name string
|
||||
if "" == existName {
|
||||
name = filepath.Base(absolutePath)
|
||||
name = util.FilterUploadFileName(name)
|
||||
name = util.AssetName(name)
|
||||
assetTargetPath := filepath.Join(assetDirPath, name)
|
||||
if err = filelock.Copy(newAbsolutePath, assetTargetPath); err != nil {
|
||||
logging.LogErrorf("copy asset from [%s] to [%s] failed: %s", newAbsolutePath, assetTargetPath, err)
|
||||
if err = filelock.Copy(absolutePath, assetTargetPath); err != nil {
|
||||
logging.LogErrorf("copy asset from [%s] to [%s] failed: %s", absolutePath, assetTargetPath, err)
|
||||
return ast.WalkContinue
|
||||
}
|
||||
assetsDone[absolutePath] = name
|
||||
} else {
|
||||
name = existName
|
||||
}
|
||||
if ast.NodeLinkDest == n.Type {
|
||||
n.Tokens = []byte("assets/" + name)
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user