|
1 |
| -import { ProgressLocation, window } from 'vscode' |
| 1 | +import { ProgressLocation, window, workspace } from 'vscode' |
2 | 2 | import { ImgSrc } from '@/service/extract-img/get-replace-list'
|
3 | 3 | import { Alert } from '@/infra/alert'
|
4 | 4 | import { findImgLink } from '@/service/extract-img/find-img-link'
|
5 | 5 | import { getReplaceList } from '@/service/extract-img/get-replace-list'
|
6 | 6 | 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' |
7 | 11 |
|
8 | 12 | export async function extractImg(text: string, fileDir: string, inputImgSrc?: ImgSrc) {
|
9 | 13 | let imgInfoList = findImgLink(text)
|
@@ -89,3 +93,27 @@ export async function extractImg(text: string, fileDir: string, inputImgSrc?: Im
|
89 | 93 | }
|
90 | 94 | )
|
91 | 95 | }
|
| 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