Skip to content

Commit

Permalink
fix: auto extract images
Browse files Browse the repository at this point in the history
  • Loading branch information
cnblogs-dudu committed Jan 7, 2024
1 parent 870ca27 commit f0fbd02
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
2 changes: 2 additions & 0 deletions src/cmd/post-list/modify-post-setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { postDataProvider } from '@/tree-view/provider/post-data-provider'
import { PostTreeItem } from '@/tree-view/model/post-tree-item'
import { postCategoryDataProvider } from '@/tree-view/provider/post-category-tree-data-provider'
import { fsUtil } from '@/infra/fs/fsUtil'
import { autoExtractImages } from '@/service/extract-img/extract-img'

export async function modifyPostSetting(input: Post | PostTreeItem | Uri) {
let post: Post | undefined
Expand Down Expand Up @@ -46,6 +47,7 @@ export async function modifyPostSetting(input: Post | PostTreeItem | Uri) {
if (localFilePath !== undefined && (await fsUtil.exists(localFilePath))) {
await saveFilePendingChanges(localFilePath)
post.postBody = await new LocalPost(localFilePath).readAllText()
await autoExtractImages(post, localFilePath)
}
return true
},
Expand Down
22 changes: 2 additions & 20 deletions src/cmd/post-list/upload-post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { PostTreeItem } from '@/tree-view/model/post-tree-item'
import { MarkdownCfg } from '@/ctx/cfg/markdown'
import { PostListView } from '@/cmd/post-list/post-list-view'
import { LocalPost } from '@/service/local-post'
import { extractImg } from '@/service/extract-img/extract-img'
import { autoExtractImages, extractImg } from '@/service/extract-img/extract-img'
import { dirname } from 'path'
import { Workspace } from '@/cmd/workspace'
import { fsUtil } from '@/infra/fs/fsUtil'
Expand Down Expand Up @@ -69,26 +69,8 @@ export async function saveLocalPost(localPost: LocalPost) {
const text = await localPost.readAllText()
if (isEmptyBody(text)) return false

// TODO: need refactor
const autoExtractImgSrc = MarkdownCfg.getAutoExtractImgSrc()
const fileDir = dirname(localPost.filePath)
postToSave.postBody = text
if (autoExtractImgSrc !== undefined) {
const extracted = await extractImg(text, fileDir, autoExtractImgSrc)
if (extracted !== undefined) {
postToSave.postBody = extracted
if (isEmptyBody(postToSave.postBody, '(发生于提取图片后')) return false

if (MarkdownCfg.getApplyAutoExtractImgToLocal()) {
const doc = window.visibleTextEditors.find(x => x.document.uri.fsPath === localPost.filePath)
?.document
if (doc !== undefined) {
const we = Workspace.resetTextDoc(doc, extracted)
await workspace.applyEdit(we)
}
}
}
}
await autoExtractImages(postToSave, localPost.filePath)

return true
},
Expand Down
30 changes: 29 additions & 1 deletion src/service/extract-img/extract-img.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { ProgressLocation, window } from 'vscode'
import { ProgressLocation, window, workspace } from 'vscode'
import { ImgSrc } from '@/service/extract-img/get-replace-list'
import { Alert } from '@/infra/alert'
import { findImgLink } from '@/service/extract-img/find-img-link'
import { getReplaceList } from '@/service/extract-img/get-replace-list'
import { applyReplaceList } from '@/service/extract-img/apply-replace-list'
import { MarkdownCfg } from '@/ctx/cfg/markdown'
import { dirname } from 'path'
import { Post } from '@/model/post'
import { Workspace } from '@/cmd/workspace'

export async function extractImg(text: string, fileDir: string, inputImgSrc?: ImgSrc) {
let imgInfoList = findImgLink(text)
Expand Down Expand Up @@ -89,3 +93,27 @@ export async function extractImg(text: string, fileDir: string, inputImgSrc?: Im
}
)
}

export async function autoExtractImages(post: Post, fileFsPath: string) {
const autoExtractImgSrc = MarkdownCfg.getAutoExtractImgSrc()
const fileDir = dirname(fileFsPath)
if (autoExtractImgSrc !== undefined) {
const extracted = await extractImg(post.postBody, fileDir, autoExtractImgSrc)
if (extracted != null) {
if (extracted === '') {
void Alert.warn('提取图片后博文内容为空,放弃提取')
return
}

post.postBody = extracted

if (MarkdownCfg.getApplyAutoExtractImgToLocal()) {
const doc = window.visibleTextEditors.find(x => x.document.uri.fsPath === fileFsPath)?.document
if (doc !== undefined) {
const we = Workspace.resetTextDoc(doc, extracted)
await workspace.applyEdit(we)
}
}
}
}
}

0 comments on commit f0fbd02

Please sign in to comment.