From ec263d45e914fa84a117717b2541712895e883a3 Mon Sep 17 00:00:00 2001
From: Christian Bager Bach Houmann <christian@bagerbach.com>
Date: Tue, 8 Nov 2022 20:42:11 +0100
Subject: [PATCH] feat: create folders necessary for file creation if they
 don't exist

for #49. When creating a podcast note, syntax like {{podcast}}/{{title}}.md is now possible

add #49
---
 src/createPodcastNote.ts | 52 ++++++++++++++++++++++++++--------------
 1 file changed, 34 insertions(+), 18 deletions(-)

diff --git a/src/createPodcastNote.ts b/src/createPodcastNote.ts
index 8c83648..b600cd8 100644
--- a/src/createPodcastNote.ts
+++ b/src/createPodcastNote.ts
@@ -4,7 +4,9 @@ import { Episode } from "./types/Episode";
 import { get } from "svelte/store";
 import { plugin } from "./store";
 
-export default async function createPodcastNote(episode: Episode): Promise<void> {
+export default async function createPodcastNote(
+	episode: Episode
+): Promise<void> {
 	const pluginInstance = get(plugin);
 
 	const filePath = FilePathTemplateEngine(
@@ -12,31 +14,45 @@ export default async function createPodcastNote(episode: Episode): Promise<void>
 		episode
 	);
 
-	const filePathDotMd = filePath.endsWith('.md') ? filePath : `${filePath}.md`;
+	const filePathDotMd = filePath.endsWith(".md")
+		? filePath
+		: `${filePath}.md`;
 
 	const content = NoteTemplateEngine(
 		pluginInstance.settings.note.template,
-		episode,
+		episode
 	);
 
-	const createOrGetFile: (path: string, content: string) => Promise<TFile>
-		= async (path: string, content: string) => {
-			const file = getPodcastNote(episode);
+	const createOrGetFile: (
+		path: string,
+		content: string
+	) => Promise<TFile> = async (path: string, content: string) => {
+		const file = getPodcastNote(episode);
+
+		if (file) {
+			new Notice(
+				`Note for "${pluginInstance.api.podcast.title}" already exists`
+			);
+			return file;
+		}
 
-			if (file) {
-				new Notice(`Note for "${pluginInstance.api.podcast.title}" already exists`);
-				return file;
-			}
+		const foldersInPath = path.split("/").slice(0, -1);
+		for (let i = 0; i < foldersInPath.length; i++) {
+			const folderPath = foldersInPath.slice(0, i + 1).join("/");
+			const folder = app.vault.getAbstractFileByPath(folderPath);
 
-			return await app.vault.create(path, content);
+			if (!folder) {
+				await app.vault.createFolder(folderPath);
+			}
 		}
 
+		return await app.vault.create(path, content);
+	};
+
 	try {
 		const file = await createOrGetFile(filePathDotMd, content);
 
-		app.workspace
-			.getLeaf()
-			.openFile(file)
+		app.workspace.getLeaf().openFile(file);
 	} catch (error) {
 		console.error(error);
 		new Notice(`Failed to create note: "${filePathDotMd}"`);
@@ -51,7 +67,9 @@ export function getPodcastNote(episode: Episode): TFile | null {
 		episode
 	);
 
-	const filePathDotMd = filePath.endsWith('.md') ? filePath : `${filePath}.md`;
+	const filePathDotMd = filePath.endsWith(".md")
+		? filePath
+		: `${filePath}.md`;
 	const file = app.vault.getAbstractFileByPath(filePathDotMd);
 
 	if (!file || !(file instanceof TFile)) {
@@ -69,7 +87,5 @@ export function openPodcastNote(epiosode: Episode): void {
 		return;
 	}
 
-	app.workspace
-		.getLeaf()
-		.openFile(file);
+	app.workspace.getLeaf().openFile(file);
 }
-- 
GitLab