Skip to content

Commit 330b88b

Browse files
authored
feat(#37): allow use vscode-cnb.save-post-file-to-cnblogs with command palette (#38)
* feat(#37): allow use vscode-cnb.save-post-file-to-cnblogs with command pattle * docs: update readme
1 parent 5c66675 commit 330b88b

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232

3333
若本地文件已经关联到一篇博客园博文, 那么会直接更新这篇博文.
3434

35+
也通过vscode的`Command Palette`(唤起`Command Palette`快捷键, windows:`ctrl+shift+p`, macos: `command+shift+p`)调用`Cnblogs: 保存到博客园`命令, 将当前正在编辑的markdown文件保存到博客园上
36+
37+
![image](https://img2022.cnblogs.com/blog/1596066/202203/1596066-20220323151757542-155709896.png)
38+
3539
### 博客园博文列表
3640

3741
当点击列表中的博文时, 会自动将博文内容下载到工作空间一个本地文件中(此时这个本地文件就关联到了这篇博文), 完成编辑后可以再将本地的内容保存到博客园博文

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@
148148
"command": "vscode-cnb.save-post-file-to-cnblogs",
149149
"title": "保存到博客园",
150150
"icon": "$(cloud-upload)",
151-
"enablement": "vscode-cnb.isAuthorized"
151+
"enablement": "vscode-cnb.isAuthorized",
152+
"category": "Cnblogs"
152153
},
153154
{
154155
"command": "vscode-cnb.show-post-to-local-file-info",
@@ -354,7 +355,7 @@
354355
},
355356
{
356357
"command": "vscode-cnb.save-post-file-to-cnblogs",
357-
"when": "false"
358+
"when": "true"
358359
},
359360
{
360361
"command": "vscode-cnb.delete-local-draft",

src/commands/posts-list/save-post.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,29 @@ import { refreshPostsList } from './refresh-posts-list';
1414
import { PostEditDto } from '../../models/post-edit-dto';
1515
import { PostTitleSanitizer } from '../../services/post-title-sanitizer.service';
1616

17-
export const savePostFileToCnblogs = async (fileUri: Uri) => {
18-
if (!fileUri || fileUri.scheme !== 'file') {
17+
const parseFileUri = async (fileUri: Uri | undefined): Promise<Uri | undefined> => {
18+
if (fileUri && fileUri.scheme !== 'file') {
19+
fileUri = undefined;
20+
} else if (!fileUri) {
21+
const { activeTextEditor } = window;
22+
if (activeTextEditor) {
23+
const { document } = activeTextEditor;
24+
if (document.languageId === 'markdown' && !document.isUntitled) {
25+
await document.save();
26+
fileUri = document.uri;
27+
}
28+
}
29+
}
30+
31+
return fileUri;
32+
};
33+
34+
export const savePostFileToCnblogs = async (fileUri: Uri | undefined) => {
35+
fileUri = await parseFileUri(fileUri);
36+
if (!fileUri) {
1937
return;
2038
}
21-
const filePath = fileUri.fsPath;
22-
// const fileName = path.basename(filePath);
23-
// const fileNameWithoutExt = path.basename(fileName, path.extname(fileName));
39+
const { fsPath: filePath } = fileUri;
2440
const postId = PostFileMapManager.getPostId(filePath);
2541
if (postId && postId >= 0) {
2642
await savePostToCnblogs(await postService.fetchPostEditDto(postId));

0 commit comments

Comments
 (0)