Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Avoid an extra root document when importing Markdown into new notebooks https://github.com/siyuan-note/siyuan/issues/18591

Daniel47183bd5febb
diff --git a/app/src/util/mount.ts b/app/src/util/mount.ts
index f44e055a44..146eecf7b7 100644
--- a/app/src/util/mount.ts
+++ b/app/src/util/mount.ts
@@ -211,6 +211,7 @@ export const newNotebook = () => {
formData.append("file", file);
formData.append("notebook", notebookID);
formData.append("toPath", "/");
+ formData.append("skipRoot", "true");
fetchPost("/api/import/importZipMd", formData);
});
});
@@ -232,6 +233,7 @@ export const newNotebook = () => {
notebook: notebookID,
localPath: sourcePath,
toPath: "/",
+ skipRoot: !isFile,
});
});
};
diff --git a/kernel/api/import.go b/kernel/api/import.go
index 6eeed57e18..91de18ca07 100644
--- a/kernel/api/import.go
+++ b/kernel/api/import.go
@@ -440,6 +440,7 @@ func importStdMd(c *gin.Context) {
notebook := arg["notebook"].(string)
localPath := arg["localPath"].(string)
toPath := arg["toPath"].(string)
+ skipRoot, _ := arg["skipRoot"].(bool)
if gulu.File.IsSubPath(util.WorkingDir, localPath) {
msg := fmt.Sprintf("import from local path [%s] failed: local path is sub path of working dir", localPath)
@@ -457,7 +458,12 @@ func importStdMd(c *gin.Context) {
return
}
- err := model.ImportFromLocalPath(notebook, localPath, toPath)
+ var err error
+ if skipRoot {
+ err = model.ImportFromLocalPathSkipRoot(notebook, localPath, toPath)
+ } else {
+ err = model.ImportFromLocalPath(notebook, localPath, toPath)
+ }
if err != nil {
ret.Code = -1
ret.Msg = err.Error()
@@ -555,6 +561,7 @@ func importZipMd(c *gin.Context) {
notebook := form.Value["notebook"][0]
toPath := form.Value["toPath"][0]
+ skipRoot := len(form.Value["skipRoot"]) > 0 && form.Value["skipRoot"][0] == "true"
// 准备解压路径
filenameMain := strings.TrimSuffix(file.Filename, filepath.Ext(file.Filename))
@@ -572,7 +579,11 @@ func importZipMd(c *gin.Context) {
}
// 调用本地导入逻辑
- err = model.ImportFromLocalPath(notebook, unzipPath, toPath)
+ if skipRoot {
+ err = model.ImportFromLocalPathSkipRoot(notebook, unzipPath, toPath)
+ } else {
+ err = model.ImportFromLocalPath(notebook, unzipPath, toPath)
+ }
if err != nil {
ret.Code = -1
diff --git a/kernel/model/import.go b/kernel/model/import.go
index 8fc939e96a..9909b823c3 100644
--- a/kernel/model/import.go
+++ b/kernel/model/import.go
@@ -1171,6 +1171,15 @@ func ImportData(zipPath string) (err error) {
}
func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
+ return importFromLocalPath(boxID, localPath, toPath, false)
+}
+
+// ImportFromLocalPathSkipRoot 导入本地路径,但不将来源根目录转换为文档。
+func ImportFromLocalPathSkipRoot(boxID, localPath string, toPath string) (err error) {
+ return importFromLocalPath(boxID, localPath, toPath, true)
+}
+
+func importFromLocalPath(boxID, localPath string, toPath string, skipRoot bool) (err error) {
box, err := getOpenedBox(boxID)
if nil != err {
return err
@@ -1268,8 +1277,16 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
id := ast.NewNodeID()
curRelPath := filepath.ToSlash(strings.TrimPrefix(currentPath, localPath))
+ if skipRoot && "" == curRelPath && d.IsDir() {
+ targetPaths["/"] = baseTargetPath
+ return nil
+ }
targetPath := path.Join(baseTargetPath, id)
- hPath := path.Join(baseHPath, filepath.Base(localPath), filepath.ToSlash(strings.TrimPrefix(currentPath, localPath)))
+ hPathRoot := filepath.Base(localPath)
+ if skipRoot {
+ hPathRoot = ""
+ }
+ hPath := path.Join(baseHPath, hPathRoot, filepath.ToSlash(strings.TrimPrefix(currentPath, localPath)))
hPath = strings.TrimSuffix(hPath, ext)
if "" == curRelPath {
curRelPath = "/"