Skip to content

Commit 65b4f7d

Browse files
authored
Merge pull request #3 from istudyatuni/add-more-config
Add more configuration options
2 parents 984e168 + 0ecb71e commit 65b4f7d

File tree

4 files changed

+93
-63
lines changed

4 files changed

+93
-63
lines changed

esbuild.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ esbuild.build({
2525
sourcemap: prod ? false : 'inline',
2626
treeShaking: true,
2727
outfile: 'main.js',
28+
minify: prod,
2829
}).catch(() => process.exit(1));

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "yesterday",
33
"name": "Yesterday",
4-
"version": "1.0.10",
4+
"version": "1.0.11",
55
"minAppVersion": "0.12.0",
66
"description": "Transform your notes into a visually stunning diary, integrating dialogs, chat logs, and media content blocks for a seamless journaling experience.",
77
"author": "Dominik Mayer",

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-yesterday",
3-
"version": "1.0.10",
3+
"version": "1.0.11",
44
"description": "Plugin that provides Yesterday journaling support to Obsidian",
55
"main": "main.js",
66
"scripts": {
@@ -19,5 +19,8 @@
1919
"obsidian": "^0.12.17",
2020
"tslib": "2.3.1",
2121
"typescript": "4.4.4"
22+
},
23+
"dependencies": {
24+
"dayjs": "^1.11.11"
2225
}
2326
}

src/main.ts

Lines changed: 87 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import dayjs, { Dayjs } from 'dayjs';
12
import { App, MarkdownRenderer, Notice, Platform, Plugin, PluginSettingTab, Setting, TFile } from 'obsidian';
3+
24
import { YesterdayMedia, ImageModal } from "./media"
35
import { YesterdayDialog } from "./dialogs"
46
import { mediaExtensions } from './constants';
@@ -9,14 +11,18 @@ interface YesterdaySettings {
911
showTodoCount: boolean;
1012
showMediaGrid: boolean;
1113
maximizeMedia: boolean;
14+
datePropFormat: string;
15+
startOfNextDay: number;
1216
}
1317

1418
const DEFAULT_SETTINGS: YesterdaySettings = {
1519
colorMarkdownFiles: true,
1620
hideMediaFiles: false,
1721
showTodoCount: false,
1822
showMediaGrid: true,
19-
maximizeMedia: true
23+
maximizeMedia: true,
24+
datePropFormat: 'YYYY-MM-DD HH:mm:ss Z',
25+
startOfNextDay: 5,
2026
}
2127

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

7278
if (
73-
target.tagName === 'IMG' &&
74-
target.closest('.media-embed') &&
79+
target.tagName === 'IMG' &&
80+
target.closest('.media-embed') &&
7581
this.settings.maximizeMedia
7682
) {
7783
let imgSrc = (target as HTMLImageElement).src;
@@ -205,7 +211,6 @@ export default class Yesterday extends Plugin {
205211
todoEventListenersAdded: boolean = false;
206212

207213
registerFileOperations() {
208-
209214
if (this.todoEventListenersAdded) return
210215

211216
this.registerEvent(
@@ -255,27 +260,12 @@ export default class Yesterday extends Plugin {
255260
}
256261

257262
async createEntry(): Promise<void> {
258-
259-
const now = new Date();
260-
261-
const year = now.getFullYear();
262-
const month = ("0" + (now.getMonth() + 1)).slice(-2);
263-
const day = ("0" + now.getDate()).slice(-2);
264-
const date = [year, month, day].join("-");
265-
266-
const hours = ("0" + now.getHours()).slice(-2);
267-
const minutes = ("0" + now.getMinutes()).slice(-2);
268-
const seconds = ("0" + now.getSeconds()).slice(-2);
269-
const time = [hours, minutes, seconds].join("-");
270-
271-
const timezoneOffsetNumber = now.getTimezoneOffset();
272-
const timezoneOffset = ((timezoneOffsetNumber < 0 ? '+' : '-') + pad(Math.abs(timezoneOffsetNumber / 60), 2) + ":" + pad(Math.abs(timezoneOffsetNumber % 60), 2));
273-
274-
const path = getPath();
275-
const fileName = path + "/" + date + " - " + time + ".md";
276-
263+
const path = getPath(this.settings.startOfNextDay);
264+
const now = dayjs();
265+
const fileName = path + "/" + now.format('YYYY-MM-DD - HH-mm-ss') + ".md";
277266
new Notice("Creating " + fileName);
278-
const frontmatter = await createFrontmatter(date + " " + hours + ":" + minutes + ":" + seconds + " " + timezoneOffset, this);
267+
268+
const frontmatter = await createFrontmatter(now.format(this.settings.datePropFormat || DEFAULT_SETTINGS.datePropFormat), this);
279269
new Notice(frontmatter);
280270

281271
try {
@@ -349,59 +339,40 @@ export default class Yesterday extends Plugin {
349339
}
350340
}
351341

352-
function getPath() {
353-
const now = new Date();
354-
if (now.getHours() < 5) {
355-
const yesterday = new Date()
356-
yesterday.setDate(now.getDate() - 1);
357-
return pathFromDate(yesterday);
342+
function getPath(startOfNextDay: number) {
343+
const now = dayjs();
344+
if (now.hour() < startOfNextDay) {
345+
return pathFromDate(now.subtract(1, 'day'));
358346
} else {
359347
return pathFromDate(now);
360348
}
361349
}
362350

363-
function pathFromDate(date: Date) {
351+
function pathFromDate(date: Dayjs) {
364352
const root = this.app.vault.getRoot().path;
365353

366-
const year = date.getFullYear();
367-
const decade = year.toString().substring(0, 3) + "0s";
368-
369-
const month = ("0" + (date.getMonth() + 1)).slice(-2);
370-
const day = ("0" + date.getDate()).slice(-2);
371-
372-
const fullMonth = [year, month].join("-");
373-
const fullDate = [year, month, day].join("-");
374-
375-
const components = [decade, year, fullMonth, fullDate].join("/");
354+
const components = [
355+
date.year().toString().substring(0, 3) + '0s',
356+
date.format('YYYY'),
357+
date.format('YYYY-MM'),
358+
date.format('YYYY-MM-DD'),
359+
].join("/");
376360

377361
return root + components;
378362
}
379363

380-
async function createFrontmatter(datetime: string, plugin: Yesterday): Promise<string> {
381-
382-
return `---
383-
date: ${datetime}
384-
---
385-
386-
`
364+
async function createFrontmatter(datetime: string, _plugin: Yesterday): Promise<string> {
365+
return `---\ndate: ${datetime}\n---\n\n`
387366
}
388367

389368
async function runCommand(command: string) {
390369
const util = require('util');
391370
const exec = util.promisify(require('child_process').exec);
392-
const { stdout, stderr } = await exec(command);
371+
const { stdout } = await exec(command);
393372

394373
return stdout
395374
}
396375

397-
function pad(number: number, length: number) {
398-
let str = "" + number
399-
while (str.length < length) {
400-
str = '0' + str
401-
}
402-
return str
403-
}
404-
405376
class YesterdaySettingTab extends PluginSettingTab {
406377
plugin: Yesterday;
407378

@@ -448,10 +419,23 @@ class YesterdaySettingTab extends PluginSettingTab {
448419
this.plugin.setStatusBar();
449420
}));
450421

422+
new Setting(containerEl)
423+
.setName('Start of next day')
424+
.setDesc('In hours after midnight')
425+
.addSlider(toggle => toggle
426+
.setLimits(0, 23, 1)
427+
.setValue(this.plugin.settings.startOfNextDay)
428+
.setDynamicTooltip()
429+
.onChange(async (value) => {
430+
this.plugin.settings.startOfNextDay = value;
431+
await this.plugin.saveSettings();
432+
this.plugin.setStatusBar();
433+
}));
434+
451435
containerEl.createEl('br');
452-
const mediaSection = containerEl.createEl('div', {cls: 'setting-item setting-item-heading'});
453-
const mediaSectionInfo = mediaSection.createEl('div', {cls: 'setting-item-info'});
454-
mediaSectionInfo.createEl('div', {text: 'Media', cls: 'setting-item-name'});
436+
const mediaSection = containerEl.createEl('div', { cls: 'setting-item setting-item-heading' });
437+
const mediaSectionInfo = mediaSection.createEl('div', { cls: 'setting-item-info' });
438+
mediaSectionInfo.createEl('div', { text: 'Media', cls: 'setting-item-name' });
455439

456440
new Setting(containerEl)
457441
.setName('Show media files in a grid')
@@ -473,5 +457,47 @@ class YesterdaySettingTab extends PluginSettingTab {
473457
this.plugin.settings.maximizeMedia = value;
474458
await this.plugin.saveSettings();
475459
}));
460+
461+
const timeFormatSection = containerEl.createEl('div', { cls: 'setting-item setting-item-heading' });
462+
const timeFormatSectionInfo = timeFormatSection.createEl('div', { cls: 'setting-item-info' });
463+
timeFormatSectionInfo.createEl('div', { text: 'Time format', cls: 'setting-item-name' });
464+
465+
const timeFormatDescription = timeFormatSectionInfo.createEl('div', { cls: 'setting-item-description' });
466+
467+
const timeFormatText = createEl('span', {
468+
text: 'If you change the time format your journal will not work with the '
469+
});
470+
timeFormatDescription.appendChild(timeFormatText);
471+
472+
const appLink = createEl('a', {
473+
text: 'Yesterday app',
474+
href: 'https://www.yesterday.md'
475+
});
476+
timeFormatText.appendChild(appLink);
477+
timeFormatText.appendChild(document.createTextNode('.'));
478+
479+
const additionalText = createEl('span', {
480+
text: ' See the '
481+
});
482+
timeFormatDescription.appendChild(additionalText);
483+
484+
const docLink = createEl('a', {
485+
text: 'format documentation',
486+
href: 'https://day.js.org/docs/en/display/format'
487+
});
488+
additionalText.appendChild(docLink);
489+
additionalText.appendChild(document.createTextNode(' for details.'));
490+
491+
new Setting(containerEl)
492+
.setName('Frontmatter \'date\'')
493+
.setDesc('The format of the \'date\' property in the frontmatter of newly created entries')
494+
.addMomentFormat(text => text.setPlaceholder(DEFAULT_SETTINGS.datePropFormat)
495+
.setValue((this.plugin.settings.datePropFormat || '') + '')
496+
.setDefaultFormat(DEFAULT_SETTINGS.datePropFormat)
497+
.onChange(async (v) => {
498+
let value = v.trim()
499+
this.plugin.settings.datePropFormat = value;
500+
await this.plugin.saveSettings();
501+
}));
476502
}
477-
}
503+
}

0 commit comments

Comments
 (0)