Skip to content

Commit 564f6dc

Browse files
authored
feat: allow create local post file with category (#50)
1 parent 2afb486 commit 564f6dc

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,13 @@
255255
"type": "string",
256256
"editPresentation": "singlelineText",
257257
"markdownDescription": "Chromium可执行文件路径, 用于进行pdf导出等操作"
258+
},
259+
"cnblogsClientForVSCode.createLocalPostFileWithCategory": {
260+
"order": 3,
261+
"default": true,
262+
"scope": "application",
263+
"type": "boolean",
264+
"markdownDescription": "设置是否根据博文分类保存到不同的文件夹中"
258265
}
259266
}
260267
}

src/commands/post-category/update-post-category.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { MessageOptions, ProgressLocation, window } from 'vscode';
1+
import fs from 'fs';
2+
import { MessageOptions, ProgressLocation, window, Uri, workspace } from 'vscode';
23
import { PostCategory } from '../../models/post-category';
34
import { postCategoryService } from '../../services/post-category.service';
45
import { extensionViews } from '../../tree-view-providers/tree-view-registration';
56
import { inputPostCategory } from './input-post-category';
67
import { refreshPostCategoriesList } from './refresh-post-categories-list';
8+
import { Settings } from '../../services/settings.service';
79

810
export const updatePostCategory = async (category?: PostCategory) => {
911
if (!category) {
@@ -32,9 +34,19 @@ export const updatePostCategory = async (category?: PostCategory) => {
3234
try {
3335
await postCategoryService.updateCategory(updateDto);
3436
refreshPostCategoriesList();
37+
// 如果选择了createLocalPostFileWithCategory模式且本地有该目录,则重命名该目录
38+
const workspaceUri = Settings.workspaceUri;
39+
const createLocalPostFileWithCategory = Settings.createLocalPostFileWithCategory;
40+
const uri = Uri.joinPath(workspaceUri, category.title).fsPath;
41+
const isFileExist = fs.existsSync(uri);
42+
if (createLocalPostFileWithCategory && isFileExist) {
43+
const oldUri = Uri.joinPath(workspaceUri, category.title);
44+
const newUri = Uri.joinPath(workspaceUri, addDto.title);
45+
await workspace.fs.rename(oldUri, newUri);
46+
}
3547
} catch (err) {
3648
void window.showErrorMessage('更新博文分类失败', {
37-
detail: `服务器反回了错误, ${err instanceof Error ? err.message : JSON.stringify(err)}`,
49+
detail: `服务器返回了错误, ${err instanceof Error ? err.message : JSON.stringify(err)}`,
3850
modal: true,
3951
} as MessageOptions);
4052
} finally {

src/commands/posts-list/open-post-in-vscode.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,22 @@ import { PostFileMapManager } from '../../services/post-file-map';
88
import { Settings } from '../../services/settings.service';
99
import { openPostFile } from './open-post-file';
1010
import { PostTitleSanitizer } from '../../services/post-title-sanitizer.service';
11+
import { postCategoryService } from '../../services/post-category.service';
1112

1213
const buildLocalPostFileUri = async (post: Post, includePostId = false): Promise<Uri> => {
1314
const workspaceUri = Settings.workspaceUri;
15+
const createLocalPostFileWithCategory = Settings.createLocalPostFileWithCategory;
1416
const ext = `.${post.isMarkdown ? 'md' : 'html'}`;
1517
const postIdSegment = includePostId ? `.${post.id}` : '';
1618
const { text: postTitle } = await PostTitleSanitizer.sanitize(post);
17-
return Uri.joinPath(workspaceUri, `${postTitle}${postIdSegment}${ext}`);
19+
if (createLocalPostFileWithCategory) {
20+
let categories = await postCategoryService.fetchCategories();
21+
categories = categories.filter(x => post.categoryIds?.includes(x.categoryId));
22+
const categoryTitle = categories[0]?.title ?? '';
23+
return Uri.joinPath(workspaceUri, categoryTitle, `${postTitle}${postIdSegment}${ext}`);
24+
} else {
25+
return Uri.joinPath(workspaceUri, `${postTitle}${postIdSegment}${ext}`);
26+
}
1827
};
1928

2029
export const openPostInVscode = async (postId: number, forceUpdateLocalPostFile = false): Promise<Uri | false> => {

src/services/settings.service.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,12 @@ export class Settings {
6666
}
6767
await this.configuration.update(this.chromiumPathConfigurationKey, value, ConfigurationTarget.Global);
6868
}
69+
70+
static get createLocalPostFileWithCategory(): boolean {
71+
return this.configuration.get<boolean>('createLocalPostFileWithCategory') ?? false;
72+
}
73+
74+
static async setCreateLocalPostFileWithCategory(value: boolean) {
75+
await this.configuration.update('createLocalPostFileWithCategory', value, ConfigurationTarget.Global);
76+
}
6977
}

0 commit comments

Comments
 (0)