1
+ import dayjs , { Dayjs } from 'dayjs' ;
1
2
import { App , MarkdownRenderer , Notice , Platform , Plugin , PluginSettingTab , Setting , TFile } from 'obsidian' ;
3
+
2
4
import { YesterdayMedia , ImageModal } from "./media"
3
5
import { YesterdayDialog } from "./dialogs"
4
6
import { mediaExtensions } from './constants' ;
@@ -9,14 +11,18 @@ interface YesterdaySettings {
9
11
showTodoCount : boolean ;
10
12
showMediaGrid : boolean ;
11
13
maximizeMedia : boolean ;
14
+ fileNameFormat : string ;
15
+ datePropFormat : string ;
12
16
}
13
17
14
18
const DEFAULT_SETTINGS : YesterdaySettings = {
15
19
colorMarkdownFiles : true ,
16
20
hideMediaFiles : false ,
17
21
showTodoCount : false ,
18
22
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' ,
20
26
}
21
27
22
28
let todoCount = 0 ;
@@ -205,7 +211,6 @@ export default class Yesterday extends Plugin {
205
211
todoEventListenersAdded : boolean = false ;
206
212
207
213
registerFileOperations ( ) {
208
-
209
214
if ( this . todoEventListenersAdded ) return
210
215
211
216
this . registerEvent (
@@ -255,27 +260,13 @@ export default class Yesterday extends Plugin {
255
260
}
256
261
257
262
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 ( ) ;
273
264
274
265
const path = getPath ( ) ;
275
- const fileName = path + "/" + date + " - " + time + ".md" ;
276
-
266
+ const fileName = path + "/" + now . format ( this . settings . fileNameFormat ) + ".md" ;
277
267
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 ) ;
279
270
new Notice ( frontmatter ) ;
280
271
281
272
try {
@@ -350,58 +341,39 @@ export default class Yesterday extends Plugin {
350
341
}
351
342
352
343
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' ) ) ;
358
347
} else {
359
348
return pathFromDate ( now ) ;
360
349
}
361
350
}
362
351
363
- function pathFromDate ( date : Date ) {
352
+ function pathFromDate ( date : Dayjs ) {
364
353
const root = this . app . vault . getRoot ( ) . path ;
365
354
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 ( "/" ) ;
376
361
377
362
return root + components ;
378
363
}
379
364
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`
387
367
}
388
368
389
369
async function runCommand ( command : string ) {
390
370
const util = require ( 'util' ) ;
391
371
const exec = util . promisify ( require ( 'child_process' ) . exec ) ;
392
- const { stdout, stderr } = await exec ( command ) ;
372
+ const { stdout } = await exec ( command ) ;
393
373
394
374
return stdout
395
375
}
396
376
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
-
405
377
class YesterdaySettingTab extends PluginSettingTab {
406
378
plugin : Yesterday ;
407
379
@@ -473,5 +445,33 @@ class YesterdaySettingTab extends PluginSettingTab {
473
445
this . plugin . settings . maximizeMedia = value ;
474
446
await this . plugin . saveSettings ( ) ;
475
447
} ) ) ;
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
+ } ) ) ;
476
476
}
477
- }
477
+ }
0 commit comments