mirror of
https://github.com/panatgithub/AnkiHeadingSync.git
synced 2026-07-22 06:51:43 +00:00
Introduced two new management commands: 'Clear currently synced cards' to reset sync state for the current file, and 'Cleanup empty decks' to remove unused decks from Anki. Both commands improve plugin maintenance and synchronization flow. 新增两个管理命令:'清空当前文件已同步卡片'用于重置当前文件的同步状态,'清理空牌组'用于从 Anki 中移除未使用的牌组。这两个命令改进了插件的维护和同步流程。
66 lines
No EOL
2 KiB
TypeScript
66 lines
No EOL
2 KiB
TypeScript
import { MarkdownWriteConflictError } from "@/application/ports/VaultGateway";
|
|
import type { ManualSyncVaultGateway } from "@/application/ports/ManualSyncVaultGateway";
|
|
import { CardMarkerRemovalService, type MarkerRemovalTarget } from "@/domain/manual-sync/services/CardMarkerRemovalService";
|
|
|
|
export interface MarkdownMarkerRemovalResult {
|
|
removedMarkers: number;
|
|
conflictFiles: string[];
|
|
failureFiles: Array<{ filePath: string; message: string }>;
|
|
}
|
|
|
|
export class MarkdownMarkerRemovalService {
|
|
constructor(
|
|
private readonly vaultGateway: ManualSyncVaultGateway,
|
|
private readonly markerRemovalService = new CardMarkerRemovalService(),
|
|
) {}
|
|
|
|
async remove(filePath: string, targets: MarkerRemovalTarget[]): Promise<MarkdownMarkerRemovalResult> {
|
|
if (targets.length === 0) {
|
|
return {
|
|
removedMarkers: 0,
|
|
conflictFiles: [],
|
|
failureFiles: [],
|
|
};
|
|
}
|
|
|
|
const sourceFile = await this.vaultGateway.readMarkdownFile(filePath);
|
|
if (!sourceFile) {
|
|
return {
|
|
removedMarkers: 0,
|
|
conflictFiles: [],
|
|
failureFiles: [{ filePath, message: `Markdown file not found: ${filePath}` }],
|
|
};
|
|
}
|
|
|
|
const removal = this.markerRemovalService.apply(sourceFile.content, targets);
|
|
|
|
try {
|
|
if (removal.nextContent !== sourceFile.content) {
|
|
await this.vaultGateway.replaceMarkdownFile(filePath, sourceFile.content, removal.nextContent);
|
|
}
|
|
|
|
return {
|
|
removedMarkers: removal.removedMarkers,
|
|
conflictFiles: [],
|
|
failureFiles: [],
|
|
};
|
|
} catch (error) {
|
|
if (error instanceof MarkdownWriteConflictError) {
|
|
return {
|
|
removedMarkers: 0,
|
|
conflictFiles: [filePath],
|
|
failureFiles: [],
|
|
};
|
|
}
|
|
|
|
return {
|
|
removedMarkers: 0,
|
|
conflictFiles: [],
|
|
failureFiles: [{
|
|
filePath,
|
|
message: error instanceof Error ? error.message : String(error),
|
|
}],
|
|
};
|
|
}
|
|
}
|
|
} |