Skip to content
This repository was archived by the owner on Sep 17, 2023. It is now read-only.

Commit e2dec33

Browse files
committed
上一次提交的
1 parent ff078c3 commit e2dec33

File tree

4 files changed

+51
-38
lines changed

4 files changed

+51
-38
lines changed

src/struct/DirToStructRes.go

+7-36
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77

88
"github.com/88250/lute/ast"
99
"github.com/88250/lute/parse"
10-
"github.com/siyuan-note/oceanpress/src/conf"
1110
"github.com/siyuan-note/oceanpress/src/util"
1211
)
1312

@@ -68,40 +67,11 @@ func (r *FileEntity) FileEntityRelativePath(target FileEntity, id string) string
6867

6968
// VirtualPath 是最终要在浏览器中可以访问的路径
7069
func (r *FileEntity) VirtualPath() (path string) {
71-
72-
// 使用文档名作为路径名
73-
if conf.OutMode == "title" {
74-
entries := strings.Split(r.RelativePath, "/")
75-
var virtualPath = []string{}
76-
for _, v := range entries {
77-
id := v
78-
if strings.HasSuffix(v, util.NotesSuffix) {
79-
id = v[:len(v)-len(util.NotesSuffix)]
80-
}
81-
if util.IsID(id) {
82-
FileEntity, _, err := NoteStore.FindFileEntityFromID(id)
83-
if err == nil {
84-
virtualPath = append(virtualPath, FileEntity.Name)
85-
continue
86-
}
87-
}
88-
virtualPath = append(virtualPath, id)
89-
}
90-
path = strings.Join(virtualPath, "/")
91-
if util.IsNotes(r.RelativePath) {
92-
path += ".html"
93-
}
94-
return path
95-
} else {
96-
// 直接使用 ID 作为路径名
97-
if conf.OutMode != "id" {
98-
util.Warn("OutMode 参数的值在预设之外,默认采用 id 模式")
99-
}
100-
if util.IsNotes(r.RelativePath) {
101-
return r.RelativePath[0:len(r.RelativePath)-len(util.NotesSuffix)] + ".html"
102-
}
103-
return r.RelativePath
70+
path = PathResolve(r.RelativePath)
71+
if util.IsNotes(path) {
72+
return path[:len(path)-len(util.NotesSuffix)] + ".html"
10473
}
74+
return path
10575
}
10676

10777
// RootPath 获取当前对象相对于 root 目录的路径
@@ -115,7 +85,7 @@ func (r *FileEntity) RootPath() string {
11585
if Level > 0 {
11686
LevelRoot += strings.Repeat("../", Level)
11787
}
118-
return LevelRoot
88+
return PathResolve(LevelRoot)
11989
}
12090

12191
// FindFileEntityFromID 通过 id 返回对应实体
@@ -161,8 +131,9 @@ type RssInfo struct {
161131
LastBuildDate string
162132
List []RssItem
163133
}
134+
164135
// RedirectInfo 重定向模板所需信息
165136
type RedirectInfo struct {
166137
RedirectPath string
167-
Title string
138+
Title string
168139
}

src/struct/noteStore.go

+43-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,48 @@
11
package structAll
22

3+
import (
4+
"strings"
5+
6+
"github.com/siyuan-note/oceanpress/src/conf"
7+
"github.com/siyuan-note/oceanpress/src/util"
8+
)
9+
310
// 来自 main.go 的生成
411
var NoteStore = DirToStructRes{}
512

6-
13+
// PathResolve 处理不同模式下路径的变换
14+
func PathResolve(path string) string {
15+
// 使用文档名作为路径名
16+
if conf.OutMode == "title" {
17+
entries := strings.Split(path, "/")
18+
var virtualPath = []string{}
19+
for _, v := range entries {
20+
if v == "" {
21+
virtualPath = append(virtualPath, v)
22+
continue
23+
}
24+
suffix := ""
25+
fragment := strings.Split(v, ".")
26+
id := fragment[0]
27+
if len(fragment) > 1 {
28+
suffix = "." + fragment[1]
29+
}
30+
if util.IsID(id) {
31+
FileEntity, _, err := NoteStore.FindFileEntityFromID(id)
32+
if err == nil {
33+
virtualPath = append(virtualPath, FileEntity.Name+suffix)
34+
continue
35+
}
36+
}
37+
virtualPath = append(virtualPath, v)
38+
}
39+
path = strings.Join(virtualPath, "/")
40+
return path
41+
} else {
42+
// 直接使用 ID 作为路径名
43+
if conf.OutMode != "id" {
44+
util.Warn("OutMode 参数的值在预设之外,默认采用 id 模式")
45+
}
46+
return path
47+
}
48+
}

src/util/file.go

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
// WriteFile 确保文件路径存在,且不超出 conf.outDir
1515
func WriteFile(targetPath string, data []byte, perm os.FileMode) error {
16+
targetPath = filepath.ToSlash(targetPath)
1617
rel, err := filepath.Rel(conf.OutDir, targetPath)
1718
if err != nil {
1819
return err

src/util/notes_handle.go

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ func IsID(id string) bool {
3636
reg, _ := regexp.Compile(`^\d{14}-[a-z0-9]{7}`)
3737
return reg.MatchString(id)
3838
}
39-
4039
// TimeFromID 从 id 中提取创建时间
4140
func TimeFromID(id string) string {
4241
reg, _ := regexp.Compile(`^\d+`)

0 commit comments

Comments
 (0)