Skip to content

Commit de1454e

Browse files
authored
fix(auto-extract-images): fix extracted images not applied before save post (#107)
1 parent 1943868 commit de1454e

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

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

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ export const saveLocalDraftToCnblogs = async (localDraft: LocalDraft) => {
105105
successCallback: async savedPost => {
106106
await refreshPostsList();
107107
await openPostFile(localDraft);
108-
if (Settings.automaticallyExtractImagesType)
109-
await extractImages(localDraft.filePathUri, Settings.automaticallyExtractImagesType);
110108

111109
await PostFileMapManager.updateOrCreate(savedPost.id, localDraft.filePath);
112110
await openPostFile(localDraft);
@@ -120,14 +118,18 @@ export const saveLocalDraftToCnblogs = async (localDraft: LocalDraft) => {
120118
AlertService.warning('本地文件已删除, 无法新建博文');
121119
return false;
122120
}
123-
const content = await localDraft.readAllText();
124-
postToSave.postBody = content;
121+
if (Settings.automaticallyExtractImagesType) {
122+
await extractImages(localDraft.filePathUri, Settings.automaticallyExtractImagesType).catch(
123+
console.warn
124+
);
125+
}
126+
postToSave.postBody = await localDraft.readAllText();
125127
return true;
126128
},
127129
});
128130
};
129131

130-
export const savePostToCnblogs = async (input: Post | PostTreeItem | PostEditDto | undefined, isNewPost = false) => {
132+
export const savePostToCnblogs = async (input: Post | PostTreeItem | PostEditDto | undefined) => {
131133
input = input instanceof PostTreeItem ? input.post : input;
132134
const post =
133135
input instanceof PostEditDto
@@ -139,16 +141,14 @@ export const savePostToCnblogs = async (input: Post | PostTreeItem | PostEditDto
139141

140142
const { id: postId } = post;
141143
const localFilePath = PostFileMapManager.getFilePath(postId);
144+
if (!localFilePath) return AlertService.warning('本地无该博文的编辑记录');
145+
146+
if (Settings.automaticallyExtractImagesType)
147+
await extractImages(Uri.file(localFilePath), Settings.automaticallyExtractImagesType).catch(console.warn);
148+
142149
await saveFilePendingChanges(localFilePath);
143-
if (!isNewPost) {
144-
if (!localFilePath) {
145-
AlertService.warning('本地无该博文的编辑记录');
146-
return;
147-
}
148-
const updatedPostBody = new TextDecoder().decode(await workspace.fs.readFile(Uri.file(localFilePath)));
149-
post.postBody = updatedPostBody;
150-
post.title = await PostTitleSanitizer.unSanitize(post);
151-
}
150+
post.postBody = (await workspace.fs.readFile(Uri.file(localFilePath))).toString();
151+
post.title = await PostTitleSanitizer.unSanitize(post);
152152

153153
if (!validatePost(post)) return false;
154154

@@ -164,12 +164,9 @@ export const savePostToCnblogs = async (input: Post | PostTreeItem | PostEditDto
164164
});
165165
let hasSaved = false;
166166
try {
167-
if (Settings.automaticallyExtractImagesType && localFilePath)
168-
await extractImages(Uri.file(localFilePath), Settings.automaticallyExtractImagesType);
169-
170167
const { id: postId } = await postService.updatePost(post);
171-
if (!isNewPost) await openPostInVscode(postId);
172-
else post.id = postId;
168+
await openPostInVscode(postId);
169+
post.id = postId;
173170

174171
hasSaved = true;
175172
progress.report({ increment: 100 });

0 commit comments

Comments
 (0)