Skip to content

Commit

Permalink
#1 replace luxor with date-fns for better compatibility with date str…
Browse files Browse the repository at this point in the history
…ings
  • Loading branch information
13hannes11 committed Aug 23, 2024
1 parent 9b0e7d1 commit 6261c9f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

bun.lockb

# Logs

logs
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
},
"dependencies": {
"@logseq/libs": "^0.0.17",
"luxon": "^3.5.0"
"date-fns": "^3.6.0"
}
}
41 changes: 22 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,52 @@
const { DateTime, Duration } = require("luxon");
import { format, addDays, subDays, startOfWeek } from 'date-fns';
import "@logseq/libs";

// Weeks start with Monday which is day 1
const weekStartIndex = 1
const weekStartIndex = 1; // Monday

function startOfWeek(date) {
const durationUntillStartOfWeek = durationFromDays(date.weekday - weekStartIndex);
return date.minus(durationUntillStartOfWeek);
function startOfWeekFromDate(date) {
return startOfWeek(date, { weekStartsOn: weekStartIndex });
}

function durationFromDays(days) {
return Duration.fromObject({ days: days});
return days;
}

function dayHasPassed(dayNumber) {
return dayNumber < DateTime.local().weekday
const today = new Date();
const todayDayNumber = today.getDay();
return dayNumber < todayDayNumber || (dayNumber === 0 && todayDayNumber !== 0);
}

function dayIsToday(dayNumber) {
return dayNumber === DateTime.local().weekday
const today = new Date();
const todayDayNumber = today.getDay();
return dayNumber === todayDayNumber;
}

function dateOfPreviousWeekdayWith(dayNumber) {
const currentDate = DateTime.local();
if(dayHasPassed(dayNumber)) {
return startOfWeek(currentDate).plus(durationFromDays(dayNumber - weekStartIndex));
const currentDate = new Date();
if (dayHasPassed(dayNumber)) {
return addDays(startOfWeekFromDate(currentDate), dayNumber - weekStartIndex);
} else {
return startOfWeek(currentDate).minus(durationFromDays(7 - dayNumber + weekStartIndex));
return subDays(startOfWeekFromDate(currentDate), 7 - dayNumber + weekStartIndex);
}
}

function dateOfNextWeekdayWith(dayNumber) {
const currentDate = DateTime.local();
if(dayHasPassed(dayNumber) || dayIsToday(dayNumber)) {
const startOfNextWeek = startOfWeek(currentDate.plus(durationFromDays(7)))
return startOfNextWeek.plus(durationFromDays(dayNumber - weekStartIndex));
const currentDate = new Date();
if (dayHasPassed(dayNumber) || dayIsToday(dayNumber)) {
const startOfNextWeek = addDays(startOfWeekFromDate(currentDate), 7);
return addDays(startOfNextWeek, dayNumber - weekStartIndex);
} else {
return startOfWeek(currentDate).plus(durationFromDays(dayNumber - weekStartIndex));
return addDays(startOfWeekFromDate(currentDate), dayNumber - weekStartIndex);
}
}

async function addDateToLogseq(date) {
const { preferredDateFormat } = await logseq.App.getUserConfigs();
logseq.ready(main).catch(console.error)
await logseq.Editor.insertAtEditingCursor(`[[${date.toFormat(preferredDateFormat) }]] `)
logseq.ready(main).catch(console.error);
await logseq.Editor.insertAtEditingCursor(`[[${format(date, preferredDateFormat)}]] `);
}

const slashCommandFunctionMap = {
Expand Down

0 comments on commit 6261c9f

Please sign in to comment.