Skip to content

Add compatability with new periodic notes plugin #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@ export const DEFAULT_WEEKLY_NOTE_FORMAT = "gggg-[W]ww";
export const DEFAULT_MONTHLY_NOTE_FORMAT = "YYYY-MM";
export const DEFAULT_QUARTERLY_NOTE_FORMAT = "YYYY-[Q]Q";
export const DEFAULT_YEARLY_NOTE_FORMAT = "YYYY";

export const DEFAULT_FORMAT = Object.freeze({
day: DEFAULT_DAILY_NOTE_FORMAT,
week: DEFAULT_WEEKLY_NOTE_FORMAT,
month: DEFAULT_MONTHLY_NOTE_FORMAT,
quarter: DEFAULT_QUARTERLY_NOTE_FORMAT,
year: DEFAULT_YEARLY_NOTE_FORMAT,
});
40 changes: 7 additions & 33 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ export function appHasDailyNotesPluginLoaded(): boolean {
return true;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const periodicNotes = (<any>app).plugins.getPlugin("periodic-notes");
return periodicNotes && periodicNotes.settings?.daily?.enabled;
return shouldUsePeriodicNotesPluginSettings("day");
}

/**
Expand All @@ -28,36 +26,24 @@ export function appHasDailyNotesPluginLoaded(): boolean {
*/
export function appHasWeeklyNotesPluginLoaded(): boolean {
const { app } = window;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
if ((<any>app).plugins.getPlugin("calendar")) {
return true;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const periodicNotes = (<any>app).plugins.getPlugin("periodic-notes");
return periodicNotes && periodicNotes.settings?.weekly?.enabled;
return shouldUsePeriodicNotesPluginSettings("week");
}

export function appHasMonthlyNotesPluginLoaded(): boolean {
const { app } = window;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const periodicNotes = (<any>app).plugins.getPlugin("periodic-notes");
return periodicNotes && periodicNotes.settings?.monthly?.enabled;
return shouldUsePeriodicNotesPluginSettings("month");
}

export function appHasQuarterlyNotesPluginLoaded(): boolean {
const { app } = window;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const periodicNotes = (<any>app).plugins.getPlugin("periodic-notes");
return periodicNotes && periodicNotes.settings?.quarterly?.enabled;
return shouldUsePeriodicNotesPluginSettings("quarter");
}

export function appHasYearlyNotesPluginLoaded(): boolean {
const { app } = window;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const periodicNotes = (<any>app).plugins.getPlugin("periodic-notes");
return periodicNotes && periodicNotes.settings?.yearly?.enabled;
return shouldUsePeriodicNotesPluginSettings("year");
}

export {
Expand All @@ -70,11 +56,13 @@ export {

import type { IGranularity, IPeriodicNoteSettings } from "./types";
import {
getPeriodicNoteSettings,
getDailyNoteSettings,
getWeeklyNoteSettings,
getMonthlyNoteSettings,
getQuarterlyNoteSettings,
getYearlyNoteSettings,
shouldUsePeriodicNotesPluginSettings,
} from "./settings";
import { createDailyNote, getDailyNote, getAllDailyNotes } from "./daily";
import { createWeeklyNote, getAllWeeklyNotes, getWeeklyNote } from "./weekly";
Expand All @@ -93,20 +81,6 @@ import { createYearlyNote, getAllYearlyNotes, getYearlyNote } from "./yearly";
export { getDateUID, getDateFromFile, getDateFromPath } from "./parse";
export { getTemplateInfo } from "./vault";

function getPeriodicNoteSettings(
granularity: IGranularity
): IPeriodicNoteSettings {
const getSettings = {
day: getDailyNoteSettings,
week: getWeeklyNoteSettings,
month: getMonthlyNoteSettings,
quarter: getQuarterlyNoteSettings,
year: getYearlyNoteSettings,
}[granularity];

return getSettings();
}

function createPeriodicNote(
granularity: IGranularity,
date: Moment
Expand Down
17 changes: 2 additions & 15 deletions src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import type { Moment } from "moment";
import { TFile } from "obsidian";

import {
getDailyNoteSettings,
getWeeklyNoteSettings,
getMonthlyNoteSettings,
getQuarterlyNoteSettings,
getYearlyNoteSettings,
} from "./settings";
import { getPeriodicNoteSettings } from "./settings";

import { IGranularity } from "./types";
import { basename } from "./vault";
Expand Down Expand Up @@ -62,15 +56,8 @@ function getDateFromFilename(
filename: string,
granularity: IGranularity
): Moment | null {
const getSettings = {
day: getDailyNoteSettings,
week: getWeeklyNoteSettings,
month: getMonthlyNoteSettings,
quarter: getQuarterlyNoteSettings,
year: getYearlyNoteSettings,
};

const format = getSettings[granularity]().format.split("/").pop();
const format = getPeriodicNoteSettings(granularity).format.split("/").pop();
const noteDate = window.moment(filename, format, true);

if (!noteDate.isValid()) {
Expand Down
194 changes: 72 additions & 122 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,151 +1,101 @@
import {
DEFAULT_DAILY_NOTE_FORMAT,
DEFAULT_MONTHLY_NOTE_FORMAT,
DEFAULT_WEEKLY_NOTE_FORMAT,
DEFAULT_QUARTERLY_NOTE_FORMAT,
DEFAULT_YEARLY_NOTE_FORMAT,
} from "./constants";
import { IPeriodicNoteSettings } from "./types";
import { DEFAULT_FORMAT } from "./constants";
import {IGranularity, IPeriodicity, IPeriodicNoteSettings} from "./types";

export function shouldUsePeriodicNotesSettings(
periodicity: "daily" | "weekly" | "monthly" | "quarterly" | "yearly"
): boolean {
function getPeriodicNotesPlugin() {
const { app } = window;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const periodicNotes = (<any>window.app).plugins.getPlugin("periodic-notes");
return periodicNotes && periodicNotes.settings?.[periodicity]?.enabled;
return (<any>app).plugins.getPlugin("periodic-notes");
}

/**
* Read the user settings for the `daily-notes` plugin
* to keep behavior of creating a new note in-sync.
*/
export function getDailyNoteSettings(): IPeriodicNoteSettings {
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { internalPlugins, plugins } = <any>window.app;
export function hasPeriodicNotesPlugin(): boolean {
return Boolean(getPeriodicNotesPlugin());
}

if (shouldUsePeriodicNotesSettings("daily")) {
const { format, folder, template } =
plugins.getPlugin("periodic-notes")?.settings?.daily || {};
return {
format: format || DEFAULT_DAILY_NOTE_FORMAT,
folder: folder?.trim() || "",
template: template?.trim() || "",
};
}
export function hasNewPeriodicNotesPlugin(): boolean {
const periodicNotes = getPeriodicNotesPlugin();
// The CalenderSetManager is not available in the old version (0.0.17) of the Periodic Notes plugin
return periodicNotes && Object.hasOwn(periodicNotes, "calendarSetManager");
}

const { folder, format, template } =
internalPlugins.getPluginById("daily-notes")?.instance?.options || {};
return {
format: format || DEFAULT_DAILY_NOTE_FORMAT,
folder: folder?.trim() || "",
template: template?.trim() || "",
};
} catch (err) {
console.info("No custom daily note settings found!", err);
export function shouldUsePeriodicNotesPluginSettings(
granularity: IGranularity
): boolean {
if(!hasPeriodicNotesPlugin()) return false; // no Periodic Notes Plugin
return (getPeriodicNotesPluginRawSettings(granularity))?.enabled;
}

function getPeriodicNotesPluginRawSettings(granularity: IGranularity){
const periodicNotes = getPeriodicNotesPlugin();
if(!periodicNotes) return {}; // no Periodic Notes Plugin
if(hasNewPeriodicNotesPlugin()) {
// New Periodic Notes uses CalendarSetManager
return periodicNotes?.calendarSetManager?.getActiveSet?.()?.[granularity] || {} ;
}
// Legacy settings
return periodicNotes?.settings?.[IPeriodicity[granularity]] || {} ;
}

/**
* Read the user settings for the `weekly-notes` plugin
* to keep behavior of creating a new note in-sync.
*/
export function getWeeklyNoteSettings(): IPeriodicNoteSettings {
export function getPeriodicNotesPluginSettings(granularity: IGranularity) : IPeriodicNoteSettings {
const settings = getPeriodicNotesPluginRawSettings(granularity);
return {
format: settings.format || DEFAULT_FORMAT[granularity],
folder: settings.folder?.trim() || "",
template: (settings.templatePath ?? settings.template)?.trim() || "",
};
}


export function getPeriodicNoteSettings(granularity: IGranularity): IPeriodicNoteSettings {
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const pluginManager = (<any>window.app).plugins;
// First try Periodic Notes Plugin
if (shouldUsePeriodicNotesPluginSettings(granularity)) {
return getPeriodicNotesPluginSettings(granularity);
}

const calendarSettings = pluginManager.getPlugin("calendar")?.options;
const periodicNotesSettings =
pluginManager.getPlugin("periodic-notes")?.settings?.weekly;
// For daily notes also check the Daily Notes plugin
if(granularity == "day") {
const { folder, format, template } =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(<any>window.app).internalPlugins.getPluginById("daily-notes")?.instance?.options || {};
return {
format: format || DEFAULT_FORMAT["day"],
folder: folder?.trim() || "",
template: template?.trim() || "",
};
}

if (shouldUsePeriodicNotesSettings("weekly")) {
// For weekly notes also check the Calendar plugin
if (granularity == "week") {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const calendarSettings = (<any>window.app).plugins.getPlugin("calendar")?.options || {};
return {
format: periodicNotesSettings.format || DEFAULT_WEEKLY_NOTE_FORMAT,
folder: periodicNotesSettings.folder?.trim() || "",
template: periodicNotesSettings.template?.trim() || "",
format: calendarSettings.weeklyNoteFormat || DEFAULT_FORMAT["week"],
folder: calendarSettings.weeklyNoteFolder?.trim() || "",
template: calendarSettings.weeklyNoteTemplate?.trim() || "",
};
}

const settings = calendarSettings || {};
return {
format: settings.weeklyNoteFormat || DEFAULT_WEEKLY_NOTE_FORMAT,
folder: settings.weeklyNoteFolder?.trim() || "",
template: settings.weeklyNoteTemplate?.trim() || "",
};
} catch (err) {
console.info("No custom weekly note settings found!", err);
console.info("No custom", IPeriodicity[granularity], "note settings found!", err);
}
}

/**
* Read the user settings for the `periodic-notes` plugin
* Read the user settings for the `daily-notes` plugin
* to keep behavior of creating a new note in-sync.
*/
export function getDailyNoteSettings(): IPeriodicNoteSettings {
return getPeriodicNoteSettings("day");
}
export function getWeeklyNoteSettings(): IPeriodicNoteSettings {
return getPeriodicNoteSettings("week");
}
export function getMonthlyNoteSettings(): IPeriodicNoteSettings {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const pluginManager = (<any>window.app).plugins;

try {
const settings =
(shouldUsePeriodicNotesSettings("monthly") &&
pluginManager.getPlugin("periodic-notes")?.settings?.monthly) ||
{};

return {
format: settings.format || DEFAULT_MONTHLY_NOTE_FORMAT,
folder: settings.folder?.trim() || "",
template: settings.template?.trim() || "",
};
} catch (err) {
console.info("No custom monthly note settings found!", err);
}
return getPeriodicNoteSettings("month");
}

/**
* Read the user settings for the `periodic-notes` plugin
* to keep behavior of creating a new note in-sync.
*/
export function getQuarterlyNoteSettings(): IPeriodicNoteSettings {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const pluginManager = (<any>window.app).plugins;

try {
const settings =
(shouldUsePeriodicNotesSettings("quarterly") &&
pluginManager.getPlugin("periodic-notes")?.settings?.quarterly) ||
{};

return {
format: settings.format || DEFAULT_QUARTERLY_NOTE_FORMAT,
folder: settings.folder?.trim() || "",
template: settings.template?.trim() || "",
};
} catch (err) {
console.info("No custom quarterly note settings found!", err);
}
return getPeriodicNoteSettings("quarter");
}

/**
* Read the user settings for the `periodic-notes` plugin
* to keep behavior of creating a new note in-sync.
*/
export function getYearlyNoteSettings(): IPeriodicNoteSettings {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const pluginManager = (<any>window.app).plugins;

try {
const settings =
(shouldUsePeriodicNotesSettings("yearly") &&
pluginManager.getPlugin("periodic-notes")?.settings?.yearly) ||
{};

return {
format: settings.format || DEFAULT_YEARLY_NOTE_FORMAT,
folder: settings.folder?.trim() || "",
template: settings.template?.trim() || "",
};
} catch (err) {
console.info("No custom yearly note settings found!", err);
}
return getPeriodicNoteSettings("year");
}
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
export type IGranularity = "day" | "week" | "month" | "quarter" | "year";

export const IPeriodicity: Record<IGranularity, string> = {
day: "daily",
week: "weekly",
month: "monthly",
quarter: "quarterly",
year: "yearly",
};

export interface IPeriodicNoteSettings {
folder?: string;
format?: string;
Expand Down