Skip to content

Commit

Permalink
Handle missing scene folder (kevboh#195)
Browse files Browse the repository at this point in the history
* Default to no scene files if folder doesn't exist

* Create note with template and parent folder

* Refactor createNote functionality
  • Loading branch information
b-camphart authored Sep 24, 2023
1 parent 00bbdb2 commit 5ff6955
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
35 changes: 33 additions & 2 deletions src/model/note-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export async function createNoteWithPotentialTemplate(
path: string,
template: string | null
): Promise<void> {
const file = await app.vault.create(path, "");
if (template) {
const file = await createNote(path);
if (template && file) {
let contents = "";
let pluginUsed = "";
try {
Expand All @@ -38,6 +38,37 @@ export async function createNoteWithPotentialTemplate(
}
}

/**
* Creates a note at `path` with the given `initialContent`.
* @param path
* @param initialContent
* @returns `null` if it fails to create the note. `TFile` for the new note, if successful.
*/
export async function createNote(path: string, initialContent: string = ""): Promise<TFile | null> {
const pathComponents = path.split("/");
pathComponents.pop();

if (!(await app.vault.adapter.exists(pathComponents.join("/")))) {
try {
await app.vault.createFolder(pathComponents.join("/"));
} catch (e) {
console.error(`[Longform] Failed to create new note at "${path}"`, e)
return null;
}
}

try {
// as of obsidian 1.4.4, vault.create will successfully create a file, and
// its parent folder, but will throw an error anyway, if the parent folder
// didn't initially exist. By creating the parent folder above, we avoid
// that situation. This may change in later versions of obsidian.
return await app.vault.create(path, initialContent);
} catch(e: unknown) {
console.error(`[Longform] Failed to create new note at "${path}"`, e)
return null;
}
}

function isTemplaterEnabled(): boolean {
return !!(app as any).plugins.getPlugin("templater-obsidian");
}
Expand Down
14 changes: 9 additions & 5 deletions src/model/store-vault-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,15 @@ export class StoreVaultSync {
`${fileWithMetadata.file.parent.path}/${sceneFolder}`
);

const filenamesInSceneFolder = (
await this.vault.adapter.list(normalizedSceneFolder)
).files
.filter((f) => f !== fileWithMetadata.file.path && f.endsWith(".md"))
.map((f) => this.vault.getAbstractFileByPath(f).name.slice(0, -3));
let filenamesInSceneFolder: string[] = [];
if (await this.vault.adapter.exists(normalizedSceneFolder)) {
filenamesInSceneFolder = (
await this.vault.adapter.list(normalizedSceneFolder)
).files
.filter((f) => f !== fileWithMetadata.file.path && f.endsWith(".md"))
.map((f) => this.vault.getAbstractFileByPath(f)?.name.slice(0, -3))
.filter(maybeName => maybeName !== null && maybeName !== undefined) as string[];
}

// Filter removed scenes
const knownScenes = scenes.filter(({ title }) =>
Expand Down

0 comments on commit 5ff6955

Please sign in to comment.