Skip to content

Commit c1c477c

Browse files
committed
got logic updated so that date modified can be set to only be updated if a change happens in Obsidian via the user or Linter
1 parent d36e962 commit c1c477c

File tree

8 files changed

+178
-32
lines changed

8 files changed

+178
-32
lines changed

__tests__/yaml-timestamp.test.ts

Lines changed: 123 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ ruleTest({
6363
format: 'dddd, MMMM Do YYYY, h:mm a ',
6464
currentTime: moment('Thursday, January 2nd 2020, 12:01 am', 'dddd, MMMM Do YYYY, h:mm a'),
6565
alreadyModified: false,
66-
forceRetentionOfCreatedValue: false,
66+
dateCreatedSourceOfTruth: 'frontmatter',
6767
fileModifiedTime: '2020-01-02T00:00:00-00',
6868
},
6969
},
7070
{
71-
testName: 'When the date format changes and `forceRetentionOfCreatedValue = true`, date created value is based on the one in the YAML frontmatter.',
71+
testName: 'When the date format changes and `dateCreatedSourceOfTruth = frontmatter`, date created value is based on the one in the YAML frontmatter.',
7272
before: dedent`
7373
---
7474
created: Wednesday, January 1st 2020, 12:00:00 am
@@ -82,7 +82,7 @@ ruleTest({
8282
options: {
8383
dateModified: false,
8484
dateCreatedKey: 'created',
85-
forceRetentionOfCreatedValue: true,
85+
dateCreatedSourceOfTruth: 'frontmatter',
8686
format: 'YYYY, h:mm:ss a',
8787
locale: 'en',
8888
},
@@ -359,7 +359,7 @@ ruleTest({
359359
},
360360
},
361361
{ // accounts for https://github.com/platers/obsidian-linter/issues/745
362-
testName: 'When no changes are made, and force retention of creation date is active, do not update date modified when no change in modification time has been made',
362+
testName: 'When no changes are made, and frontmatter is the source of truth, do not update date modified when no change in modification time has been made',
363363
before: dedent`
364364
---
365365
tag: tag1
@@ -383,11 +383,11 @@ ruleTest({
383383
fileModifiedTime: '2020-02-04T18:00:00-00:00',
384384
currentTime: moment('Tuesday, February 4th 2020, 6:00:07 pm', 'dddd, MMMM Do YYYY, h:mm:ss a'),
385385
alreadyModified: false,
386-
forceRetentionOfCreatedValue: true,
386+
dateCreatedSourceOfTruth: 'frontmatter',
387387
},
388388
},
389389
{
390-
testName: 'When creation date exists and force retention of created date and convert to UTC are true, creation date should remain unchanged',
390+
testName: 'When creation date exists and frontmatter is the source of truth and convert to UTC are true, creation date should remain unchanged',
391391
before: dedent`
392392
---
393393
created: 2019-12-31T14:00:00+00:00
@@ -405,7 +405,7 @@ ruleTest({
405405
dateModified: false,
406406
fileCreatedTime: '2020-01-01T09:00:00-05:00', // 9 AM Eastern Standard Time
407407
currentTime: moment('2020-01-01T21:00:05-05:00', 'YYYY-MM-DDTHH:mm:ssZ'), // 9:00:05 PM EST, same day
408-
forceRetentionOfCreatedValue: true,
408+
dateCreatedSourceOfTruth: 'frontmatter',
409409
convertToUTC: true,
410410
},
411411
},
@@ -435,5 +435,121 @@ ruleTest({
435435
convertToUTC: true,
436436
},
437437
},
438+
{
439+
testName: 'When changes are made prior to the YAML timestamp rule and user and Linter edits are the source of truth, update date modified',
440+
before: dedent`
441+
---
442+
tag: tag1
443+
modified: Tuesday, February 4th 2020, 6:00:00 pm
444+
created: Wednesday, January 1st 2020, 12:00:00 am
445+
location: "path"
446+
---
447+
`,
448+
after: dedent`
449+
---
450+
tag: tag1
451+
modified: Tuesday, February 4th 2020, 6:00:07 pm
452+
created: Wednesday, January 1st 2020, 12:00:00 am
453+
location: "path"
454+
---
455+
`,
456+
options: {
457+
dateCreatedKey: 'created',
458+
dateModifiedKey: 'modified',
459+
fileCreatedTime: '2020-01-01T00:00:00-00:00',
460+
fileModifiedTime: '2020-02-04T18:00:00-00:00',
461+
currentTime: moment('Tuesday, February 4th 2020, 6:00:07 pm', 'dddd, MMMM Do YYYY, h:mm:ss a'),
462+
alreadyModified: true,
463+
dateCreatedSourceOfTruth: 'frontmatter',
464+
dateModifiedSourceOfTruth: 'user or Linter edits',
465+
},
466+
},
467+
{
468+
testName: 'When no changes are made prior to the YAML timestamp rule, the created date format is different from the current format in settings, and user and Linter edits are the source of truth, update date modified',
469+
before: dedent`
470+
---
471+
tag: tag1
472+
modified: Tuesday, February 4th 2020, 6:00:00 pm
473+
created: Wednesday, January 1st 2020, 12:00 am
474+
location: "path"
475+
---
476+
`,
477+
after: dedent`
478+
---
479+
tag: tag1
480+
modified: Tuesday, February 4th 2020, 6:00:07 pm
481+
created: Wednesday, January 1st 2020, 12:00:00 am
482+
location: "path"
483+
---
484+
`,
485+
options: {
486+
dateCreatedKey: 'created',
487+
dateModifiedKey: 'modified',
488+
fileCreatedTime: '2020-01-01T00:00:00-00:00',
489+
fileModifiedTime: '2020-02-04T18:00:00-00:00',
490+
currentTime: moment('Tuesday, February 4th 2020, 6:00:07 pm', 'dddd, MMMM Do YYYY, h:mm:ss a'),
491+
alreadyModified: false,
492+
dateCreatedSourceOfTruth: 'frontmatter',
493+
dateModifiedSourceOfTruth: 'user or Linter edits',
494+
},
495+
},
496+
{
497+
testName: 'When no changes are made prior to the YAML timestamp rule, the modified date format is different from the current format in settings, and user and Linter edits are the source of truth, update date modified',
498+
before: dedent`
499+
---
500+
tag: tag1
501+
modified: Tuesday, February 4th 2020, 6:00 pm
502+
created: Wednesday, January 1st 2020, 12:00:00 am
503+
location: "path"
504+
---
505+
`,
506+
after: dedent`
507+
---
508+
tag: tag1
509+
modified: Tuesday, February 4th 2020, 6:00:07 pm
510+
created: Wednesday, January 1st 2020, 12:00:00 am
511+
location: "path"
512+
---
513+
`,
514+
options: {
515+
dateCreatedKey: 'created',
516+
dateModifiedKey: 'modified',
517+
fileCreatedTime: '2020-01-01T00:00:00-00:00',
518+
fileModifiedTime: '2020-02-04T18:00:00-00:00',
519+
currentTime: moment('Tuesday, February 4th 2020, 6:00:07 pm', 'dddd, MMMM Do YYYY, h:mm:ss a'),
520+
alreadyModified: false,
521+
dateCreatedSourceOfTruth: 'frontmatter',
522+
dateModifiedSourceOfTruth: 'user or Linter edits',
523+
},
524+
},
525+
{
526+
testName: 'When no changes are made prior to the YAML timestamp rule, the file system date modified is more than 5 seconds different from the date modified, and user and Linter edits are the source of truth, do not update date modified',
527+
before: dedent`
528+
---
529+
tag: tag1
530+
modified: Tuesday, February 4th 2020, 6:00:00 pm
531+
created: Wednesday, January 1st 2020, 12:00:00 am
532+
location: "path"
533+
---
534+
`,
535+
after: dedent`
536+
---
537+
tag: tag1
538+
modified: Tuesday, February 4th 2020, 6:00:00 pm
539+
created: Wednesday, January 1st 2020, 12:00:00 am
540+
location: "path"
541+
---
542+
`,
543+
options: {
544+
dateCreatedKey: 'created',
545+
dateModifiedKey: 'modified',
546+
fileCreatedTime: '2020-01-01T00:00:00-00:00',
547+
fileModifiedTime: '2020-02-05T18:00:00-00:00',
548+
currentTime: moment('Tuesday, February 5th 2020, 6:00:07 pm', 'dddd, MMMM Do YYYY, h:mm:ss a'),
549+
alreadyModified: false,
550+
dateCreatedSourceOfTruth: 'frontmatter',
551+
dateModifiedSourceOfTruth: 'user or Linter edits',
552+
},
553+
},
438554
],
439555
});

src/lang/locale/de.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -690,10 +690,6 @@ export default {
690690
'name': 'Schlüssel für das Erstellungsdatum',
691691
'description': 'Der YAML-Schlüssel, der für das Erstellungsdatum verwendet werden soll',
692692
},
693-
'force-retention-of-create-value': {
694-
'name': 'Erzwinge die Beibehaltung des Schlüsselwertes für das Erstellungsdatum',
695-
'description': 'Verwendet den Wert im YAML-Frontmatter für das Erstellungsdatum anstelle den Dateimetadaten, was nützlich ist, um zu verhindern, dass Änderungen an Dateimetadaten dazu führen, dass der Wert in einen anderen Wert geändert wird.',
696-
},
697693
'date-modified': {
698694
'name': 'Änderungsdatum',
699695
'description': 'Geben Sie das Datum ein, an dem die Datei zuletzt geändert wurde',

src/lang/locale/en.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -813,9 +813,13 @@ export default {
813813
'name': 'Date Created Key',
814814
'description': 'Which YAML key to use for creation date',
815815
},
816-
'force-retention-of-create-value': {
817-
'name': 'Force Date Created Key Value Retention',
818-
'description': 'Reuses the value in the YAML frontmatter for date created instead of the file metadata which is useful for preventing file metadata changes from causing the value to change to a different value.',
816+
'date-created-source-of-truth': {
817+
'name': 'Date Created Source of Truth',
818+
'description': 'Specifies where to get the date created value from if it is already present in the frontmatter.',
819+
},
820+
'date-modified-source-of-truth': {
821+
'name': 'Date Modified Source of Truth',
822+
'description': 'Specifies what way should be used to determine when the date modified should be updated if it is already present in the frontmatter.',
819823
},
820824
'date-modified': {
821825
'name': 'Date Modified',
@@ -918,6 +922,10 @@ export default {
918922
'after 15 seconds': 'After 15 seconds',
919923
'after 30 seconds': 'After 30 seconds',
920924
'after 1 minute': 'After 1 minute',
925+
// yaml-timestamp.ts
926+
'file system': 'File system',
927+
'frontmatter': 'YAML frontmatter',
928+
'user or Linter edits': 'Changes in Obsidian',
921929
// quote-style.ts
922930
'\'\'': '\'\'', // leave as is
923931
'‘’': '‘’', // leave as is

src/lang/locale/es.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -606,10 +606,6 @@ export default {
606606
'name': 'Clave de fecha de creación',
607607
'description': 'La clave de YAML para usar para la fecha de creación',
608608
},
609-
'force-retention-of-create-value': {
610-
'name': 'Forzar la fecha de creación de la retención del valor clave',
611-
'description': 'Reutiliza el valor en el frontmatter del YAML para la fecha de creación en lugar de los metadatos del archivo, lo que es útil para evitar que los cambios en los metadatos del archivo provoquen que el valor cambie a un valor diferente.',
612-
},
613609
'date-modified': {
614610
'name': 'Fecha modificada',
615611
'description': 'Inserte la fecha en que se modificó el archivo por última vez',

src/lang/locale/tr.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -686,10 +686,6 @@ export default {
686686
'name': 'Oluşturma Tarihi Anahtarı',
687687
'description': 'Oluşturma tarihi için hangi YAML anahtarını kullanacağı',
688688
},
689-
'force-retention-of-create-value': {
690-
'name': 'Oluşturma Tarihi Anahtar Değerinin Korunmasını Zorla',
691-
'description': 'Dosya metadatası yerine YAML ön maddesindeki tarihi yeniden kullanır, bu da dosya metadatasındaki değişikliklerin değerin farklı bir değere değişmesine neden olmasını önler.',
692-
},
693689
'date-modified': {
694690
'name': 'Değiştirme Tarihi',
695691
'description': 'Dosyanın son değiştirildiği tarihi ekler',

src/lang/locale/zh-cn.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -750,10 +750,6 @@ export default {
750750
'name': '创建日期键名',
751751
'description': '使用哪个 YAML 键来表示创建日期',
752752
},
753-
'force-retention-of-create-value': {
754-
'name': '强制保留创建日期值',
755-
'description': '沿用 YAML Front-matter 中已有的创建日期,忽略文档元数据。对于文档元数据更改(比如复制文件)导致的创建时间更改非常有用',
756-
},
757753
'date-modified': {
758754
'name': '修改日期',
759755
'description': '插入文件的最近一次的修改日期',

src/main.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,20 @@ export default class LinterPlugin extends Plugin {
145145
if (!('english-symbols-punctuation-after' in this.settings.ruleConfigs[rule.alias])) {
146146
this.settings.ruleConfigs[rule.alias]['english-symbols-punctuation-after'] = defaults['english-symbols-punctuation-after'];
147147
}
148+
} else if (rule.alias == 'yaml-timestamp') {
149+
const defaults = rule.getDefaultOptions();
150+
if ('force-retention-of-create-value' in this.settings.ruleConfigs[rule.alias]) {
151+
if (this.settings.ruleConfigs[rule.alias]['force-retention-of-create-value']) {
152+
this.settings.ruleConfigs[rule.alias]['date-created-source-of-truth'] = 'frontmatter';
153+
} else {
154+
this.settings.ruleConfigs[rule.alias]['date-created-source-of-truth'] = defaults['date-created-source-of-truth'];
155+
}
156+
}
157+
158+
if (!('date-modified-source-of-truth' in this.settings.ruleConfigs[rule.alias])) {
159+
this.settings.ruleConfigs[rule.alias]['date-modified-source-of-truth'] = defaults['date-modified-source-of-truth'];
160+
}
148161
}
149-
// TODO: add logic for moving force retention pjk
150162
}
151163

152164
this.updatePasteOverrideStatus();

src/rules/yaml-timestamp.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Options, RuleType} from '../rules';
2-
import RuleBuilder, {BooleanOptionBuilder, ExampleBuilder, MomentFormatOptionBuilder, OptionBuilderBase, TextOptionBuilder} from './rule-builder';
2+
import RuleBuilder, {BooleanOptionBuilder, DropdownOptionBuilder, ExampleBuilder, MomentFormatOptionBuilder, OptionBuilderBase, TextOptionBuilder} from './rule-builder';
33
import dedent from 'ts-dedent';
44
import {formatYAML, initYAML} from '../utils/yaml';
55
import {moment} from 'obsidian';
@@ -399,11 +399,21 @@ export default class YamlTimestamp extends RuleBuilder<YamlTimestampOptions> {
399399
descriptionKey: 'rules.yaml-timestamp.date-created-key.description',
400400
optionsKey: 'dateCreatedKey',
401401
}),
402-
new BooleanOptionBuilder({
402+
new DropdownOptionBuilder<YamlTimestampOptions, DateCreatedSourceOfTruth>({
403403
OptionsClass: YamlTimestampOptions,
404-
nameKey: 'rules.yaml-timestamp.force-retention-of-create-value.name',
405-
descriptionKey: 'rules.yaml-timestamp.force-retention-of-create-value.description',
406-
optionsKey: 'forceRetentionOfCreatedValue',
404+
nameKey: 'rules.yaml-timestamp.date-created-source-of-truth.name',
405+
descriptionKey: 'rules.yaml-timestamp.date-created-source-of-truth.description',
406+
optionsKey: 'dateCreatedSourceOfTruth',
407+
records: [
408+
{
409+
value: 'file system',
410+
description: 'The file system date created value is used to set the value of date created in the frontmatter',
411+
},
412+
{
413+
value: 'frontmatter',
414+
description: 'When a value is present in the frontmatter for date created, this value is used as the value for the date created',
415+
},
416+
],
407417
}),
408418
new BooleanOptionBuilder({
409419
OptionsClass: YamlTimestampOptions,
@@ -417,6 +427,22 @@ export default class YamlTimestamp extends RuleBuilder<YamlTimestampOptions> {
417427
descriptionKey: 'rules.yaml-timestamp.date-modified-key.description',
418428
optionsKey: 'dateModifiedKey',
419429
}),
430+
new DropdownOptionBuilder<YamlTimestampOptions, DateModifiedSourceOfTruth>({
431+
OptionsClass: YamlTimestampOptions,
432+
nameKey: 'rules.yaml-timestamp.date-modified-source-of-truth.name',
433+
descriptionKey: 'rules.yaml-timestamp.date-modified-source-of-truth.description',
434+
optionsKey: 'dateModifiedSourceOfTruth',
435+
records: [
436+
{
437+
value: 'file system',
438+
description: 'The file system date modified value is used to set the value of date modified in the frontmatter',
439+
},
440+
{
441+
value: 'user or Linter edits',
442+
description: 'When a value is present in the frontmatter for date modified, date modified is kept as is unless the user or the Linter makes a change to the file',
443+
},
444+
],
445+
}),
420446
new MomentFormatOptionBuilder({
421447
OptionsClass: YamlTimestampOptions,
422448
nameKey: 'rules.yaml-timestamp.format.name',

0 commit comments

Comments
 (0)