fix(build): tsc --noEmit зелёный, добавлен npm run typecheck
- @types/node@20 в devDependencies (без него TS2688) - skipLibCheck: obsidian.d.ts несовместим с TS 5.9 (HistoryHandler) - настройки модулей: двойной cast Record<string, unknown> -> Settings - legacy Vault API (getConfig, getAvailablePath) вынесен в типизированный интерфейс - тестовые моки типизированы (VaultMock, makeFile) - vitest.config: удалён неподдерживаемый transformMode - main.js пересобран, поведение не менялось
This commit is contained in:
1 parent
d6048e5956
commit
7cc89afbf7
10 files changed
+113
-125
No files matched your search
@@ -200,17 +200,18 @@ var AttachmentCleanPasteModule = class extends BaseModule {
|
|||||||
const arrayBuffer = await file.arrayBuffer();
|
const arrayBuffer = await file.arrayBuffer();
|
||||||
const filename = file.name;
|
const filename = file.name;
|
||||||
const nameWithoutExt = filename.replace(/\.[^/.]+$/, "");
|
const nameWithoutExt = filename.replace(/\.[^/.]+$/, "");
|
||||||
let attachmentFolder = this.app.vault.getConfig("attachmentFolderPath");
|
const vault = this.app.vault;
|
||||||
|
let attachmentFolder = vault.getConfig("attachmentFolderPath");
|
||||||
if (!attachmentFolder)
|
if (!attachmentFolder)
|
||||||
attachmentFolder = "attachments";
|
attachmentFolder = "attachments";
|
||||||
const folderPath = (0, import_obsidian.normalizePath)(attachmentFolder);
|
const folderPath = (0, import_obsidian.normalizePath)(attachmentFolder);
|
||||||
if (!await this.app.vault.adapter.exists(folderPath)) {
|
if (!await vault.adapter.exists(folderPath)) {
|
||||||
await this.app.vault.createFolder(folderPath);
|
await vault.createFolder(folderPath);
|
||||||
}
|
}
|
||||||
const ext = filename.slice(filename.lastIndexOf(".") + 1);
|
const ext = filename.slice(filename.lastIndexOf(".") + 1);
|
||||||
const basePath = (0, import_obsidian.normalizePath)(`${folderPath}/${nameWithoutExt}`);
|
const basePath = (0, import_obsidian.normalizePath)(`${folderPath}/${nameWithoutExt}`);
|
||||||
const availablePath = await this.app.vault.getAvailablePath(basePath, ext);
|
const availablePath = await vault.getAvailablePath(basePath, ext);
|
||||||
await this.app.vault.createBinary(availablePath, arrayBuffer);
|
await vault.createBinary(availablePath, arrayBuffer);
|
||||||
return `[[${availablePath}|${nameWithoutExt}]]`;
|
return `[[${availablePath}|${nameWithoutExt}]]`;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
new import_obsidian.Notice(`Clean Paste error: ${e.message}`);
|
new import_obsidian.Notice(`Clean Paste error: ${e.message}`);
|
||||||
|
|||||||
Generated
+18
-39
@@ -9,6 +9,7 @@
|
|||||||
"version": "1.1.5",
|
"version": "1.1.5",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/node": "^20.19.43",
|
||||||
"@typescript-eslint/eslint-plugin": "^7.18.0",
|
"@typescript-eslint/eslint-plugin": "^7.18.0",
|
||||||
"@typescript-eslint/parser": "^7.18.0",
|
"@typescript-eslint/parser": "^7.18.0",
|
||||||
"builtin-modules": "^3.3.0",
|
"builtin-modules": "^3.3.0",
|
||||||
@@ -745,9 +746,6 @@
|
|||||||
"arm"
|
"arm"
|
||||||
],
|
],
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"libc": [
|
|
||||||
"glibc"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"os": [
|
"os": [
|
||||||
@@ -762,9 +760,6 @@
|
|||||||
"arm"
|
"arm"
|
||||||
],
|
],
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"libc": [
|
|
||||||
"musl"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"os": [
|
"os": [
|
||||||
@@ -779,9 +774,6 @@
|
|||||||
"arm64"
|
"arm64"
|
||||||
],
|
],
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"libc": [
|
|
||||||
"glibc"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"os": [
|
"os": [
|
||||||
@@ -796,9 +788,6 @@
|
|||||||
"arm64"
|
"arm64"
|
||||||
],
|
],
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"libc": [
|
|
||||||
"musl"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"os": [
|
"os": [
|
||||||
@@ -813,9 +802,6 @@
|
|||||||
"loong64"
|
"loong64"
|
||||||
],
|
],
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"libc": [
|
|
||||||
"glibc"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"os": [
|
"os": [
|
||||||
@@ -830,9 +816,6 @@
|
|||||||
"loong64"
|
"loong64"
|
||||||
],
|
],
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"libc": [
|
|
||||||
"musl"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"os": [
|
"os": [
|
||||||
@@ -847,9 +830,6 @@
|
|||||||
"ppc64"
|
"ppc64"
|
||||||
],
|
],
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"libc": [
|
|
||||||
"glibc"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"os": [
|
"os": [
|
||||||
@@ -864,9 +844,6 @@
|
|||||||
"ppc64"
|
"ppc64"
|
||||||
],
|
],
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"libc": [
|
|
||||||
"musl"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"os": [
|
"os": [
|
||||||
@@ -881,9 +858,6 @@
|
|||||||
"riscv64"
|
"riscv64"
|
||||||
],
|
],
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"libc": [
|
|
||||||
"glibc"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"os": [
|
"os": [
|
||||||
@@ -898,9 +872,6 @@
|
|||||||
"riscv64"
|
"riscv64"
|
||||||
],
|
],
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"libc": [
|
|
||||||
"musl"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"os": [
|
"os": [
|
||||||
@@ -915,9 +886,6 @@
|
|||||||
"s390x"
|
"s390x"
|
||||||
],
|
],
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"libc": [
|
|
||||||
"glibc"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"os": [
|
"os": [
|
||||||
@@ -932,9 +900,6 @@
|
|||||||
"x64"
|
"x64"
|
||||||
],
|
],
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"libc": [
|
|
||||||
"glibc"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"os": [
|
"os": [
|
||||||
@@ -949,9 +914,6 @@
|
|||||||
"x64"
|
"x64"
|
||||||
],
|
],
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"libc": [
|
|
||||||
"musl"
|
|
||||||
],
|
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"optional": true,
|
"optional": true,
|
||||||
"os": [
|
"os": [
|
||||||
@@ -1066,6 +1028,16 @@
|
|||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/@types/node": {
|
||||||
|
"version": "20.19.43",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.19.43.tgz",
|
||||||
|
"integrity": "sha512-6oYBAi5ikg4Pl+kGsoYtawUMBT2zZMCvPNF7pVLnHZfd1zf38DRiWn/gT01RYCdUqkv7Fhr+C9ot4/tb+2sVvA==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"undici-types": "~6.21.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@types/tern": {
|
"node_modules/@types/tern": {
|
||||||
"version": "0.23.9",
|
"version": "0.23.9",
|
||||||
"resolved": "https://registry.npmjs.org/@types/tern/-/tern-0.23.9.tgz",
|
"resolved": "https://registry.npmjs.org/@types/tern/-/tern-0.23.9.tgz",
|
||||||
@@ -3416,6 +3388,13 @@
|
|||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/undici-types": {
|
||||||
|
"version": "6.21.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz",
|
||||||
|
"integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/uri-js": {
|
"node_modules/uri-js": {
|
||||||
"version": "4.4.1",
|
"version": "4.4.1",
|
||||||
"resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz",
|
"resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz",
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
"dev": "node esbuild.config.mjs",
|
"dev": "node esbuild.config.mjs",
|
||||||
"build": "node esbuild.config.mjs production",
|
"build": "node esbuild.config.mjs production",
|
||||||
"test": "vitest run",
|
"test": "vitest run",
|
||||||
|
"typecheck": "tsc --noEmit",
|
||||||
"lint": "eslint src/",
|
"lint": "eslint src/",
|
||||||
"format": "prettier --check src/",
|
"format": "prettier --check src/",
|
||||||
"format:fix": "prettier --write src/",
|
"format:fix": "prettier --write src/",
|
||||||
@@ -19,6 +20,7 @@
|
|||||||
"author": "",
|
"author": "",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/node": "^20.19.43",
|
||||||
"@typescript-eslint/eslint-plugin": "^7.18.0",
|
"@typescript-eslint/eslint-plugin": "^7.18.0",
|
||||||
"@typescript-eslint/parser": "^7.18.0",
|
"@typescript-eslint/parser": "^7.18.0",
|
||||||
"builtin-modules": "^3.3.0",
|
"builtin-modules": "^3.3.0",
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ export class AttachmentCleanPasteModule extends BaseModule {
|
|||||||
|
|
||||||
onload(plugin: ZeroQoLModulesPlugin, moduleSettings: Record<string, unknown>): void {
|
onload(plugin: ZeroQoLModulesPlugin, moduleSettings: Record<string, unknown>): void {
|
||||||
this.app = plugin.app;
|
this.app = plugin.app;
|
||||||
const rawSettings = moduleSettings as AttachmentCleanPasteSettings;
|
const rawSettings = moduleSettings as unknown as AttachmentCleanPasteSettings;
|
||||||
|
|
||||||
const pasteRef = plugin.app.workspace.on(
|
const pasteRef = plugin.app.workspace.on(
|
||||||
'editor-paste',
|
'editor-paste',
|
||||||
@@ -141,20 +141,27 @@ export class AttachmentCleanPasteModule extends BaseModule {
|
|||||||
const filename = file.name;
|
const filename = file.name;
|
||||||
const nameWithoutExt = filename.replace(/\.[^/.]+$/, '');
|
const nameWithoutExt = filename.replace(/\.[^/.]+$/, '');
|
||||||
|
|
||||||
let attachmentFolder = this.app.vault.getConfig('attachmentFolderPath') as string;
|
// Legacy/undocumented Vault surface: `getConfig` and `getAvailablePath`
|
||||||
|
// exist at runtime but are absent from the public typings.
|
||||||
|
const vault = this.app.vault as typeof this.app.vault & {
|
||||||
|
getConfig(key: string): string;
|
||||||
|
getAvailablePath(basePath: string, extension: string): Promise<string>;
|
||||||
|
};
|
||||||
|
|
||||||
|
let attachmentFolder = vault.getConfig('attachmentFolderPath');
|
||||||
if (!attachmentFolder) attachmentFolder = 'attachments';
|
if (!attachmentFolder) attachmentFolder = 'attachments';
|
||||||
|
|
||||||
const folderPath = normalizePath(attachmentFolder);
|
const folderPath = normalizePath(attachmentFolder);
|
||||||
if (!(await this.app.vault.adapter.exists(folderPath))) {
|
if (!(await vault.adapter.exists(folderPath))) {
|
||||||
await this.app.vault.createFolder(folderPath);
|
await vault.createFolder(folderPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
const ext = filename.slice(filename.lastIndexOf('.') + 1);
|
const ext = filename.slice(filename.lastIndexOf('.') + 1);
|
||||||
const basePath = normalizePath(`${folderPath}/${nameWithoutExt}`);
|
const basePath = normalizePath(`${folderPath}/${nameWithoutExt}`);
|
||||||
|
|
||||||
const availablePath = await this.app.vault.getAvailablePath(basePath, ext);
|
const availablePath = await vault.getAvailablePath(basePath, ext);
|
||||||
|
|
||||||
await this.app.vault.createBinary(availablePath, arrayBuffer);
|
await vault.createBinary(availablePath, arrayBuffer);
|
||||||
|
|
||||||
return `[[${availablePath}|${nameWithoutExt}]]`;
|
return `[[${availablePath}|${nameWithoutExt}]]`;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ export class PreserveLinkAliasesModule extends BaseModule {
|
|||||||
|
|
||||||
onload(plugin: ZeroQoLModulesPlugin, moduleSettings: Record<string, unknown>): void {
|
onload(plugin: ZeroQoLModulesPlugin, moduleSettings: Record<string, unknown>): void {
|
||||||
this.vault = plugin.app.vault;
|
this.vault = plugin.app.vault;
|
||||||
const rawSettings = moduleSettings as PreserveLinkAliasesSettings;
|
const rawSettings = moduleSettings as unknown as PreserveLinkAliasesSettings;
|
||||||
|
|
||||||
const renameRef = plugin.app.vault.on('rename', (file: TFile, oldPath: string) =>
|
const renameRef = plugin.app.vault.on('rename', (file: TFile, oldPath: string) =>
|
||||||
this.handleRename(file, oldPath, rawSettings),
|
this.handleRename(file, oldPath, rawSettings),
|
||||||
|
|||||||
@@ -27,7 +27,10 @@ vi.mock('obsidian', () => ({
|
|||||||
// Minimal normalizePath: collapses backslashes, collapses duplicate slashes,
|
// Minimal normalizePath: collapses backslashes, collapses duplicate slashes,
|
||||||
// strips leading/trailing slash. Sufficient for the inputs in these tests.
|
// strips leading/trailing slash. Sufficient for the inputs in these tests.
|
||||||
normalizePath: (p: string): string =>
|
normalizePath: (p: string): string =>
|
||||||
p.replace(/\\/g, '/').replace(/\/+/g, '/').replace(/^\/|\/$/g, ''),
|
p
|
||||||
|
.replace(/\\/g, '/')
|
||||||
|
.replace(/\/+/g, '/')
|
||||||
|
.replace(/^\/|\/$/g, ''),
|
||||||
Plugin: class {},
|
Plugin: class {},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -38,8 +41,7 @@ import { AttachmentCleanPasteModule } from '../../src/modules/attachment-clean-p
|
|||||||
function createFile(name: string, content: string = 'data'): File {
|
function createFile(name: string, content: string = 'data'): File {
|
||||||
return {
|
return {
|
||||||
name,
|
name,
|
||||||
arrayBuffer: async () =>
|
arrayBuffer: async () => new TextEncoder().encode(content).buffer as ArrayBuffer,
|
||||||
new TextEncoder().encode(content).buffer as ArrayBuffer,
|
|
||||||
} as unknown as File;
|
} as unknown as File;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,11 +60,8 @@ describe('AttachmentCleanPasteModule', () => {
|
|||||||
createBinary: ReturnType<typeof vi.fn>;
|
createBinary: ReturnType<typeof vi.fn>;
|
||||||
};
|
};
|
||||||
let pasteHandler:
|
let pasteHandler:
|
||||||
| ((evt: ClipboardEvent, editor: unknown, info: unknown) => Promise<void>)
|
((evt: ClipboardEvent, editor: unknown, info: unknown) => Promise<void>) | null;
|
||||||
| null;
|
let dropHandler: ((evt: DragEvent, editor: unknown, info: unknown) => Promise<void>) | null;
|
||||||
let dropHandler:
|
|
||||||
| ((evt: DragEvent, editor: unknown, info: unknown) => Promise<void>)
|
|
||||||
| null;
|
|
||||||
let settings: { extensions: string };
|
let settings: { extensions: string };
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -92,9 +91,7 @@ describe('AttachmentCleanPasteModule', () => {
|
|||||||
adapter: mockAdapter,
|
adapter: mockAdapter,
|
||||||
createFolder: vi.fn(async () => undefined),
|
createFolder: vi.fn(async () => undefined),
|
||||||
// Mirrors the real Obsidian contract: returns "<basePath>.<ext>".
|
// Mirrors the real Obsidian contract: returns "<basePath>.<ext>".
|
||||||
getAvailablePath: vi.fn(
|
getAvailablePath: vi.fn(async (basePath: string, ext: string) => `${basePath}.${ext}`),
|
||||||
async (basePath: string, ext: string) => `${basePath}.${ext}`,
|
|
||||||
),
|
|
||||||
createBinary: vi.fn(async () => undefined),
|
createBinary: vi.fn(async () => undefined),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -109,9 +106,7 @@ describe('AttachmentCleanPasteModule', () => {
|
|||||||
|
|
||||||
module = new AttachmentCleanPasteModule();
|
module = new AttachmentCleanPasteModule();
|
||||||
module.onload(
|
module.onload(
|
||||||
mockPlugin as unknown as Parameters<
|
mockPlugin as unknown as Parameters<AttachmentCleanPasteModule['onload']>[0],
|
||||||
AttachmentCleanPasteModule['onload']
|
|
||||||
>[0],
|
|
||||||
settings,
|
settings,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@@ -124,10 +119,7 @@ describe('AttachmentCleanPasteModule', () => {
|
|||||||
): boolean =>
|
): boolean =>
|
||||||
(
|
(
|
||||||
module as unknown as {
|
module as unknown as {
|
||||||
shouldProcess: (
|
shouldProcess: (f: string, s: { extensions: string }) => boolean;
|
||||||
f: string,
|
|
||||||
s: { extensions: string },
|
|
||||||
) => boolean;
|
|
||||||
}
|
}
|
||||||
).shouldProcess(filename, cfg);
|
).shouldProcess(filename, cfg);
|
||||||
|
|
||||||
@@ -182,9 +174,7 @@ describe('AttachmentCleanPasteModule', () => {
|
|||||||
expect(stopPropagation).toHaveBeenCalledTimes(1);
|
expect(stopPropagation).toHaveBeenCalledTimes(1);
|
||||||
// The link was inserted into the editor.
|
// The link was inserted into the editor.
|
||||||
expect(editor.replaceSelection).toHaveBeenCalledTimes(1);
|
expect(editor.replaceSelection).toHaveBeenCalledTimes(1);
|
||||||
expect(editor.replaceSelection).toHaveBeenCalledWith(
|
expect(editor.replaceSelection).toHaveBeenCalledWith('[[attachments/image.png|image]]');
|
||||||
'[[attachments/image.png|image]]',
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('НЕ вызывает preventDefault, если ни один файл не подходит по расширению', async () => {
|
it('НЕ вызывает preventDefault, если ни один файл не подходит по расширению', async () => {
|
||||||
@@ -214,19 +204,14 @@ describe('AttachmentCleanPasteModule', () => {
|
|||||||
|
|
||||||
const result = await (
|
const result = await (
|
||||||
module as unknown as {
|
module as unknown as {
|
||||||
processFile: (
|
processFile: (f: File, s: { extensions: string }) => Promise<string | null>;
|
||||||
f: File,
|
|
||||||
s: { extensions: string },
|
|
||||||
) => Promise<string | null>;
|
|
||||||
}
|
}
|
||||||
).processFile(file, settings);
|
).processFile(file, settings);
|
||||||
|
|
||||||
expect(result).toBe('[[attachments/image.png|image]]');
|
expect(result).toBe('[[attachments/image.png|image]]');
|
||||||
expect(mockVault.createBinary).toHaveBeenCalledTimes(1);
|
expect(mockVault.createBinary).toHaveBeenCalledTimes(1);
|
||||||
// The binary payload was passed to the vault.
|
// The binary payload was passed to the vault.
|
||||||
const [, payload] = (
|
const [, payload] = mockVault.createBinary.mock.calls[0] as [string, ArrayBuffer];
|
||||||
mockVault.createBinary.mock.calls[0] as [string, ArrayBuffer]
|
|
||||||
);
|
|
||||||
expect(payload).toBeInstanceOf(ArrayBuffer);
|
expect(payload).toBeInstanceOf(ArrayBuffer);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -235,10 +220,7 @@ describe('AttachmentCleanPasteModule', () => {
|
|||||||
|
|
||||||
const result = await (
|
const result = await (
|
||||||
module as unknown as {
|
module as unknown as {
|
||||||
processFile: (
|
processFile: (f: File, s: { extensions: string }) => Promise<string | null>;
|
||||||
f: File,
|
|
||||||
s: { extensions: string },
|
|
||||||
) => Promise<string | null>;
|
|
||||||
}
|
}
|
||||||
).processFile(file, settings);
|
).processFile(file, settings);
|
||||||
|
|
||||||
@@ -256,21 +238,13 @@ describe('AttachmentCleanPasteModule', () => {
|
|||||||
|
|
||||||
describe('onload lifecycle', () => {
|
describe('onload lifecycle', () => {
|
||||||
it('регистрирует оба обработчика (editor-paste и editor-drop) в workspace', () => {
|
it('регистрирует оба обработчика (editor-paste и editor-drop) в workspace', () => {
|
||||||
expect(mockWorkspace.on).toHaveBeenCalledWith(
|
expect(mockWorkspace.on).toHaveBeenCalledWith('editor-paste', expect.any(Function));
|
||||||
'editor-paste',
|
expect(mockWorkspace.on).toHaveBeenCalledWith('editor-drop', expect.any(Function));
|
||||||
expect.any(Function),
|
|
||||||
);
|
|
||||||
expect(mockWorkspace.on).toHaveBeenCalledWith(
|
|
||||||
'editor-drop',
|
|
||||||
expect.any(Function),
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('onunload отключает оба зарегистрированных обработчика', () => {
|
it('onunload отключает оба зарегистрированных обработчика', () => {
|
||||||
module.onunload(
|
module.onunload(
|
||||||
mockPlugin as unknown as Parameters<
|
mockPlugin as unknown as Parameters<AttachmentCleanPasteModule['onunload']>[0],
|
||||||
AttachmentCleanPasteModule['onunload']
|
|
||||||
>[0],
|
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(mockWorkspace.offref).toHaveBeenCalledTimes(2);
|
expect(mockWorkspace.offref).toHaveBeenCalledTimes(2);
|
||||||
|
|||||||
@@ -7,6 +7,21 @@ interface PreserveLinkAliasesSettings {
|
|||||||
processCanvas: boolean;
|
processCanvas: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// `obsidian` is aliased to __mocks__/obsidian.ts at runtime, but tsc types the
|
||||||
|
// import against the real package. These helpers keep the mock's extra surface
|
||||||
|
// visible without weakening the types of the module under test.
|
||||||
|
type VaultMock = Vault & {
|
||||||
|
mdFiles: TFile[];
|
||||||
|
allFiles: TFile[];
|
||||||
|
storage: Map<TFile, string>;
|
||||||
|
readCalls: TFile[];
|
||||||
|
modifyCalls: Array<{ file: TFile; content: string }>;
|
||||||
|
};
|
||||||
|
|
||||||
|
function makeFile(path: string): TFile {
|
||||||
|
return new (TFile as unknown as new (path: string) => TFile)(path);
|
||||||
|
}
|
||||||
|
|
||||||
// TestablePreserveLinkAliasesModule: surface the private members so tests
|
// TestablePreserveLinkAliasesModule: surface the private members so tests
|
||||||
// can invoke processContent / getFilesToProcess / handleRename directly.
|
// can invoke processContent / getFilesToProcess / handleRename directly.
|
||||||
class TestablePreserveLinkAliasesModule extends PreserveLinkAliasesModule {
|
class TestablePreserveLinkAliasesModule extends PreserveLinkAliasesModule {
|
||||||
@@ -17,21 +32,25 @@ class TestablePreserveLinkAliasesModule extends PreserveLinkAliasesModule {
|
|||||||
oldBasename: string,
|
oldBasename: string,
|
||||||
settings: PreserveLinkAliasesSettings,
|
settings: PreserveLinkAliasesSettings,
|
||||||
): string {
|
): string {
|
||||||
return (this as unknown as {
|
return (
|
||||||
processContent: (
|
this as unknown as {
|
||||||
c: string,
|
processContent: (
|
||||||
nf: string,
|
c: string,
|
||||||
nb: string,
|
nf: string,
|
||||||
ob: string,
|
nb: string,
|
||||||
s: PreserveLinkAliasesSettings,
|
ob: string,
|
||||||
) => string;
|
s: PreserveLinkAliasesSettings,
|
||||||
}).processContent(content, newFileName, newBasename, oldBasename, settings);
|
) => string;
|
||||||
|
}
|
||||||
|
).processContent(content, newFileName, newBasename, oldBasename, settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
public callGetFilesToProcess(settings: PreserveLinkAliasesSettings): TFile[] {
|
public callGetFilesToProcess(settings: PreserveLinkAliasesSettings): TFile[] {
|
||||||
return (this as unknown as {
|
return (
|
||||||
getFilesToProcess: (s: PreserveLinkAliasesSettings) => TFile[];
|
this as unknown as {
|
||||||
}).getFilesToProcess(settings);
|
getFilesToProcess: (s: PreserveLinkAliasesSettings) => TFile[];
|
||||||
|
}
|
||||||
|
).getFilesToProcess(settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async callHandleRename(
|
public async callHandleRename(
|
||||||
@@ -39,9 +58,15 @@ class TestablePreserveLinkAliasesModule extends PreserveLinkAliasesModule {
|
|||||||
oldPath: string,
|
oldPath: string,
|
||||||
settings: PreserveLinkAliasesSettings,
|
settings: PreserveLinkAliasesSettings,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
await (this as unknown as {
|
await (
|
||||||
handleRename: (f: TFile, op: string, s: PreserveLinkAliasesSettings) => Promise<void>;
|
this as unknown as {
|
||||||
}).handleRename(file, oldPath, settings);
|
handleRename: (
|
||||||
|
f: TFile,
|
||||||
|
op: string,
|
||||||
|
s: PreserveLinkAliasesSettings,
|
||||||
|
) => Promise<void>;
|
||||||
|
}
|
||||||
|
).handleRename(file, oldPath, settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
public setVault(vault: Vault): void {
|
public setVault(vault: Vault): void {
|
||||||
@@ -57,12 +82,14 @@ function makeModule(vault?: Vault): TestablePreserveLinkAliasesModule {
|
|||||||
return module;
|
return module;
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeVault(opts: {
|
function makeVault(
|
||||||
mdFiles?: TFile[];
|
opts: {
|
||||||
allFiles?: TFile[];
|
mdFiles?: TFile[];
|
||||||
storage?: Map<TFile, string>;
|
allFiles?: TFile[];
|
||||||
} = {}): Vault {
|
storage?: Map<TFile, string>;
|
||||||
const vault = new Vault();
|
} = {},
|
||||||
|
): VaultMock {
|
||||||
|
const vault = new Vault() as VaultMock;
|
||||||
vault.mdFiles = opts.mdFiles ?? [];
|
vault.mdFiles = opts.mdFiles ?? [];
|
||||||
vault.allFiles = opts.allFiles ?? [];
|
vault.allFiles = opts.allFiles ?? [];
|
||||||
if (opts.storage) {
|
if (opts.storage) {
|
||||||
@@ -178,10 +205,10 @@ describe('PreserveLinkAliasesModule', () => {
|
|||||||
// 5. getFilesToProcess включает canvas-файлы при processCanvas=true
|
// 5. getFilesToProcess включает canvas-файлы при processCanvas=true
|
||||||
// 6. getFilesToProcess НЕ включает canvas-файлы при processCanvas=false
|
// 6. getFilesToProcess НЕ включает canvas-файлы при processCanvas=false
|
||||||
describe('getFilesToProcess', () => {
|
describe('getFilesToProcess', () => {
|
||||||
const mdA = new TFile('notes/a.md');
|
const mdA = makeFile('notes/a.md');
|
||||||
const mdB = new TFile('notes/b.md');
|
const mdB = makeFile('notes/b.md');
|
||||||
const canvasX = new TFile('boards/x.canvas');
|
const canvasX = makeFile('boards/x.canvas');
|
||||||
const pdfF = new TFile('papers/f.pdf');
|
const pdfF = makeFile('papers/f.pdf');
|
||||||
|
|
||||||
it('включает canvas-файлы при processCanvas=true', () => {
|
it('включает canvas-файлы при processCanvas=true', () => {
|
||||||
const vault = makeVault({
|
const vault = makeVault({
|
||||||
@@ -217,7 +244,7 @@ describe('PreserveLinkAliasesModule', () => {
|
|||||||
// 7. handleRename не вызывает processContent если oldBasename === newBasename
|
// 7. handleRename не вызывает processContent если oldBasename === newBasename
|
||||||
describe('handleRename', () => {
|
describe('handleRename', () => {
|
||||||
it('не вызывает processContent если oldBasename === newBasename', async () => {
|
it('не вызывает processContent если oldBasename === newBasename', async () => {
|
||||||
const file = new TFile('notes/note.md');
|
const file = makeFile('notes/note.md');
|
||||||
const vault = makeVault({
|
const vault = makeVault({
|
||||||
mdFiles: [file],
|
mdFiles: [file],
|
||||||
storage: new Map([[file, 'See [[other.md|note]] here.']]),
|
storage: new Map([[file, 'See [[other.md|note]] here.']]),
|
||||||
@@ -233,4 +260,4 @@ describe('PreserveLinkAliasesModule', () => {
|
|||||||
expect(vault.modifyCalls).toEqual([]);
|
expect(vault.modifyCalls).toEqual([]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
+1
-1
@@ -10,4 +10,4 @@ if (typeof (globalThis as { document?: unknown }).document === 'undefined') {
|
|||||||
writable: true,
|
writable: true,
|
||||||
configurable: true,
|
configurable: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -12,6 +12,7 @@
|
|||||||
"allowSyntheticDefaultImports": true,
|
"allowSyntheticDefaultImports": true,
|
||||||
"strictNullChecks": true,
|
"strictNullChecks": true,
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
|
"skipLibCheck": true,
|
||||||
"types": ["vitest/globals", "node"]
|
"types": ["vitest/globals", "node"]
|
||||||
},
|
},
|
||||||
"include": ["src/**/*.ts", "tests/**/*.ts", "vitest.config.ts"]
|
"include": ["src/**/*.ts", "tests/**/*.ts", "vitest.config.ts"]
|
||||||
|
|||||||
@@ -15,9 +15,6 @@ export default defineConfig({
|
|||||||
environment: 'node',
|
environment: 'node',
|
||||||
include: ['tests/**/*.test.ts'],
|
include: ['tests/**/*.test.ts'],
|
||||||
setupFiles: ['tests/setup.ts'],
|
setupFiles: ['tests/setup.ts'],
|
||||||
transformMode: {
|
|
||||||
web: [/\.[jt]sx?$/],
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
resolve: {
|
resolve: {
|
||||||
alias: {
|
alias: {
|
||||||
|
|||||||
Reference in new issue
Block a user