Skip to content

Commit

Permalink
fix when filename date could be misinterpreted
Browse files Browse the repository at this point in the history
  • Loading branch information
Ebonsignori committed Nov 2, 2023
1 parent 8a4b2d4 commit ecb2228
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/note-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function getJournalLink(
nextOrPrevious: "next" | "previous" | "today"
) {
const currentFile = app.workspace.getActiveFile();
let startDate = moment();
let startDate: moment.Moment | null = moment();
if (currentFile?.path) {
if (dailyOrMonthly === "daily") {
startDate = getDateFromDailyNotePath(settings, currentFile?.path);
Expand All @@ -20,7 +20,7 @@ export function getJournalLink(
}

// If current file doesn't provide a valid date, use today's date
if (!startDate.isValid()) {
if (!startDate || !startDate.isValid()) {
startDate = moment();
}

Expand Down Expand Up @@ -62,11 +62,14 @@ export function navigateToJournalLink(
export function getDateFromDailyNotePath(
settings: AutoJournalSettings,
filePath: string
): moment.Moment {
): moment.Moment | null {
const splitPath = filePath?.split("/");
const day = splitPath?.[splitPath.length - 1].split("-")?.[0]?.trim();
const month = splitPath?.[splitPath.length - 2];
const year = splitPath?.[splitPath.length - 3];
if (!month || !year || !day) {
return null;
}
return moment(
`${year}-${month}-${day}`,
`${settings.yearFormat}-${settings.monthFormat}-${settings.dayFormat}`
Expand All @@ -76,10 +79,13 @@ export function getDateFromDailyNotePath(
export function getDateFromMonthlyNotePath(
settings: AutoJournalSettings,
filePath: string
): moment.Moment {
): moment.Moment | null {
const splitPath = filePath?.split("/");
const month = splitPath?.[splitPath.length - 1].split("-")?.[0]?.trim();
const year = splitPath?.[splitPath.length - 3];
if (!month || !year) {
return null;
}
return moment(
`${year}-${month}-01`,
`${settings.yearFormat}-${settings.monthFormat}-DD`
Expand Down

0 comments on commit ecb2228

Please sign in to comment.