diff --git a/src/api.ts b/src/api.ts new file mode 100644 index 0000000..5ea8980 --- /dev/null +++ b/src/api.ts @@ -0,0 +1,51 @@ +import { TFile, TFolder } from 'obsidian'; +import type FolderNotesPlugin from './main'; +import { getFolderNote, turnIntoFolderNote } from './functions/folderNoteFunctions'; + +export interface ConvertNoteToFolderNoteOptions { + skipConfirmation?: boolean; +} + +export interface FolderNotesApi { + convertNoteToFolderNote(notePath: string, options?: ConvertNoteToFolderNoteOptions): Promise; +} + +export class FolderNotesApiPathError extends Error { + constructor( + message: string, + readonly notePath: string, + ) { + super(message); + this.name = 'FolderNotesApiPathError'; + } +} + +export class FolderNotesPublicApi implements FolderNotesApi { + constructor(private readonly plugin: FolderNotesPlugin) {} + + async convertNoteToFolderNote( + notePath: string, + options: ConvertNoteToFolderNoteOptions = {}, + ): Promise { + const file = this.plugin.app.vault.getAbstractFileByPath(notePath); + if (!(file instanceof TFile)) { + throw new FolderNotesApiPathError(`No note exists at path: ${notePath}`, notePath); + } + + const folder = file.parent; + if (!(folder instanceof TFolder) || folder.path === '' || folder.path === '/') { + throw new FolderNotesApiPathError(`Note must be inside a folder: ${notePath}`, notePath); + } + + const folderNote = getFolderNote(this.plugin, folder.path); + if (folderNote === file) return; + + await turnIntoFolderNote( + this.plugin, + file, + folder, + folderNote, + options.skipConfirmation ?? true, + ); + } +} diff --git a/src/main.ts b/src/main.ts index 0370f5c..43c38cf 100644 --- a/src/main.ts +++ b/src/main.ts @@ -36,6 +36,7 @@ import { registerOverviewCommands } from './obsidian-folder-overview/src/Command import { updateOverviewView, updateViewDropdown } from './obsidian-folder-overview/src/main'; import { FvIndexDB } from './obsidian-folder-overview/src/utils/IndexDB'; import { updateAllOverviews } from './obsidian-folder-overview/src/utils/functions'; +import { FolderNotesPublicApi, type FolderNotesApi } from './api'; interface FileExplorerPluginLike extends Plugin { revealInFolder: (file: TAbstractFile) => void; @@ -88,6 +89,7 @@ export default class FolderNotesPlugin extends Plugin { settingsOpened = false; askModalCurrentlyOpen = false; fvIndexDB!: FvIndexDB; + api!: FolderNotesApi; async onload(): Promise { console.debug('loading folder notes plugin'); @@ -95,6 +97,7 @@ export default class FolderNotesPlugin extends Plugin { this.settingsTab = new SettingsTab(this.app, this); this.addSettingTab(this.settingsTab); await this.saveSettings(); + this.api = new FolderNotesPublicApi(this); this.fvIndexDB = new FvIndexDB(this); // Add CSS Classes