Skip to content

Commit

Permalink
support monthly note navigation commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Ebonsignori committed Nov 1, 2023
1 parent 3464df8 commit 8a56b14
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 51 deletions.
95 changes: 64 additions & 31 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ import {
SettingsTab,
} from "./settings/settings";
import Core from "./core";
import {
getDateFromNotePath,
getJournalLinkForDay,
getNextDayJournalLink,
getPreviousDayJournalLink,
} from "./note-links";
import moment from "moment-timezone";
import { getJournalLink, navigateToJournalLink } from "./note-links";

export default class AutoJournal extends Plugin {
settings: AutoJournalSettings;
Expand All @@ -35,44 +29,83 @@ export default class AutoJournal extends Plugin {
id: "todays-daily-note",
name: "Open todays daily note",
callback: () => {
const link = getJournalLinkForDay(this.app, this.settings);
if (link) {
this.app.workspace.openLinkText(link, "", this.settings.openNoteCommandInNewTab);
}
const link = getJournalLink(
this.app,
this.settings,
"daily",
"today"
);
navigateToJournalLink(this.app, this.settings, link);
},
});

const getXDayJournalLink = (
nextPrevFunction: typeof getNextDayJournalLink
) => {
const currentFile = this.app.workspace.getActiveFile();
let startDate = moment();
if (currentFile?.path) {
startDate = getDateFromNotePath(
this.addCommand({
id: "next-daily-note",
name: "Open next daily note",
callback: () => {
const link = getJournalLink(
this.app,
this.settings,
currentFile?.path
"daily",
"next"
);
}

const link = nextPrevFunction(startDate, this.app, this.settings);
if (link) {
this.app.workspace.openLinkText(link, "", this.settings.openNoteCommandInNewTab);
}
};
navigateToJournalLink(this.app, this.settings, link);
},
});

this.addCommand({
id: "previous-daily-note",
name: "Open next daily note",
name: "Open previous daily note",
callback: () => {
getXDayJournalLink(getNextDayJournalLink);
const link = getJournalLink(
this.app,
this.settings,
"daily",
"previous"
);
navigateToJournalLink(this.app, this.settings, link);
},
});

this.addCommand({
id: "next-daily-note",
name: "Open previous daily note",
id: "todays-monthly-note",
name: "Open todays monthly note",
callback: () => {
getXDayJournalLink(getPreviousDayJournalLink);
const link = getJournalLink(
this.app,
this.settings,
"monthly",
"today"
);
navigateToJournalLink(this.app, this.settings, link);
},
});

this.addCommand({
id: "next-monthly-note",
name: "Open next monthly note",
callback: () => {
const link = getJournalLink(
this.app,
this.settings,
"monthly",
"next"
);
navigateToJournalLink(this.app, this.settings, link);
},
});

this.addCommand({
id: "previous-monthly-note",
name: "Open previous monthly note",
callback: () => {
const link = getJournalLink(
this.app,
this.settings,
"monthly",
"previous"
);
navigateToJournalLink(this.app, this.settings, link);
},
});

Expand Down
119 changes: 99 additions & 20 deletions src/note-links.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,60 @@
import moment from "moment";
import { AutoJournalSettings } from "./settings/settings";
import { App, Notice } from "obsidian";
import { addMonthToDate, subtractMonthFromDate } from "./utils/date";

export function getDateFromNotePath(
export function getJournalLink(
app: App,
settings: AutoJournalSettings,
dailyOrMonthly: "daily" | "monthly",
nextOrPrevious: "next" | "previous" | "today"
) {
const currentFile = app.workspace.getActiveFile();
let startDate = moment();
if (currentFile?.path) {
if (dailyOrMonthly === "daily") {
startDate = getDateFromDailyNotePath(settings, currentFile?.path);
} else if (dailyOrMonthly === "monthly") {
startDate = getDateFromMonthlyNotePath(settings, currentFile?.path);
}
}

let link;
let adjustedDate;
if (nextOrPrevious === "next") {
adjustedDate =
dailyOrMonthly === "daily"
? startDate.clone().add(1, "day")
: addMonthToDate(startDate);
} else if (nextOrPrevious === "previous") {
adjustedDate =
dailyOrMonthly === "daily"
? startDate.clone().subtract(1, "day")
: subtractMonthFromDate(startDate);
} else {
adjustedDate = moment();
}

if (dailyOrMonthly === "daily") {
link = getDailyJournalLinkForDay(app, settings, adjustedDate);
} else if (dailyOrMonthly === "monthly") {
link = getMonthlyJournalLinkForDay(app, settings, adjustedDate);
}

return link;
}

export function navigateToJournalLink(
app: App,
settings: AutoJournalSettings,
link?: string
) {
if (link) {
app.workspace.openLinkText(link, "", settings.openNoteCommandInNewTab);
}
}

export function getDateFromDailyNotePath(
settings: AutoJournalSettings,
filePath: string
): moment.Moment {
Expand All @@ -16,52 +68,79 @@ export function getDateFromNotePath(
);
}

export function getJournalLinkForDay(
export function getDateFromMonthlyNotePath(
settings: AutoJournalSettings,
filePath: string
): moment.Moment {
const splitPath = filePath?.split("/");
const month = splitPath?.[splitPath.length - 1].split("-")?.[0]?.trim();
const year = splitPath?.[splitPath.length - 3];
return moment(
`${year}-${month}-01`,
`${settings.yearFormat}-${settings.monthFormat}-DD`
);
}

export function getDailyJournalLinkForDay(
app: App,
settings: AutoJournalSettings,
journalDate = moment()
): string {
const journalFolder = `${settings.rootFolder}/${journalDate.format(
const noteFolder = `${settings.rootFolder}/${journalDate.format(
settings.yearFormat
)}/${journalDate.format(settings.monthFormat)}`;
const journalDay = journalDate.format(settings.dayFormat);
const noteDay = journalDate.format(settings.dayFormat);

const dayFiles = app.vault.getFiles().filter((file) => {
return file?.parent?.path === journalFolder;
return file?.parent?.path === noteFolder;
});

let link;
for (const file of dayFiles) {
if (file.name.split("-")?.[0]?.trim() === journalDay) {
if (file.name.split("-")?.[0]?.trim() === noteDay) {
link = file.path;
break;
}
}

if (!link) {
new Notice(
`Unable to find journal file for day ${journalDay} in ${journalFolder}. Is it created?`
`Unable to find journal file for day ${noteDay} in ${noteFolder}. Is it created?`
);
return "";
}

return link;
}

export function getNextDayJournalLink(
startDate: moment.Moment,
export function getMonthlyJournalLinkForDay(
app: App,
settings: AutoJournalSettings
settings: AutoJournalSettings,
journalDate = moment()
): string {
const nextDay = startDate.clone().add(1, "day");
return getJournalLinkForDay(app, settings, nextDay);
}
const noteFolder = `${settings.rootFolder}/${journalDate.format(
settings.yearFormat
)}/${settings.monthlyNotesFolderName}`;
const noteMonth = journalDate.format(settings.monthFormat);

export function getPreviousDayJournalLink(
startDate: moment.Moment,
app: App,
settings: AutoJournalSettings
): string {
const previousDay = startDate.clone().subtract(1, "day");
return getJournalLinkForDay(app, settings, previousDay);
const monthlyFiles = app.vault.getFiles().filter((file) => {
return file?.parent?.path === noteFolder;
});

let link;
for (const file of monthlyFiles) {
if (file.name.split("-")?.[0]?.trim() === noteMonth) {
link = file.path;
break;
}
}

if (!link) {
new Notice(
`Unable to find journal file for month ${noteMonth} in ${noteFolder}. Is it created?`
);
return "";
}

return link;
}
18 changes: 18 additions & 0 deletions src/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,28 @@ export class SettingsTab extends PluginSettingTab {
new Setting(this.containerEl).setName(`${type} notes`).setHeading();

// - - - Begin Option: notesEnabled
let pathString = "Notes will be saved to: ";
if (type === "Daily") {
pathString =
pathString +
`${this.plugin.settings.rootFolder}/${this.plugin.settings.yearFormat}/${this.plugin.settings.monthFormat}/${this.plugin.settings.dayFormat} -`;
} else if (type === "Monthly") {
pathString =
pathString +
`${this.plugin.settings.rootFolder}/${this.plugin.settings.yearFormat}/[${this.plugin.settings.monthlyNotesFolderName}]/${this.plugin.settings.monthFormat} -`;
}
const derivedFilePath = document.createDocumentFragment();
derivedFilePath.append(pathString);
const createNotesDesc = document.createDocumentFragment();
createNotesDesc.append(
`Toggle on/off to trigger ${lowerType} note functionality.`
);
if (this.plugin.settings[`${lowerType}NotesEnabled`]) {
createNotesDesc.append(
createNotesDesc.createEl("br"),
derivedFilePath
);
}
new Setting(this.containerEl)
.setName(`Create ${lowerType} notes?`)
.setDesc(createNotesDesc)
Expand Down
31 changes: 31 additions & 0 deletions src/utils/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import moment from "moment";

export function addMonthToDate(currentDate: moment.Moment): moment.Moment {
let futureMonth = moment(currentDate).clone().add(1, "M");
const futureMonthEnd = moment(futureMonth).endOf("month");

if (
currentDate.date() != futureMonth.date() &&
futureMonth.isSame(futureMonthEnd.format("YYYY-MM-DD"))
) {
futureMonth = futureMonth.add(1, "d");
}

return futureMonth;
}

export function subtractMonthFromDate(
currentDate: moment.Moment
): moment.Moment {
let previousMonth = moment(currentDate).clone().subtract(1, "M");
const previousMonthBegin = moment(previousMonth).startOf("month");

if (
currentDate.date() != previousMonth.date() &&
previousMonth.isSame(previousMonthBegin.format("YYYY-MM-DD"))
) {
previousMonth = previousMonth.subtract(1, "d");
}

return previousMonth;
}

0 comments on commit 8a56b14

Please sign in to comment.