Skip to content

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

:bug: Validate Markdown import targets and preserve parent hpaths https://github.com/siyuan-note/siyuan/issues/19279

Daniel99f9a4e59d9d
diff --git a/kernel/cli/cmd/import.go b/kernel/cli/cmd/import.go
index bd5116b573..a58773302d 100644
--- a/kernel/cli/cmd/import.go
+++ b/kernel/cli/cmd/import.go
@@ -55,6 +55,9 @@ var importMdCmd = &cobra.Command{
}
if dryRun {
+ if err := model.ValidateImportFromLocalPath(notebook, absPath, targetPath); err != nil {
+ return formatNotebookWriteError(notebook, err)
+ }
fmt.Printf("[dry-run] Would import Markdown from \"%s\" to notebook %s\n", filePath, notebook)
return nil
}
@@ -136,7 +139,7 @@ var importDataCmd = &cobra.Command{
func init() {
importMdCmd.Flags().String("file", "", "file or directory path")
importMdCmd.Flags().StringP("notebook", "n", "", "notebook ID")
- importMdCmd.Flags().String("path", "", "target internal path (default /)")
+ importMdCmd.Flags().String("path", "", "target parent document internal path, with or without .sy suffix (default /)")
importMdCmd.Flags().String("hpath", "", "target human-readable path")
importSYCmd.Flags().String("file", "", ".sy.zip file path")
diff --git a/kernel/model/import.go b/kernel/model/import.go
index 13626ea7dc..965dea1f7f 100644
--- a/kernel/model/import.go
+++ b/kernel/model/import.go
@@ -1369,12 +1369,39 @@ func ImportFromLocalPathSkipRoot(boxID, localPath string, toPath string) (err er
return importFromLocalPath(boxID, localPath, toPath, true)
}
+// ValidateImportFromLocalPath 校验 Markdown 导入来源和目标,不写入导入数据。
+func ValidateImportFromLocalPath(boxID, localPath, toPath string) error {
+ if _, err := getOpenedBox(boxID); err != nil {
+ return err
+ }
+ if _, err := os.Stat(localPath); err != nil {
+ return err
+ }
+ _, _, err := resolveMarkdownImportTarget(boxID, toPath)
+ return err
+}
+
+func resolveMarkdownImportTarget(boxID, toPath string) (baseHPath, baseTargetPath string, err error) {
+ toPath = path.Clean("/" + strings.TrimPrefix(toPath, "/"))
+ if toPath != "/" && !strings.HasSuffix(toPath, ".sy") {
+ toPath += ".sy"
+ }
+ toPath = normalizeBoxDocTarget(boxID, toPath)
+ if toPath == "/" {
+ return "/", "/", nil
+ }
+ block := treenode.GetBlockTreeRootByPath(boxID, toPath)
+ if block == nil || block.ID == "" {
+ return "", "", fmt.Errorf("target document not found in notebook %s: %s", boxID, toPath)
+ }
+ return block.HPath, strings.TrimSuffix(block.Path, ".sy"), nil
+}
+
func importFromLocalPath(boxID, localPath string, toPath string, skipRoot bool) (err error) {
box, err := getOpenedBox(boxID)
if nil != err {
return err
}
- toPath = normalizeBoxDocTarget(boxID, toPath)
util.PushEndlessProgress(Conf.Language(73))
defer func() {
util.PushClearProgress()
@@ -1392,18 +1419,9 @@ func importFromLocalPath(boxID, localPath string, toPath string, skipRoot bool)
FlushTxQueue()
- var baseHPath, baseTargetPath string
- if "/" == toPath {
- baseHPath = "/"
- baseTargetPath = "/"
- } else {
- block := treenode.GetBlockTreeRootByPath(boxID, toPath)
- if nil == block {
- logging.LogErrorf("not found block by path [%s]", toPath)
- return nil
- }
- baseHPath = block.HPath
- baseTargetPath = strings.TrimSuffix(block.Path, ".sy")
+ baseHPath, baseTargetPath, err := resolveMarkdownImportTarget(boxID, toPath)
+ if err != nil {
+ return err
}
targetDocDirLocalPath := filepath.Join(util.DataDir, boxID,
filepath.FromSlash(strings.TrimPrefix(baseTargetPath, "/")))
@@ -1480,7 +1498,7 @@ func importFromLocalPath(boxID, localPath string, toPath string, skipRoot bool)
hPath = strings.TrimSuffix(hPath, ext)
if "" == curRelPath {
curRelPath = "/"
- hPath = "/" + title
+ hPath = path.Join(baseHPath, title)
} else {
dirPath := targetPaths[path.Dir(curRelPath)]
targetPath = path.Join(dirPath, id)
diff --git a/kernel/model/import_markdown_fts5_test.go b/kernel/model/import_markdown_fts5_test.go
new file mode 100644
index 0000000000..d1d5cf82d6
--- /dev/null
+++ b/kernel/model/import_markdown_fts5_test.go
@@ -0,0 +1,105 @@
+//go:build fts5
+
+package model
+
+import (
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/siyuan-note/siyuan/kernel/conf"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+func TestMarkdownImportTargetAndHPath(t *testing.T) {
+ const boxID = "20260910000000-box0001"
+ const parentID = "20260910000001-doc0001"
+ setupExportRelatedTest(t, boxID)
+ Conf.Editor = conf.NewEditor()
+ setupNotebookDocumentImportDatabase(t)
+ parentPath := "/" + parentID + ".sy"
+ writeExportRelatedTestTree(t, treenode.NewTree(boxID, parentPath, "/Parent", "Parent"))
+ source := filepath.Join(t.TempDir(), "Imported")
+ if err := os.MkdirAll(source, 0755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(source, "test.md"), []byte("# test"), 0644); err != nil {
+ t.Fatal(err)
+ }
+ for _, target := range []string{"", "/", "/" + boxID, "/" + boxID + ".sy", parentPath, strings.TrimSuffix(parentPath, ".sy"), "/" + boxID + parentPath} {
+ if err := ValidateImportFromLocalPath(boxID, source, target); err != nil {
+ t.Fatalf("valid target %q: %v", target, err)
+ }
+ }
+ for _, target := range []string{"missing", "/missing.sy"} {
+ if err := ValidateImportFromLocalPath(boxID, source, target); err == nil {
+ t.Fatalf("validation accepted %q", target)
+ }
+ if err := ImportFromLocalPath(boxID, source, target); err == nil {
+ t.Fatalf("import accepted %q", target)
+ }
+ }
+ if err := ValidateImportFromLocalPath(boxID, source+"-missing", parentPath); err == nil {
+ t.Fatal("validation accepted missing source")
+ }
+ if err := ValidateImportFromLocalPath("missing", source, "/"); err == nil {
+ t.Fatal("validation accepted missing notebook")
+ }
+ entries, err := os.ReadDir(filepath.Join(util.DataDir, boxID))
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, entry := range entries {
+ if entry.IsDir() && entry.Name() != ".siyuan" {
+ t.Fatalf("validation or rejected import created %s", entry.Name())
+ }
+ }
+ for _, target := range []string{parentPath, strings.TrimSuffix(parentPath, ".sy"), "/"} {
+ if err := ImportFromLocalPath(boxID, source, target); err != nil {
+ t.Fatal(err)
+ }
+ prefix := "/Parent/Imported"
+ if target == "/" {
+ prefix = "/Imported"
+ }
+ for _, hpath := range []string{prefix, prefix + "/test"} {
+ if block := treenode.GetBlockTreeRootByHPath(boxID, hpath); block == nil || block.ID == "" {
+ t.Fatalf("missing imported hpath %q", hpath)
+ }
+ }
+ }
+ root := treenode.GetBlockTreeRootByHPath(boxID, "/Parent/Imported")
+ if err := ImportFromLocalPath(boxID, source, root.Path); err != nil {
+ t.Fatal(err)
+ }
+ if block := treenode.GetBlockTreeRootByHPath(boxID, "/Parent/Imported/Imported/test"); block == nil || block.ID == "" {
+ t.Fatal("nested import lost parent hpath")
+ }
+ if err := ImportFromLocalPath(boxID, filepath.Join(source, "test.md"), parentPath); err != nil {
+ t.Fatal(err)
+ }
+ if block := treenode.GetBlockTreeRootByHPath(boxID, "/Parent/test"); block == nil || block.ID == "" {
+ t.Fatal("single file import lost parent hpath")
+ }
+ if err := ImportFromLocalPathSkipRoot(boxID, source, root.Path); err != nil {
+ t.Fatal(err)
+ }
+ children, err := os.ReadDir(filepath.Join(util.DataDir, boxID, strings.TrimSuffix(strings.TrimPrefix(root.Path, "/"), ".sy")))
+ if err != nil {
+ t.Fatal(err)
+ }
+ count := 0
+ for _, child := range children {
+ if strings.HasSuffix(child.Name(), ".sy") {
+ block := treenode.GetBlockTreeRootByPath(boxID, strings.TrimSuffix(root.Path, ".sy")+"/"+child.Name())
+ if block != nil && block.HPath == "/Parent/Imported/test" {
+ count++
+ }
+ }
+ }
+ if count != 2 {
+ t.Fatalf("skip-root import: got %d direct test documents, want 2", count)
+ }
+}