Skip to content

Commit

Permalink
Merge pull request #3 from istudyatuni/add-more-config
Browse files Browse the repository at this point in the history
Add more configuration options
  • Loading branch information
dominikmayer committed May 13, 2024
2 parents 984e168 + 0ecb71e commit 65b4f7d
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 63 deletions.
1 change: 1 addition & 0 deletions esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ esbuild.build({
sourcemap: prod ? false : 'inline',
treeShaking: true,
outfile: 'main.js',
minify: prod,
}).catch(() => process.exit(1));
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.0.10",
"version": "1.0.11",
"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
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-yesterday",
"version": "1.0.10",
"version": "1.0.11",
"description": "Plugin that provides Yesterday journaling support to Obsidian",
"main": "main.js",
"scripts": {
Expand All @@ -19,5 +19,8 @@
"obsidian": "^0.12.17",
"tslib": "2.3.1",
"typescript": "4.4.4"
},
"dependencies": {
"dayjs": "^1.11.11"
}
}
148 changes: 87 additions & 61 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import dayjs, { Dayjs } from 'dayjs';
import { App, MarkdownRenderer, Notice, Platform, Plugin, PluginSettingTab, Setting, TFile } from 'obsidian';

import { YesterdayMedia, ImageModal } from "./media"
import { YesterdayDialog } from "./dialogs"
import { mediaExtensions } from './constants';
Expand All @@ -9,14 +11,18 @@ interface YesterdaySettings {
showTodoCount: boolean;
showMediaGrid: boolean;
maximizeMedia: boolean;
datePropFormat: string;
startOfNextDay: number;
}

const DEFAULT_SETTINGS: YesterdaySettings = {
colorMarkdownFiles: true,
hideMediaFiles: false,
showTodoCount: false,
showMediaGrid: true,
maximizeMedia: true
maximizeMedia: true,
datePropFormat: 'YYYY-MM-DD HH:mm:ss Z',
startOfNextDay: 5,
}

let todoCount = 0;
Expand Down Expand Up @@ -70,8 +76,8 @@ export default class Yesterday extends Plugin {
let target = event.target as HTMLElement;

if (
target.tagName === 'IMG' &&
target.closest('.media-embed') &&
target.tagName === 'IMG' &&
target.closest('.media-embed') &&
this.settings.maximizeMedia
) {
let imgSrc = (target as HTMLImageElement).src;
Expand Down Expand Up @@ -205,7 +211,6 @@ export default class Yesterday extends Plugin {
todoEventListenersAdded: boolean = false;

registerFileOperations() {

if (this.todoEventListenersAdded) return

this.registerEvent(
Expand Down Expand Up @@ -255,27 +260,12 @@ export default class Yesterday extends Plugin {
}

async createEntry(): Promise<void> {

const now = new Date();

const year = now.getFullYear();
const month = ("0" + (now.getMonth() + 1)).slice(-2);
const day = ("0" + now.getDate()).slice(-2);
const date = [year, month, day].join("-");

const hours = ("0" + now.getHours()).slice(-2);
const minutes = ("0" + now.getMinutes()).slice(-2);
const seconds = ("0" + now.getSeconds()).slice(-2);
const time = [hours, minutes, seconds].join("-");

const timezoneOffsetNumber = now.getTimezoneOffset();
const timezoneOffset = ((timezoneOffsetNumber < 0 ? '+' : '-') + pad(Math.abs(timezoneOffsetNumber / 60), 2) + ":" + pad(Math.abs(timezoneOffsetNumber % 60), 2));

const path = getPath();
const fileName = path + "/" + date + " - " + time + ".md";

const path = getPath(this.settings.startOfNextDay);
const now = dayjs();
const fileName = path + "/" + now.format('YYYY-MM-DD - HH-mm-ss') + ".md";
new Notice("Creating " + fileName);
const frontmatter = await createFrontmatter(date + " " + hours + ":" + minutes + ":" + seconds + " " + timezoneOffset, this);

const frontmatter = await createFrontmatter(now.format(this.settings.datePropFormat || DEFAULT_SETTINGS.datePropFormat), this);
new Notice(frontmatter);

try {
Expand Down Expand Up @@ -349,59 +339,40 @@ export default class Yesterday extends Plugin {
}
}

function getPath() {
const now = new Date();
if (now.getHours() < 5) {
const yesterday = new Date()
yesterday.setDate(now.getDate() - 1);
return pathFromDate(yesterday);
function getPath(startOfNextDay: number) {
const now = dayjs();
if (now.hour() < startOfNextDay) {
return pathFromDate(now.subtract(1, 'day'));
} else {
return pathFromDate(now);
}
}

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

const year = date.getFullYear();
const decade = year.toString().substring(0, 3) + "0s";

const month = ("0" + (date.getMonth() + 1)).slice(-2);
const day = ("0" + date.getDate()).slice(-2);

const fullMonth = [year, month].join("-");
const fullDate = [year, month, day].join("-");

const components = [decade, year, fullMonth, fullDate].join("/");
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> {

return `---
date: ${datetime}
---
`
async function createFrontmatter(datetime: string, _plugin: Yesterday): Promise<string> {
return `---\ndate: ${datetime}\n---\n\n`
}

async function runCommand(command: string) {
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const { stdout, stderr } = await exec(command);
const { stdout } = await exec(command);

return stdout
}

function pad(number: number, length: number) {
let str = "" + number
while (str.length < length) {
str = '0' + str
}
return str
}

class YesterdaySettingTab extends PluginSettingTab {
plugin: Yesterday;

Expand Down Expand Up @@ -448,10 +419,23 @@ 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'});
mediaSectionInfo.createEl('div', {text: 'Media', cls: 'setting-item-name'});
const mediaSection = containerEl.createEl('div', { cls: 'setting-item setting-item-heading' });
const mediaSectionInfo = mediaSection.createEl('div', { cls: 'setting-item-info' });
mediaSectionInfo.createEl('div', { text: 'Media', cls: 'setting-item-name' });

new Setting(containerEl)
.setName('Show media files in a grid')
Expand All @@ -473,5 +457,47 @@ class YesterdaySettingTab extends PluginSettingTab {
this.plugin.settings.maximizeMedia = value;
await this.plugin.saveSettings();
}));

const timeFormatSection = containerEl.createEl('div', { cls: 'setting-item setting-item-heading' });
const timeFormatSectionInfo = timeFormatSection.createEl('div', { cls: 'setting-item-info' });
timeFormatSectionInfo.createEl('div', { text: 'Time format', cls: 'setting-item-name' });

const timeFormatDescription = timeFormatSectionInfo.createEl('div', { cls: 'setting-item-description' });

const timeFormatText = createEl('span', {
text: 'If you change the time format your journal will not work with the '
});
timeFormatDescription.appendChild(timeFormatText);

const appLink = createEl('a', {
text: 'Yesterday app',
href: 'https://www.yesterday.md'
});
timeFormatText.appendChild(appLink);
timeFormatText.appendChild(document.createTextNode('.'));

const additionalText = createEl('span', {
text: ' See the '
});
timeFormatDescription.appendChild(additionalText);

const docLink = createEl('a', {
text: 'format documentation',
href: 'https://day.js.org/docs/en/display/format'
});
additionalText.appendChild(docLink);
additionalText.appendChild(document.createTextNode(' for details.'));

new Setting(containerEl)
.setName('Frontmatter \'date\'')
.setDesc('The format of the \'date\' property in the frontmatter of newly created entries')
.addMomentFormat(text => text.setPlaceholder(DEFAULT_SETTINGS.datePropFormat)
.setValue((this.plugin.settings.datePropFormat || '') + '')
.setDefaultFormat(DEFAULT_SETTINGS.datePropFormat)
.onChange(async (v) => {
let value = v.trim()
this.plugin.settings.datePropFormat = value;
await this.plugin.saveSettings();
}));
}
}
}

0 comments on commit 65b4f7d

Please sign in to comment.