Skip to content

Commit e27067f

Browse files
committed
style: format code
1 parent cb7f206 commit e27067f

File tree

7 files changed

+443
-417
lines changed

7 files changed

+443
-417
lines changed

bun.lockb

-294 KB
Binary file not shown.

src/TemplateEngine.ts

Lines changed: 49 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { htmlToMarkdown, Notice } from "obsidian";
2-
import { Episode } from "src/types/Episode";
2+
import type { Episode } from "src/types/Episode";
33
import Fuse from "fuse.js";
44
import { plugin } from "src/store";
55
import { get } from "svelte/store";
@@ -11,7 +11,7 @@ interface Tags {
1111

1212
type AddTagFn = (
1313
tag: Lowercase<string>,
14-
value: string | ((...args: unknown[]) => string)
14+
value: string | ((...args: unknown[]) => string),
1515
) => void;
1616
type ReplacerFn = (template: string) => string;
1717

@@ -20,7 +20,7 @@ function useTemplateEngine(): Readonly<[ReplacerFn, AddTagFn]> {
2020

2121
function addTag(
2222
tag: Lowercase<string>,
23-
value: string | ((...args: unknown[]) => string)
23+
value: string | ((...args: unknown[]) => string),
2424
): void {
2525
tags[tag] = value;
2626
}
@@ -45,7 +45,7 @@ function useTemplateEngine(): Readonly<[ReplacerFn, AddTagFn]> {
4545
similarTag.length > 0
4646
? ` Did you mean ${similarTag[0].item}?`
4747
: ""
48-
}`
48+
}`,
4949
);
5050
return match;
5151
}
@@ -54,9 +54,7 @@ function useTemplateEngine(): Readonly<[ReplacerFn, AddTagFn]> {
5454
if (params) {
5555
// Remove initial colon with splice.
5656
const splitParams = params.slice(1).split(",");
57-
const args = Array.isArray(splitParams)
58-
? splitParams
59-
: [params];
57+
const args = Array.isArray(splitParams) ? splitParams : [params];
6058

6159
return tagValue(...args);
6260
}
@@ -65,7 +63,7 @@ function useTemplateEngine(): Readonly<[ReplacerFn, AddTagFn]> {
6563
}
6664

6765
return tagValue;
68-
}
66+
},
6967
);
7068
}
7169

@@ -96,15 +94,12 @@ export function NoteTemplateEngine(template: string, episode: Episode) {
9694

9795
return htmlToMarkdown(episode.content);
9896
});
99-
addTag(
100-
"safetitle",
101-
replaceIllegalFileNameCharactersInString(episode.title)
102-
);
97+
addTag("safetitle", replaceIllegalFileNameCharactersInString(episode.title));
10398
addTag("url", episode.url);
10499
addTag("date", (format?: string) =>
105100
episode.episodeDate
106101
? window.moment(episode.episodeDate).format(format ?? "YYYY-MM-DD")
107-
: ""
102+
: "",
108103
);
109104
addTag("podcast", episode.podcastName);
110105
addTag("artwork", episode.artworkUrl ?? "");
@@ -116,10 +111,10 @@ export function TimestampTemplateEngine(template: string) {
116111
const [replacer, addTag] = useTemplateEngine();
117112

118113
addTag("time", (format?: string) =>
119-
get(plugin).api.getPodcastTimeFormatted(format ?? "HH:mm:ss")
114+
get(plugin).api.getPodcastTimeFormatted(format ?? "HH:mm:ss"),
120115
);
121116
addTag("linktime", (format?: string) =>
122-
get(plugin).api.getPodcastTimeFormatted(format ?? "HH:mm:ss", true)
117+
get(plugin).api.getPodcastTimeFormatted(format ?? "HH:mm:ss", true),
123118
);
124119

125120
return replacer(template);
@@ -129,9 +124,7 @@ export function FilePathTemplateEngine(template: string, episode: Episode) {
129124
const [replacer, addTag] = useTemplateEngine();
130125

131126
addTag("title", (whitespaceReplacement?: string) => {
132-
const legalTitle = replaceIllegalFileNameCharactersInString(
133-
episode.title
134-
);
127+
const legalTitle = replaceIllegalFileNameCharactersInString(episode.title);
135128
if (whitespaceReplacement) {
136129
return legalTitle.replace(/\s+/g, whitespaceReplacement);
137130
}
@@ -140,7 +133,7 @@ export function FilePathTemplateEngine(template: string, episode: Episode) {
140133
});
141134
addTag("podcast", (whitespaceReplacement?: string) => {
142135
const legalName = replaceIllegalFileNameCharactersInString(
143-
episode.podcastName
136+
episode.podcastName,
144137
);
145138
if (whitespaceReplacement) {
146139
return legalName.replace(/\s+/g, whitespaceReplacement);
@@ -151,7 +144,7 @@ export function FilePathTemplateEngine(template: string, episode: Episode) {
151144
addTag("date", (format?: string) =>
152145
episode.episodeDate
153146
? window.moment(episode.episodeDate).format(format ?? "YYYY-MM-DD")
154-
: ""
147+
: "",
155148
);
156149

157150
return replacer(template);
@@ -167,9 +160,7 @@ export function DownloadPathTemplateEngine(template: string, episode: Episode) {
167160
const [replacer, addTag] = useTemplateEngine();
168161

169162
addTag("title", (whitespaceReplacement?: string) => {
170-
const legalTitle = replaceIllegalFileNameCharactersInString(
171-
episode.title
172-
);
163+
const legalTitle = replaceIllegalFileNameCharactersInString(episode.title);
173164
if (whitespaceReplacement) {
174165
return legalTitle.replace(/\s+/g, whitespaceReplacement);
175166
}
@@ -178,7 +169,7 @@ export function DownloadPathTemplateEngine(template: string, episode: Episode) {
178169
});
179170
addTag("podcast", (whitespaceReplacement?: string) => {
180171
const legalName = replaceIllegalFileNameCharactersInString(
181-
episode.podcastName
172+
episode.podcastName,
182173
);
183174
if (whitespaceReplacement) {
184175
return legalName.replace(/\s+/g, whitespaceReplacement);
@@ -189,35 +180,41 @@ export function DownloadPathTemplateEngine(template: string, episode: Episode) {
189180
addTag("date", (format?: string) =>
190181
episode.episodeDate
191182
? window.moment(episode.episodeDate).format(format ?? "YYYY-MM-DD")
192-
: ""
183+
: "",
193184
);
194185

195186
return replacer(templateWithoutExtension);
196187
}
197188

198-
export function TranscriptTemplateEngine(template: string, episode: Episode, transcription: string) {
199-
const [replacer, addTag] = useTemplateEngine();
200-
201-
addTag("title", (whitespaceReplacement?: string) => {
202-
const legalTitle = replaceIllegalFileNameCharactersInString(episode.title);
203-
if (whitespaceReplacement) {
204-
return legalTitle.replace(/\s+/g, whitespaceReplacement);
205-
}
206-
return legalTitle;
207-
});
208-
addTag("podcast", (whitespaceReplacement?: string) => {
209-
const legalName = replaceIllegalFileNameCharactersInString(episode.podcastName);
210-
if (whitespaceReplacement) {
211-
return legalName.replace(/\s+/g, whitespaceReplacement);
212-
}
213-
return legalName;
214-
});
215-
addTag("date", (format?: string) =>
216-
episode.episodeDate
217-
? window.moment(episode.episodeDate).format(format ?? "YYYY-MM-DD")
218-
: ""
219-
);
220-
addTag("transcript", transcription);
189+
export function TranscriptTemplateEngine(
190+
template: string,
191+
episode: Episode,
192+
transcription: string,
193+
) {
194+
const [replacer, addTag] = useTemplateEngine();
195+
196+
addTag("title", (whitespaceReplacement?: string) => {
197+
const legalTitle = replaceIllegalFileNameCharactersInString(episode.title);
198+
if (whitespaceReplacement) {
199+
return legalTitle.replace(/\s+/g, whitespaceReplacement);
200+
}
201+
return legalTitle;
202+
});
203+
addTag("podcast", (whitespaceReplacement?: string) => {
204+
const legalName = replaceIllegalFileNameCharactersInString(
205+
episode.podcastName,
206+
);
207+
if (whitespaceReplacement) {
208+
return legalName.replace(/\s+/g, whitespaceReplacement);
209+
}
210+
return legalName;
211+
});
212+
addTag("date", (format?: string) =>
213+
episode.episodeDate
214+
? window.moment(episode.episodeDate).format(format ?? "YYYY-MM-DD")
215+
: "",
216+
);
217+
addTag("transcript", transcription);
221218
addTag("description", (prependToLines?: string) => {
222219
if (prependToLines) {
223220
return htmlToMarkdown(episode.description)
@@ -228,15 +225,15 @@ export function TranscriptTemplateEngine(template: string, episode: Episode, tra
228225

229226
return htmlToMarkdown(episode.description);
230227
});
231-
addTag("url", episode.url);
232-
addTag("artwork", episode.artworkUrl ?? "");
228+
addTag("url", episode.url);
229+
addTag("artwork", episode.artworkUrl ?? "");
233230

234-
return replacer(template);
231+
return replacer(template);
235232
}
236233

237234
function replaceIllegalFileNameCharactersInString(string: string) {
238235
return string
239236
.replace(/[\\,#%&{}/*<>$'":@\u2023|?]*/g, "") // Replace illegal file name characters with empty string
240237
.replace(/\n/, " ") // replace newlines with spaces
241238
.replace(" ", " "); // replace multiple spaces with single space to make sure we don't have double spaces in the file name
242-
}
239+
}

src/downloadEpisode.ts

Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { Notice, TFile, requestUrl } from "obsidian";
22
import { downloadedEpisodes } from "./store";
33
import { DownloadPathTemplateEngine } from "./TemplateEngine";
4-
import { Episode } from "./types/Episode";
4+
import type { Episode } from "./types/Episode";
55
import getUrlExtension from "./utility/getUrlExtension";
66

77
async function downloadFile(
88
url: string,
99
options?: Partial<{
1010
onFinished: () => void;
1111
onError: (error: Error) => void;
12-
}>
12+
}>,
1313
) {
1414
try {
1515
const response = await requestUrl({ url, method: "GET" });
@@ -43,7 +43,7 @@ async function downloadFile(
4343

4444
export default async function downloadEpisodeWithNotice(
4545
episode: Episode,
46-
downloadPathTemplate: string
46+
downloadPathTemplate: string,
4747
): Promise<void> {
4848
const { doc, update } = createNoticeDoc(`Download "${episode.title}"`);
4949
const SOME_LARGE_INT_SO_THE_BOX_DOESNT_AUTO_CLOSE = 999999999;
@@ -57,15 +57,13 @@ export default async function downloadEpisodeWithNotice(
5757

5858
const { blob } = await downloadFile(episode.streamUrl, {
5959
onFinished: () => {
60-
update((bodyEl) =>
61-
bodyEl.createEl("p", { text: "Download complete!" })
62-
);
60+
update((bodyEl) => bodyEl.createEl("p", { text: "Download complete!" }));
6361
},
6462
onError: (error) => {
6563
update((bodyEl) =>
6664
bodyEl.createEl("p", {
6765
text: `Download failed: ${error.message}`,
68-
})
66+
}),
6967
);
7068
},
7169
});
@@ -104,7 +102,7 @@ export default async function downloadEpisodeWithNotice(
104102
update((bodyEl) =>
105103
bodyEl.createEl("p", {
106104
text: `Successfully downloaded "${episode.title}" from ${episode.podcastName}.`,
107-
})
105+
}),
108106
);
109107
} catch (error) {
110108
update((bodyEl) => {
@@ -173,56 +171,54 @@ async function createEpisodeFile({
173171

174172
export async function downloadEpisode(
175173
episode: Episode,
176-
downloadPathTemplate: string
174+
downloadPathTemplate: string,
177175
): Promise<string> {
178-
const basename = DownloadPathTemplateEngine(downloadPathTemplate, episode);
179-
const fileExtension = await getFileExtension(episode.streamUrl);
180-
const filePath = `${basename}.${fileExtension}`;
181-
182-
// Check if the file already exists
183-
const existingFile = app.vault.getAbstractFileByPath(filePath);
184-
if (existingFile instanceof TFile) {
185-
return filePath; // Return the existing file path
186-
}
187-
188-
try {
189-
const { blob, responseUrl } = await downloadFile(episode.streamUrl);
190-
191-
if (!blob.type.includes("audio") && !fileExtension) {
192-
throw new Error("Not an audio file.");
193-
}
194-
195-
await createEpisodeFile({
196-
episode,
197-
downloadPathTemplate,
198-
blob,
199-
extension: fileExtension,
200-
});
201-
202-
return filePath;
203-
} catch (error) {
204-
throw new Error(
205-
`Failed to download ${episode.title}: ${error.message}`
206-
);
207-
}
176+
const basename = DownloadPathTemplateEngine(downloadPathTemplate, episode);
177+
const fileExtension = await getFileExtension(episode.streamUrl);
178+
const filePath = `${basename}.${fileExtension}`;
179+
180+
// Check if the file already exists
181+
const existingFile = app.vault.getAbstractFileByPath(filePath);
182+
if (existingFile instanceof TFile) {
183+
return filePath; // Return the existing file path
184+
}
185+
186+
try {
187+
const { blob, responseUrl } = await downloadFile(episode.streamUrl);
188+
189+
if (!blob.type.includes("audio") && !fileExtension) {
190+
throw new Error("Not an audio file.");
191+
}
192+
193+
await createEpisodeFile({
194+
episode,
195+
downloadPathTemplate,
196+
blob,
197+
extension: fileExtension,
198+
});
199+
200+
return filePath;
201+
} catch (error) {
202+
throw new Error(`Failed to download ${episode.title}: ${error.message}`);
203+
}
208204
}
209205

210206
async function getFileExtension(url: string): Promise<string> {
211-
const urlExtension = getUrlExtension(url);
212-
if (urlExtension) return urlExtension;
213-
214-
// If URL doesn't have an extension, fetch headers to determine content type
215-
const response = await fetch(url, { method: 'HEAD' });
216-
const contentType = response.headers.get('content-type');
217-
218-
if (contentType?.includes('audio/mpeg')) return 'mp3';
219-
if (contentType?.includes('audio/mp4')) return 'm4a';
220-
if (contentType?.includes('audio/ogg')) return 'ogg';
221-
if (contentType?.includes('audio/wav')) return 'wav';
222-
if (contentType?.includes('audio/x-m4a')) return 'm4a';
223-
224-
// Default to mp3 if we can't determine the type
225-
return 'mp3';
207+
const urlExtension = getUrlExtension(url);
208+
if (urlExtension) return urlExtension;
209+
210+
// If URL doesn't have an extension, fetch headers to determine content type
211+
const response = await fetch(url, { method: "HEAD" });
212+
const contentType = response.headers.get("content-type");
213+
214+
if (contentType?.includes("audio/mpeg")) return "mp3";
215+
if (contentType?.includes("audio/mp4")) return "m4a";
216+
if (contentType?.includes("audio/ogg")) return "ogg";
217+
if (contentType?.includes("audio/wav")) return "wav";
218+
if (contentType?.includes("audio/x-m4a")) return "m4a";
219+
220+
// Default to mp3 if we can't determine the type
221+
return "mp3";
226222
}
227223

228224
interface AudioSignature {
@@ -231,7 +227,9 @@ interface AudioSignature {
231227
fileExtension: string;
232228
}
233229

234-
export async function detectAudioFileExtension(blob: Blob): Promise<string | null> {
230+
export async function detectAudioFileExtension(
231+
blob: Blob,
232+
): Promise<string | null> {
235233
const audioSignatures: AudioSignature[] = [
236234
{ signature: [0xff, 0xe0], mask: [0xff, 0xe0], fileExtension: "mp3" },
237235
{ signature: [0x49, 0x44, 0x33], fileExtension: "mp3" },
@@ -289,8 +287,8 @@ export async function detectAudioFileExtension(blob: Blob): Promise<string | nul
289287
fileReader.readAsArrayBuffer(
290288
blob.slice(
291289
0,
292-
Math.max(...audioSignatures.map((sig) => sig.signature.length))
293-
)
290+
Math.max(...audioSignatures.map((sig) => sig.signature.length)),
291+
),
294292
);
295293
});
296-
}
294+
}

0 commit comments

Comments
 (0)