From e2dec33669b19e75038118fef0ad8095c71a356f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=AE=E7=94=9F?= Date: Mon, 23 Aug 2021 17:41:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=B8=80=E6=AC=A1=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/struct/DirToStructRes.go | 43 ++++++----------------------------- src/struct/noteStore.go | 44 +++++++++++++++++++++++++++++++++++- src/util/file.go | 1 + src/util/notes_handle.go | 1 - 4 files changed, 51 insertions(+), 38 deletions(-) diff --git a/src/struct/DirToStructRes.go b/src/struct/DirToStructRes.go index af8da7e..5d54abf 100644 --- a/src/struct/DirToStructRes.go +++ b/src/struct/DirToStructRes.go @@ -7,7 +7,6 @@ import ( "github.com/88250/lute/ast" "github.com/88250/lute/parse" - "github.com/siyuan-note/oceanpress/src/conf" "github.com/siyuan-note/oceanpress/src/util" ) @@ -68,40 +67,11 @@ func (r *FileEntity) FileEntityRelativePath(target FileEntity, id string) string // VirtualPath 是最终要在浏览器中可以访问的路径 func (r *FileEntity) VirtualPath() (path string) { - - // 使用文档名作为路径名 - if conf.OutMode == "title" { - entries := strings.Split(r.RelativePath, "/") - var virtualPath = []string{} - for _, v := range entries { - id := v - if strings.HasSuffix(v, util.NotesSuffix) { - id = v[:len(v)-len(util.NotesSuffix)] - } - if util.IsID(id) { - FileEntity, _, err := NoteStore.FindFileEntityFromID(id) - if err == nil { - virtualPath = append(virtualPath, FileEntity.Name) - continue - } - } - virtualPath = append(virtualPath, id) - } - path = strings.Join(virtualPath, "/") - if util.IsNotes(r.RelativePath) { - path += ".html" - } - return path - } else { - // 直接使用 ID 作为路径名 - if conf.OutMode != "id" { - util.Warn("OutMode 参数的值在预设之外,默认采用 id 模式") - } - if util.IsNotes(r.RelativePath) { - return r.RelativePath[0:len(r.RelativePath)-len(util.NotesSuffix)] + ".html" - } - return r.RelativePath + path = PathResolve(r.RelativePath) + if util.IsNotes(path) { + return path[:len(path)-len(util.NotesSuffix)] + ".html" } + return path } // RootPath 获取当前对象相对于 root 目录的路径 @@ -115,7 +85,7 @@ func (r *FileEntity) RootPath() string { if Level > 0 { LevelRoot += strings.Repeat("../", Level) } - return LevelRoot + return PathResolve(LevelRoot) } // FindFileEntityFromID 通过 id 返回对应实体 @@ -161,8 +131,9 @@ type RssInfo struct { LastBuildDate string List []RssItem } + // RedirectInfo 重定向模板所需信息 type RedirectInfo struct { RedirectPath string - Title string + Title string } diff --git a/src/struct/noteStore.go b/src/struct/noteStore.go index 027024e..1c97542 100644 --- a/src/struct/noteStore.go +++ b/src/struct/noteStore.go @@ -1,6 +1,48 @@ package structAll +import ( + "strings" + + "github.com/siyuan-note/oceanpress/src/conf" + "github.com/siyuan-note/oceanpress/src/util" +) + // 来自 main.go 的生成 var NoteStore = DirToStructRes{} - +// PathResolve 处理不同模式下路径的变换 +func PathResolve(path string) string { + // 使用文档名作为路径名 + if conf.OutMode == "title" { + entries := strings.Split(path, "/") + var virtualPath = []string{} + for _, v := range entries { + if v == "" { + virtualPath = append(virtualPath, v) + continue + } + suffix := "" + fragment := strings.Split(v, ".") + id := fragment[0] + if len(fragment) > 1 { + suffix = "." + fragment[1] + } + if util.IsID(id) { + FileEntity, _, err := NoteStore.FindFileEntityFromID(id) + if err == nil { + virtualPath = append(virtualPath, FileEntity.Name+suffix) + continue + } + } + virtualPath = append(virtualPath, v) + } + path = strings.Join(virtualPath, "/") + return path + } else { + // 直接使用 ID 作为路径名 + if conf.OutMode != "id" { + util.Warn("OutMode 参数的值在预设之外,默认采用 id 模式") + } + return path + } +} diff --git a/src/util/file.go b/src/util/file.go index 90ed8d3..5fde66b 100644 --- a/src/util/file.go +++ b/src/util/file.go @@ -13,6 +13,7 @@ import ( // WriteFile 确保文件路径存在,且不超出 conf.outDir func WriteFile(targetPath string, data []byte, perm os.FileMode) error { + targetPath = filepath.ToSlash(targetPath) rel, err := filepath.Rel(conf.OutDir, targetPath) if err != nil { return err diff --git a/src/util/notes_handle.go b/src/util/notes_handle.go index 52291d7..366b1c5 100644 --- a/src/util/notes_handle.go +++ b/src/util/notes_handle.go @@ -36,7 +36,6 @@ func IsID(id string) bool { reg, _ := regexp.Compile(`^\d{14}-[a-z0-9]{7}`) return reg.MatchString(id) } - // TimeFromID 从 id 中提取创建时间 func TimeFromID(id string) string { reg, _ := regexp.Compile(`^\d+`)