Skip to content

Commit

Permalink
Added support for custom root folder #5
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikmayer committed Jul 25, 2024
1 parent 2a2e143 commit 945dfd6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 36 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "yesterday",
"name": "Yesterday",
"version": "1.1.0",
"version": "1.2.0",
"minAppVersion": "0.12.0",
"description": "Transform your notes into a visually stunning diary, integrating dialogs, chat logs, and media content blocks for a seamless journaling experience.",
"author": "Dominik Mayer",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-yesterday",
"version": "1.1.0",
"version": "1.2.0",
"description": "Plugin that provides Yesterday journaling support to Obsidian",
"main": "main.js",
"scripts": {
Expand Down
88 changes: 54 additions & 34 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface YesterdaySettings {
maximizeMedia: boolean;
datePropFormat: string;
startOfNextDay: number;
customRootFolder: string;
}

const DEFAULT_SETTINGS: YesterdaySettings = {
Expand All @@ -23,6 +24,7 @@ const DEFAULT_SETTINGS: YesterdaySettings = {
maximizeMedia: true,
datePropFormat: 'YYYY-MM-DD HH:mm:ss Z',
startOfNextDay: 5,
customRootFolder: '',
}

let todoCount = 0;
Expand Down Expand Up @@ -260,7 +262,7 @@ export default class Yesterday extends Plugin {
}

async createEntry(): Promise<void> {
const path = getPath(this.settings.startOfNextDay);
const path = this.getPath(this.settings.startOfNextDay);
const now = dayjs();
const fileName = path + "/" + now.format('YYYY-MM-DD - HH-mm-ss') + ".md";
new Notice("Creating " + fileName);
Expand Down Expand Up @@ -337,28 +339,30 @@ export default class Yesterday extends Plugin {
async saveSettings() {
await this.saveData(this.settings);
}
}

function getPath(startOfNextDay: number) {
const now = dayjs();
if (now.hour() < startOfNextDay) {
return pathFromDate(now.subtract(1, 'day'));
} else {
return pathFromDate(now);
getPath(startOfNextDay: number) {
const now = dayjs();
if (now.hour() < startOfNextDay) {
return this.pathFromDate(now.subtract(1, 'day'));
} else {
return this.pathFromDate(now);
}
}

pathFromDate(date: Dayjs) {
const root = this.settings.customRootFolder
? this.app.vault.getRoot().path + '/' + this.settings.customRootFolder
: this.app.vault.getRoot().path;

const components = [
date.year().toString().substring(0, 3) + '0s',
date.format('YYYY'),
date.format('YYYY-MM'),
date.format('YYYY-MM-DD'),
].join("/");

return root + '/' + components;
}
}

function pathFromDate(date: Dayjs) {
const root = this.app.vault.getRoot().path;

const components = [
date.year().toString().substring(0, 3) + '0s',
date.format('YYYY'),
date.format('YYYY-MM'),
date.format('YYYY-MM-DD'),
].join("/");

return root + components;
}

async function createFrontmatter(datetime: string, _plugin: Yesterday): Promise<string> {
Expand Down Expand Up @@ -386,6 +390,35 @@ class YesterdaySettingTab extends PluginSettingTab {

containerEl.empty();

new Setting(containerEl)
.setName('Root folder')
.setDesc('Set a custom folder for newly created entries')
.addText(text => text
.setPlaceholder('/')
.setValue(this.plugin.settings.customRootFolder)
.onChange(async (value) => {
this.plugin.settings.customRootFolder = value.trim();
await this.plugin.saveSettings();
}));

new Setting(containerEl)
.setName('Start of next day')
.setDesc('In hours after midnight')
.addSlider(toggle => toggle
.setLimits(0, 23, 1)
.setValue(this.plugin.settings.startOfNextDay)
.setDynamicTooltip()
.onChange(async (value) => {
this.plugin.settings.startOfNextDay = value;
await this.plugin.saveSettings();
this.plugin.setStatusBar();
}));

containerEl.createEl('br');
const appearanceSection = containerEl.createEl('div', { cls: 'setting-item setting-item-heading' });
const appearanceSectionInfo = appearanceSection.createEl('div', { cls: 'setting-item-info' });
appearanceSectionInfo.createEl('div', { text: 'Interface', cls: 'setting-item-name' });

new Setting(containerEl)
.setName('Color entries')
.setDesc('Highlights open and resolved entries in different colors')
Expand Down Expand Up @@ -419,19 +452,6 @@ class YesterdaySettingTab extends PluginSettingTab {
this.plugin.setStatusBar();
}));

new Setting(containerEl)
.setName('Start of next day')
.setDesc('In hours after midnight')
.addSlider(toggle => toggle
.setLimits(0, 23, 1)
.setValue(this.plugin.settings.startOfNextDay)
.setDynamicTooltip()
.onChange(async (value) => {
this.plugin.settings.startOfNextDay = value;
await this.plugin.saveSettings();
this.plugin.setStatusBar();
}));

containerEl.createEl('br');
const mediaSection = containerEl.createEl('div', { cls: 'setting-item setting-item-heading' });
const mediaSectionInfo = mediaSection.createEl('div', { cls: 'setting-item-info' });
Expand Down

0 comments on commit 945dfd6

Please sign in to comment.