Skip to content

Commit

Permalink
refactor: createPodcastNote separation of concerns
Browse files Browse the repository at this point in the history
  • Loading branch information
chhoumann committed Nov 14, 2022
1 parent 2f25ca4 commit abb3e63
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 33 deletions.
68 changes: 35 additions & 33 deletions src/createPodcastNote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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)) {
Expand All @@ -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);
}
17 changes: 17 additions & 0 deletions src/utility/addExtension.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});
5 changes: 5 additions & 0 deletions src/utility/addExtension.ts
Original file line number Diff line number Diff line change
@@ -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}`;
}

0 comments on commit abb3e63

Please sign in to comment.