Skip to content

Commit d735f89

Browse files
committed
Add settings to configure time format
1 parent 984e168 commit d735f89

File tree

3 files changed

+57
-53
lines changed

3 files changed

+57
-53
lines changed

esbuild.config.mjs

+1
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));

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -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

+53-53
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+
fileNameFormat: string;
15+
datePropFormat: string;
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+
fileNameFormat: 'YYYY-MM-DD - HH-mm-ss',
25+
datePropFormat: 'YYYY-MM-DD HH:mm:ss Z',
2026
}
2127

2228
let todoCount = 0;
@@ -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,13 @@ 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));
263+
const now = dayjs();
273264

274265
const path = getPath();
275-
const fileName = path + "/" + date + " - " + time + ".md";
276-
266+
const fileName = path + "/" + now.format(this.settings.fileNameFormat) + ".md";
277267
new Notice("Creating " + fileName);
278-
const frontmatter = await createFrontmatter(date + " " + hours + ":" + minutes + ":" + seconds + " " + timezoneOffset, this);
268+
269+
const frontmatter = await createFrontmatter(now.format(this.settings.datePropFormat), this);
279270
new Notice(frontmatter);
280271

281272
try {
@@ -350,58 +341,39 @@ export default class Yesterday extends Plugin {
350341
}
351342

352343
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);
344+
const now = dayjs();
345+
if (now.hour() < 5) {
346+
return pathFromDate(now.subtract(1, 'day'));
358347
} else {
359348
return pathFromDate(now);
360349
}
361350
}
362351

363-
function pathFromDate(date: Date) {
352+
function pathFromDate(date: Dayjs) {
364353
const root = this.app.vault.getRoot().path;
365354

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("/");
355+
const components = [
356+
date.year().toString().substring(0, 3) + "0s",
357+
date.format('YYYY'),
358+
date.format('YYYY-MM'),
359+
date.format('YYYY-MM-DD'),
360+
].join("/");
376361

377362
return root + components;
378363
}
379364

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

389369
async function runCommand(command: string) {
390370
const util = require('util');
391371
const exec = util.promisify(require('child_process').exec);
392-
const { stdout, stderr } = await exec(command);
372+
const { stdout } = await exec(command);
393373

394374
return stdout
395375
}
396376

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-
405377
class YesterdaySettingTab extends PluginSettingTab {
406378
plugin: Yesterday;
407379

@@ -473,5 +445,33 @@ class YesterdaySettingTab extends PluginSettingTab {
473445
this.plugin.settings.maximizeMedia = value;
474446
await this.plugin.saveSettings();
475447
}));
448+
449+
containerEl.createEl('br');
450+
const timeFormatSection = containerEl.createEl('div', {cls: 'setting-item setting-item-heading'});
451+
const timeFormatSectionInfo = timeFormatSection.createEl('div', {cls: 'setting-item-info'});
452+
timeFormatSectionInfo.createEl('div', {text: 'Time format', cls: 'setting-item-name'});
453+
const subHeading = containerEl.createDiv()
454+
subHeading.createEl('a', {text: 'Format documentation', href: 'https://day.js.org/docs/en/display/format'});
455+
containerEl.createEl('br');
456+
457+
new Setting(containerEl)
458+
.setName('Filename format')
459+
.setDesc('Format of entry\'s filename')
460+
.addMomentFormat(toggle => toggle
461+
.setValue(this.plugin.settings.fileNameFormat)
462+
.onChange(async (value) => {
463+
this.plugin.settings.fileNameFormat = value;
464+
await this.plugin.saveSettings();
465+
}));
466+
467+
new Setting(containerEl)
468+
.setName('Format of \'date\' property')
469+
.setDesc('Format of value which will be written to \'date\' property of newly created entry')
470+
.addMomentFormat(toggle => toggle
471+
.setValue(this.plugin.settings.datePropFormat)
472+
.onChange(async (value) => {
473+
this.plugin.settings.datePropFormat = value;
474+
await this.plugin.saveSettings();
475+
}));
476476
}
477-
}
477+
}

0 commit comments

Comments
 (0)