Skip to content

Commit 4e1b032

Browse files
authored
Merge pull request #285 from cnblogs/fix-auto-extract-images
fix: auto extract images
2 parents 870ca27 + f0fbd02 commit 4e1b032

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

src/cmd/post-list/modify-post-setting.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { postDataProvider } from '@/tree-view/provider/post-data-provider'
1111
import { PostTreeItem } from '@/tree-view/model/post-tree-item'
1212
import { postCategoryDataProvider } from '@/tree-view/provider/post-category-tree-data-provider'
1313
import { fsUtil } from '@/infra/fs/fsUtil'
14+
import { autoExtractImages } from '@/service/extract-img/extract-img'
1415

1516
export async function modifyPostSetting(input: Post | PostTreeItem | Uri) {
1617
let post: Post | undefined
@@ -46,6 +47,7 @@ export async function modifyPostSetting(input: Post | PostTreeItem | Uri) {
4647
if (localFilePath !== undefined && (await fsUtil.exists(localFilePath))) {
4748
await saveFilePendingChanges(localFilePath)
4849
post.postBody = await new LocalPost(localFilePath).readAllText()
50+
await autoExtractImages(post, localFilePath)
4951
}
5052
return true
5153
},

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { PostTreeItem } from '@/tree-view/model/post-tree-item'
1515
import { MarkdownCfg } from '@/ctx/cfg/markdown'
1616
import { PostListView } from '@/cmd/post-list/post-list-view'
1717
import { LocalPost } from '@/service/local-post'
18-
import { extractImg } from '@/service/extract-img/extract-img'
18+
import { autoExtractImages, extractImg } from '@/service/extract-img/extract-img'
1919
import { dirname } from 'path'
2020
import { Workspace } from '@/cmd/workspace'
2121
import { fsUtil } from '@/infra/fs/fsUtil'
@@ -69,26 +69,8 @@ export async function saveLocalPost(localPost: LocalPost) {
6969
const text = await localPost.readAllText()
7070
if (isEmptyBody(text)) return false
7171

72-
// TODO: need refactor
73-
const autoExtractImgSrc = MarkdownCfg.getAutoExtractImgSrc()
74-
const fileDir = dirname(localPost.filePath)
7572
postToSave.postBody = text
76-
if (autoExtractImgSrc !== undefined) {
77-
const extracted = await extractImg(text, fileDir, autoExtractImgSrc)
78-
if (extracted !== undefined) {
79-
postToSave.postBody = extracted
80-
if (isEmptyBody(postToSave.postBody, '(发生于提取图片后')) return false
81-
82-
if (MarkdownCfg.getApplyAutoExtractImgToLocal()) {
83-
const doc = window.visibleTextEditors.find(x => x.document.uri.fsPath === localPost.filePath)
84-
?.document
85-
if (doc !== undefined) {
86-
const we = Workspace.resetTextDoc(doc, extracted)
87-
await workspace.applyEdit(we)
88-
}
89-
}
90-
}
91-
}
73+
await autoExtractImages(postToSave, localPost.filePath)
9274

9375
return true
9476
},

src/service/extract-img/extract-img.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import { ProgressLocation, window } from 'vscode'
1+
import { ProgressLocation, window, workspace } from 'vscode'
22
import { ImgSrc } from '@/service/extract-img/get-replace-list'
33
import { Alert } from '@/infra/alert'
44
import { findImgLink } from '@/service/extract-img/find-img-link'
55
import { getReplaceList } from '@/service/extract-img/get-replace-list'
66
import { applyReplaceList } from '@/service/extract-img/apply-replace-list'
7+
import { MarkdownCfg } from '@/ctx/cfg/markdown'
8+
import { dirname } from 'path'
9+
import { Post } from '@/model/post'
10+
import { Workspace } from '@/cmd/workspace'
711

812
export async function extractImg(text: string, fileDir: string, inputImgSrc?: ImgSrc) {
913
let imgInfoList = findImgLink(text)
@@ -89,3 +93,27 @@ export async function extractImg(text: string, fileDir: string, inputImgSrc?: Im
8993
}
9094
)
9195
}
96+
97+
export async function autoExtractImages(post: Post, fileFsPath: string) {
98+
const autoExtractImgSrc = MarkdownCfg.getAutoExtractImgSrc()
99+
const fileDir = dirname(fileFsPath)
100+
if (autoExtractImgSrc !== undefined) {
101+
const extracted = await extractImg(post.postBody, fileDir, autoExtractImgSrc)
102+
if (extracted != null) {
103+
if (extracted === '') {
104+
void Alert.warn('提取图片后博文内容为空,放弃提取')
105+
return
106+
}
107+
108+
post.postBody = extracted
109+
110+
if (MarkdownCfg.getApplyAutoExtractImgToLocal()) {
111+
const doc = window.visibleTextEditors.find(x => x.document.uri.fsPath === fileFsPath)?.document
112+
if (doc !== undefined) {
113+
const we = Workspace.resetTextDoc(doc, extracted)
114+
await workspace.applyEdit(we)
115+
}
116+
}
117+
}
118+
}
119+
}

0 commit comments

Comments
 (0)