Skip to content

Commit 9b84655

Browse files
authored
fix(auto-extract-images): fix save-to-cnblogs blocked by auto-extract-images in some cases if enabled (#69)
1 parent 685a47d commit 9b84655

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"@typescript-eslint/naming-convention": "warn",
1313
"@typescript-eslint/semi": "warn",
1414
"curly": "warn",
15-
"eqeqeq": "warn",
15+
"eqeqeq": ["warn", "smart"],
1616
"no-throw-literal": "warn",
1717
"semi": "warn",
1818
"require-await": "warn",

src/commands/extract-images.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,49 @@
11
import { Uri, workspace, window, MessageOptions, MessageItem, ProgressLocation, Range } from 'vscode';
22
import { MarkdownImage, MarkdownImagesExtractor } from '../services/images-extractor.service';
33

4+
type ExtractOption = MessageItem & Partial<Pick<MarkdownImagesExtractor, 'imageType'>>;
5+
const extractOptions: readonly ExtractOption[] = [
6+
{ title: '提取本地图片', imageType: 'local' },
7+
{ title: '提取网络图片', imageType: 'web' },
8+
{ title: '提取全部', imageType: 'all' },
9+
{ title: '取消', imageType: undefined, isCloseAffordance: true },
10+
];
11+
412
export const extractImages = async (
513
arg: unknown,
614
inputImageType: MarkdownImagesExtractor['imageType'] | null | undefined
7-
) => {
15+
): Promise<void> => {
816
if (arg instanceof Uri && arg.scheme === 'file') {
17+
const ignoreWarnings = inputImageType != null;
918
const markdown = new TextDecoder().decode(await workspace.fs.readFile(arg));
1019
const extractor = new MarkdownImagesExtractor(markdown, arg);
1120
const images = extractor.findImages();
1221
const availableWebImagesCount = images.filter(extractor.createImageTypeFilter('web')).length;
1322
const availableLocalImagesCount = images.filter(extractor.createImageTypeFilter('local')).length;
23+
const warnNoImages = (): void => void (ignoreWarnings ? null : window.showWarningMessage('没有可以提取的图片'));
1424
if (images.length <= 0) {
15-
await window.showWarningMessage('没有可以提取的图片');
16-
return;
25+
return warnNoImages();
1726
}
18-
const messageItems: (MessageItem & Partial<Pick<MarkdownImagesExtractor, 'imageType'>>)[] = [
19-
{ title: '提取本地图片', imageType: 'local' },
20-
{ title: '提取网络图片', imageType: 'web' },
21-
{ title: '提取全部', imageType: 'all' },
22-
{ title: '取消', imageType: undefined, isCloseAffordance: true },
23-
];
24-
let result = messageItems.find(x => inputImageType != null && x.imageType === inputImageType);
27+
28+
let result = extractOptions.find(x => inputImageType != null && x.imageType === inputImageType);
2529
result = result
2630
? result
27-
: await window.showInformationMessage<typeof messageItems extends [...infer U] ? U[0] : never>(
31+
: await window.showInformationMessage<ExtractOption>(
2832
'请选择要提取哪些图片? 注意! 此操作会替换源文件中的图片链接!',
2933
{
3034
modal: true,
3135
detail:
3236
`共找到 ${availableWebImagesCount} 张可以提取的网络图片\n` +
3337
`${availableLocalImagesCount} 张可以提取的本地图片`,
3438
} as MessageOptions,
35-
...messageItems
39+
...extractOptions
3640
);
3741
const editor = window.visibleTextEditors.find(x => x.document.fileName === arg.fsPath);
3842

3943
if (result && result.imageType && editor) {
4044
extractor.imageType = result.imageType;
4145
if (extractor.findImages().length <= 0) {
42-
await window.showWarningMessage('没有可以提取的图片');
43-
return;
46+
return warnNoImages();
4447
}
4548
const document = editor.document;
4649
await document.save();

0 commit comments

Comments
 (0)