From abb3e63fba344fd97c317475228c322f0f78197e Mon Sep 17 00:00:00 2001 From: Christian Bager Bach Houmann <christian@bagerbach.com> Date: Mon, 14 Nov 2022 11:24:25 +0100 Subject: [PATCH] refactor: createPodcastNote separation of concerns --- src/createPodcastNote.ts | 68 ++++++++++++++++---------------- src/utility/addExtension.test.ts | 17 ++++++++ src/utility/addExtension.ts | 5 +++ 3 files changed, 57 insertions(+), 33 deletions(-) create mode 100644 src/utility/addExtension.test.ts create mode 100644 src/utility/addExtension.ts diff --git a/src/createPodcastNote.ts b/src/createPodcastNote.ts index b600cd8..0d417df 100644 --- a/src/createPodcastNote.ts +++ b/src/createPodcastNote.ts @@ -3,6 +3,7 @@ import { FilePathTemplateEngine, NoteTemplateEngine } from "./TemplateEngine"; import { Episode } from "./types/Episode"; import { get } from "svelte/store"; import { plugin } from "./store"; +import addExtension from "./utility/addExtension"; export default async function createPodcastNote( episode: Episode @@ -14,43 +15,19 @@ export default async function createPodcastNote( episode ); - const filePathDotMd = filePath.endsWith(".md") - ? filePath - : `${filePath}.md`; + const filePathDotMd = addExtension(filePath, "md"); const content = NoteTemplateEngine( pluginInstance.settings.note.template, 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; - } - - 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); - - if (!folder) { - await app.vault.createFolder(folderPath); - } - } - - return await app.vault.create(path, content); - }; - try { - const file = await createOrGetFile(filePathDotMd, content); + const file = await createFileIfNotExists( + filePathDotMd, + content, + episode + ); app.workspace.getLeaf().openFile(file); } catch (error) { @@ -67,9 +44,7 @@ export function getPodcastNote(episode: Episode): TFile | null { episode ); - const filePathDotMd = filePath.endsWith(".md") - ? filePath - : `${filePath}.md`; + const filePathDotMd = addExtension(filePath, "md"); const file = app.vault.getAbstractFileByPath(filePathDotMd); if (!file || !(file instanceof TFile)) { @@ -89,3 +64,30 @@ export function openPodcastNote(epiosode: Episode): void { app.workspace.getLeaf().openFile(file); } + +async function createFileIfNotExists( + path: string, + content: string, + episode: Episode, + createFolder = true +): Promise<TFile> { + const file = getPodcastNote(episode); + + if (file) { + new Notice(`Note for "${episode.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); + + if (!folder && createFolder) { + await app.vault.createFolder(folderPath); + } + } + + return await app.vault.create(path, content); +} diff --git a/src/utility/addExtension.test.ts b/src/utility/addExtension.test.ts new file mode 100644 index 0000000..1699b12 --- /dev/null +++ b/src/utility/addExtension.test.ts @@ -0,0 +1,17 @@ +import { expect, describe, test } from "vitest"; +import addExtension from "./addExtension"; + +describe("addExtension", () => { + test("adds an extension to a file path", () => { + expect(addExtension("path/to/file", "md")).toBe("path/to/file.md"); + }); + + test("does not add an extension if the file path already has one", () => { + expect(addExtension("path/to/file.md", "md")).toBe("path/to/file.md"); + }); + + test("works with and without dot in extension", () => { + expect(addExtension("path/to/file", ".md")).toBe("path/to/file.md"); + expect(addExtension("path/to/file.md", "md")).toBe("path/to/file.md"); + }); +}); \ No newline at end of file diff --git a/src/utility/addExtension.ts b/src/utility/addExtension.ts new file mode 100644 index 0000000..67b2c2a --- /dev/null +++ b/src/utility/addExtension.ts @@ -0,0 +1,5 @@ +export default function addExtension(path: string, extension: string): string { + const ext = extension.startsWith(".") ? extension : `.${extension}`; + + return path.endsWith(ext) ? path : `${path}${ext}`; +} -- GitLab