Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions scripts/test-hinote-rename-fix.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { readFileSync } from "node:fs";
import assert from "node:assert/strict";

const bootstrap = readFileSync("src/plugin/PluginBootstrap.ts", "utf8");
const dataManager = readFileSync("src/storage/HiNoteDataManager.ts", "utf8");

assert.match(
bootstrap,
/const services = await plugin\.ensureServicesInitialized\(\);[\s\S]*await services\.highlightManager\.handleFileRename\(oldPath, file\.path\)/,
"rename handler should initialize HiNote services before migrating highlights",
);

assert.doesNotMatch(
bootstrap,
/const services = plugin\.services;[\s\S]*if \(services\)/,
"rename handler should not skip migration when services are still lazy",
);

assert.match(
dataManager,
/const newSafeFileName = FilePathUtils\.toSafeFileName\(newPath\);[\s\S]*const newStoragePath = `\$\{FilePathUtils\.getHighlightsDir\(this\.vaultPath\)\}\/\$\{newSafeFileName\}`;/,
"highlight migration should compute the target storage path without creating a stale mapping first",
);

assert.match(
dataManager,
/this\.fileMappingStore\.set\(newPath, newSafeFileName\);/,
"highlight migration should persist the new path mapping after the file data is written",
);

assert.doesNotMatch(
dataManager,
/const newStoragePath = this\.getStoragePathForFile\(newPath\);/,
"highlight migration should not call getStoragePathForFile(newPath) before the old data is read",
);
6 changes: 4 additions & 2 deletions src/plugin/PluginBootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ export function registerPluginCommands(plugin: CommentPlugin, windowManager: Win
export function registerPluginVaultEvents(plugin: CommentPlugin): void {
plugin.registerEvent(
plugin.app.vault.on('rename', async (file, oldPath) => {
const services = plugin.services;
if (services) {
try {
const services = await plugin.ensureServicesInitialized();
await services.highlightManager.handleFileRename(oldPath, file.path);
} catch (error) {
console.error('[HiNote] Failed to migrate highlights after file rename:', error);
}
})
);
Expand Down
13 changes: 10 additions & 3 deletions src/storage/HiNoteDataManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ensureHiNoteDirectoryStructure
} from './HiNoteStorageLayout';
import { FlashcardDataStore } from './FlashcardDataStore';
import { FilePathUtils } from './FilePathUtils';

/**
* HiNote数据管理器 - 存储层(已重构)
Expand Down Expand Up @@ -152,7 +153,8 @@ export class HiNoteDataManager {
*/
async handleFileRename(oldPath: string, newPath: string): Promise<void> {
const oldStoragePath = this.getStoragePathForFile(oldPath);
const newStoragePath = this.getStoragePathForFile(newPath);
const newSafeFileName = FilePathUtils.toSafeFileName(newPath);
const newStoragePath = `${FilePathUtils.getHighlightsDir(this.vaultPath)}/${newSafeFileName}`;

try {
// 检查旧文件是否存在
Expand All @@ -162,13 +164,18 @@ export class HiNoteDataManager {
await this.app.vault.adapter.write(newStoragePath, content);

// 删除旧文件
await this.app.vault.adapter.remove(oldStoragePath);
if (oldStoragePath !== newStoragePath) {
await this.app.vault.adapter.remove(oldStoragePath);
}

// 更新映射
this.fileMappingStore.delete(oldPath);
this.fileMappingStore.set(newPath, newSafeFileName);
await this.saveFileMapping();
} catch {
// 旧文件可能不存在,忽略错误
// 旧文件可能不存在,确保旧路径不会继续指向不存在的数据
this.fileMappingStore.delete(oldPath);
await this.saveFileMapping();
}
}

Expand Down