Skip to content

Commit

Permalink
add a command which fixes #13 so you can use slash command or the com…
Browse files Browse the repository at this point in the history
…mand bar to insert todays voicenotes into editor
  • Loading branch information
kinabalu committed Jun 8, 2024
1 parent e5c7739 commit 9855e5f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
42 changes: 39 additions & 3 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import {
Setting,
PluginManifest,
DataAdapter,
normalizePath, TFile,
normalizePath, TFile, Editor,
} from 'obsidian';
import {moment} from "obsidian"
import VoiceNotesApi from "./voicenotes-api";
import {capitalizeFirstLetter, isAlphaNumeric} from "./utils";
import {capitalizeFirstLetter, isToday} from "./utils";
import {VoiceNoteEmail} from "./types";
import { sanitize } from 'sanitize-filename-ts';
import * as path from 'path';

declare global {
interface Window {
Expand Down Expand Up @@ -64,6 +63,28 @@ export default class VoiceNotesPlugin extends Plugin {
await this.loadSettings();
this.addSettingTab(new VoiceNotesSettingTab(this.app, this));


this.addCommand({
id: 'insert-voicenotes-from-today',
name: 'Insert Today\'s Voicenotes',
editorCallback: async (editor: Editor) => {
if (!this.settings.token) {
new Notice('No access available, please login in plugin settings')
return
}

const todaysRecordings = await this.getTodaysSyncedRecordings()

if (todaysRecordings.length === 0) {
new Notice("No recordings from today found")
return
}

let listOfToday = todaysRecordings.map(filename => `- [[${filename}]]`).join('\n')
editor.replaceSelection(listOfToday)
}
});

this.registerEvent(this.app.metadataCache.on("deleted", (deletedFile, prevCache) => {
if (prevCache.frontmatter?.recording_id) {
this.syncedRecordingIds.remove(prevCache.frontmatter?.recording_id);
Expand All @@ -75,6 +96,7 @@ export default class VoiceNotesPlugin extends Plugin {
}

onunload() {
this.syncedRecordingIds = []
window.clearInterval(this.syncInterval)
}

Expand All @@ -90,6 +112,10 @@ export default class VoiceNotesPlugin extends Plugin {
return this.app.metadataCache.getFileCache(file)?.frontmatter?.['recording_id'];
}

async isRecordingFromToday(file: TFile) : Promise<boolean> {
return isToday(await this.app.metadataCache.getFileCache(file)?.frontmatter?.['created_at']);
}

/**
* Return the recording IDs that we've already synced
*/
Expand All @@ -103,6 +129,16 @@ export default class VoiceNotesPlugin extends Plugin {
)).filter(recordingId => recordingId !== undefined) as number[];
}

async getTodaysSyncedRecordings(): Promise<string[]> {
const { vault } = this.app;

const markdownFiles = vault.getMarkdownFiles().filter(file => file.path.startsWith(this.settings.syncDirectory));

return (await Promise.all(
markdownFiles.map(async (file) => await this.isRecordingFromToday(file) ? file.basename : undefined)
)).filter(filename => filename !== undefined) as string[];
}

async sync(fullSync: boolean = false) {
this.vnApi = new VoiceNotesApi({});
this.vnApi.token = this.settings.token
Expand Down
5 changes: 5 additions & 0 deletions utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { moment } from 'obsidian'

export function capitalizeFirstLetter(word: string): string {
return word[0].toUpperCase() + word.slice(1);
}
Expand All @@ -7,3 +9,6 @@ export function isAlphaNumeric(value: string): boolean {
return regex.test(value);
}

export function isToday(date: string): boolean {
return moment(date).isSame(moment(), 'day')
}

0 comments on commit 9855e5f

Please sign in to comment.