Skip to content

Commit ae24abc

Browse files
committed
fix: remove local path support for post category
1 parent 98f19ae commit ae24abc

File tree

7 files changed

+12
-59
lines changed

7 files changed

+12
-59
lines changed

package.json

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -487,13 +487,6 @@
487487
"editPresentation": "singlelineText",
488488
"markdownDescription": "macOS 上 Chromium 可执行文件路径, 用于进行 PDF 导出等操作"
489489
},
490-
"cnblogsClient.createLocalPostFileWithCategory": {
491-
"order": 5,
492-
"default": true,
493-
"scope": "application",
494-
"type": "boolean",
495-
"markdownDescription": "创建本地博文时, 是否根据博文分类保存到对应的文件夹中"
496-
},
497490
"cnblogsClient.pageSize.postList": {
498491
"order": 6,
499492
"default": 30,

src/cmd/post-cat/update-post-cat-treeview.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import { ProgressLocation, window, Uri, workspace } from 'vscode'
1+
import { ProgressLocation, window } from 'vscode'
22
import { PostCat } from '@/model/post-cat'
33
import { PostCatService } from '@/service/post/post-cat'
44
import { inputPostCat } from './input-post-cat'
5-
import { PostCatCfg } from '@/ctx/cfg/post-cat'
6-
import { WorkspaceCfg } from '@/ctx/cfg/workspace'
75
import { Alert } from '@/infra/alert'
86
import { PostCatTreeItem } from '@/tree-view/model/post-category-tree-item'
97
import { extTreeViews } from '@/tree-view/tree-view-register'
108
import { postCategoryDataProvider } from '@/tree-view/provider/post-category-tree-data-provider'
11-
import { fsUtil } from '@/infra/fs/fsUtil'
129

1310
export async function updatePostCatTreeView(arg?: PostCat | PostCatTreeItem) {
1411
let category: PostCat
@@ -36,16 +33,6 @@ export async function updatePostCatTreeView(arg?: PostCat | PostCatTreeItem) {
3633
try {
3734
await PostCatService.update(updateDto)
3835
postCategoryDataProvider.refresh()
39-
// 如果选择了createLocalPostFileWithCategory模式且本地有该目录,则重命名该目录
40-
const workspaceUri = WorkspaceCfg.getWorkspaceUri()
41-
const shouldCreateLocalPostFileWithCategory = PostCatCfg.isCreateLocalPostFileWithCategory()
42-
const path = Uri.joinPath(workspaceUri, category.title).fsPath
43-
const isFileExist = await fsUtil.exists(path)
44-
if (shouldCreateLocalPostFileWithCategory && isFileExist) {
45-
const oldUri = Uri.joinPath(workspaceUri, category.title)
46-
const newUri = Uri.joinPath(workspaceUri, addDto.title)
47-
await workspace.fs.rename(oldUri, newUri)
48-
}
4936
p.report({ increment: 100 })
5037
} catch (e) {
5138
void Alert.err(`更新博文失败: ${<string>e}`)

src/cmd/post-list/open-post-file.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { LocalPost } from '@/service/local-post'
66
import { fsUtil } from '@/infra/fs/fsUtil'
77
import { postPull } from './post-pull'
88
import { Alert } from '@/infra/alert'
9+
import { WorkspaceCfg } from '@/ctx/cfg/workspace'
910

1011
export async function openPostFile(
1112
post: LocalPost | Post | string,
@@ -17,7 +18,10 @@ export async function openPostFile(
1718
filePath = post.filePath
1819
} else if (post instanceof Post) {
1920
filePath = PostFileMapManager.getFilePath(post.id) ?? ''
20-
if (autoPull) if (!(await fsUtil.exists(filePath))) await postPull(post, false, true)
21+
if (autoPull) {
22+
if (!(await fsUtil.exists(filePath)) || filePath.indexOf(WorkspaceCfg.getWorkspaceUri().path) < 0)
23+
await postPull(post, false, true)
24+
}
2125
} else {
2226
filePath = post
2327
}

src/cmd/post-list/open-post-in-vscode.ts

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,17 @@ import { Alert } from '@/infra/alert'
55
import { PostService } from '@/service/post/post'
66
import { PostFileMapManager } from '@/service/post/post-file-map'
77
import { openPostFile } from './open-post-file'
8-
import { PostCatService } from '@/service/post/post-cat'
98
import sanitizeFileName from 'sanitize-filename'
109
import { WorkspaceCfg } from '@/ctx/cfg/workspace'
11-
import { PostCatCfg } from '@/ctx/cfg/post-cat'
1210
import { fsUtil } from '@/infra/fs/fsUtil'
1311

14-
export async function buildLocalPostFileUri(post: Post, includePostId = false): Promise<Uri> {
12+
export function buildLocalPostFileUri(post: Post, includePostId = false): Uri {
1513
const workspaceUri = WorkspaceCfg.getWorkspaceUri()
16-
const shouldCreateLocalPostFileWithCategory = PostCatCfg.isCreateLocalPostFileWithCategory()
1714
const ext = `.${post.isMarkdown ? 'md' : 'html'}`
1815
const postIdSegment = includePostId ? `.${post.id}` : ''
1916
const postTitle = sanitizeFileName(post.title)
2017

21-
if (!shouldCreateLocalPostFileWithCategory) return Uri.joinPath(workspaceUri, `${postTitle}${postIdSegment}${ext}`)
22-
23-
const firstCategoryId = post.categoryIds?.[0] ?? null
24-
let i = firstCategoryId !== null ? await PostCatService.getOne(firstCategoryId) : null
25-
let categoryTitle = ''
26-
while (i != null) {
27-
categoryTitle = path.join(
28-
sanitizeFileName(i.title, {
29-
replacement: invalidChar => (invalidChar === '/' ? '_' : ''),
30-
}),
31-
categoryTitle
32-
)
33-
i = i.parent ?? null
34-
}
35-
36-
return Uri.joinPath(workspaceUri, categoryTitle, `${postTitle}${postIdSegment}${ext}`)
18+
return Uri.joinPath(workspaceUri, `${postTitle}${postIdSegment}${ext}`)
3719
}
3820

3921
export async function openPostInVscode(postId: number, forceUpdateLocalPostFile = false): Promise<Uri | false> {
@@ -53,7 +35,7 @@ export async function openPostInVscode(postId: number, forceUpdateLocalPostFile
5335

5436
const workspaceUri = WorkspaceCfg.getWorkspaceUri()
5537
await mkDirIfNotExist(workspaceUri)
56-
let fileUri = mappedPostFilePath !== undefined ? Uri.file(mappedPostFilePath) : await buildLocalPostFileUri(post)
38+
let fileUri = mappedPostFilePath !== undefined ? Uri.file(mappedPostFilePath) : buildLocalPostFileUri(post)
5739

5840
// 博文尚未关联到本地文件的情况
5941
// 本地存在和博文同名的文件, 询问用户是要覆盖还是同时保留两者
@@ -65,7 +47,7 @@ export async function openPostInVscode(postId: number, forceUpdateLocalPostFile
6547
...opt
6648
)
6749

68-
if (selected === opt[0]) fileUri = await buildLocalPostFileUri(post, true)
50+
if (selected === opt[0]) fileUri = buildLocalPostFileUri(post, true)
6951
}
7052

7153
// 博文内容写入本地文件, 若文件不存在, 会自动创建对应的文件

src/cmd/post-list/post-pull-all.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export async function postPullAll() {
6868

6969
// 本地没有博文或关联到的文件不存在
7070
if (path === undefined || !(await fsUtil.exists(path))) {
71-
const uri = await buildLocalPostFileUri(post, false)
71+
const uri = buildLocalPostFileUri(post, false)
7272
const buf = Buffer.from(post.postBody)
7373
await workspace.fs.writeFile(uri, buf)
7474
await PostFileMapManager.updateOrCreate(post.id, uri.path)

src/cmd/post-list/post-pull.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export async function postPull(input: Post | PostTreeItem | Uri | undefined | nu
2424
path.indexOf(WorkspaceCfg.getWorkspaceUri().path) < 0
2525
) {
2626
isFreshPull = true
27-
const uri = await buildLocalPostFileUri(post, false)
27+
const uri = buildLocalPostFileUri(post, false)
2828
await workspace.fs.writeFile(uri, Buffer.from(post.postBody))
2929
await PostFileMapManager.updateOrCreate(post.id, uri.path)
3030
await handlePostInput(input, ctxList, uri.path)

src/ctx/cfg/post-cat.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)