feat: throw FolderNotesApiAlreadyFolderNoteError if note is already a folder note

This commit is contained in:
Moy 2026-07-03 20:36:12 +08:00
parent b6d947b248
commit 89bae42d0b

View file

@ -39,6 +39,8 @@ export interface FolderNotesApi {
* Converts an existing note into the folder note for its parent folder.
* Throws FolderNotesApiPathError if the path does not point to a note
* inside a non-root folder.
* Throws FolderNotesApiAlreadyFolderNoteError if the note is already
* a folder note (e.g. aa/aa.md).
*/
convertNoteToFolderNote(notePath: string, options?: ConvertNoteToFolderNoteOptions): Promise<void>;
}
@ -53,6 +55,13 @@ export class FolderNotesApiPathError extends Error {
}
}
export class FolderNotesApiAlreadyFolderNoteError extends Error {
constructor(readonly notePath: string) {
super(`Note is already a folder note: ${notePath}`);
this.name = 'FolderNotesApiAlreadyFolderNoteError';
}
}
export function getApi(plugin: FolderNotesPlugin): FolderNotesApi {
return {
version: API_VERSION,
@ -85,6 +94,11 @@ async function convertNoteToFolderNote(
throw new FolderNotesApiPathError(`Note must be inside a non-root folder: ${notePath}`, notePath);
}
// Guard: already a folder note (e.g. aa/aa.md)
if (getFolderNote(plugin, parentFolder.path) === file) {
throw new FolderNotesApiAlreadyFolderNoteError(notePath);
}
// Build the new folder path: sibling folder named after the note's basename
const newFolderPath = parentFolder.path === ''
? file.basename