From f0fbd0270eca8cde44964af1c42a0548537fe3a3 Mon Sep 17 00:00:00 2001 From: dudu Date: Sun, 7 Jan 2024 11:15:27 +0800 Subject: [PATCH] fix: auto extract images --- src/cmd/post-list/modify-post-setting.ts | 2 ++ src/cmd/post-list/upload-post.ts | 22 ++--------------- src/service/extract-img/extract-img.ts | 30 +++++++++++++++++++++++- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/cmd/post-list/modify-post-setting.ts b/src/cmd/post-list/modify-post-setting.ts index 9adca3c6..d7b25027 100644 --- a/src/cmd/post-list/modify-post-setting.ts +++ b/src/cmd/post-list/modify-post-setting.ts @@ -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 @@ -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 }, diff --git a/src/cmd/post-list/upload-post.ts b/src/cmd/post-list/upload-post.ts index d0dab3ff..dc4192b9 100644 --- a/src/cmd/post-list/upload-post.ts +++ b/src/cmd/post-list/upload-post.ts @@ -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' @@ -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 }, diff --git a/src/service/extract-img/extract-img.ts b/src/service/extract-img/extract-img.ts index dd1aa355..3486eef4 100644 --- a/src/service/extract-img/extract-img.ts +++ b/src/service/extract-img/extract-img.ts @@ -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) @@ -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) + } + } + } + } +}