Skip to content

Commit 4ef12b1

Browse files
authored
Merge pull request #1415 from pjkaufman/master
Fix Escaped Characters in Timestamp Format Resulting in Forced Update Even when It Should Not
2 parents 1877e2b + 39361a0 commit 4ef12b1

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

__tests__/yaml-timestamp.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,5 +551,28 @@ ruleTest({
551551
dateModifiedSourceOfTruth: 'user or Linter edits',
552552
},
553553
},
554+
{ // accounts for https://github.com/platers/obsidian-linter/issues/1411
555+
testName: 'When the format for the file has escaped characters, if it matches the original format and the source of truth is not filesystem, then it should not update the date modified',
556+
before: dedent`
557+
---
558+
modified_on: "[[2025-10-11]]"
559+
---
560+
`,
561+
after: dedent`
562+
---
563+
modified_on: "[[2025-10-11]]"
564+
---
565+
`,
566+
options: {
567+
dateCreated: false,
568+
format: '"[[[]YYYY-MM-DD[]]]"',
569+
dateModifiedKey: 'modified_on',
570+
fileCreatedTime: '2020-01-01T00:00:00-00:00',
571+
fileModifiedTime: '2020-02-05T18:00:00-00:00',
572+
currentTime: moment('Tuesday, February 5th 2026, 6:00:07 pm', 'dddd, MMMM Do YYYY, h:mm:ss a'),
573+
alreadyModified: false,
574+
dateModifiedSourceOfTruth: 'user or Linter edits',
575+
},
576+
},
554577
],
555578
});

src/rules/yaml-timestamp.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,12 @@ export default class YamlTimestamp extends RuleBuilder<YamlTimestampOptions> {
132132
textModified = true;
133133
}
134134
} else if (options.dateCreatedSourceOfTruth != 'frontmatter') {
135-
const createdDateTime = moment(createdDateString, options.format, options.locale, true);
136-
if (createdDateTime == undefined || !createdDateTime.isValid()) {
135+
// due to how moment validation works for formats, we must check if the output string is the same for the provided value since
136+
// parsing when it has escaped characters in it does not result in a valid value
137+
// see https://github.com/platers/obsidian-linter/issues/1411
138+
const createdDateTime = moment(createdDateString, options.format, options.locale);
139+
// to keep backwards compatibility, we will just go ahead and say that the format output must be equal if the format is not blank
140+
if (createdDateTime == undefined || !createdDateTime.isValid() || (options.format !== '' && createdDateTime.format(options.format) != createdDateString)) {
137141
text = text.replace(
138142
created_match,
139143
escapeDollarSigns(created_date_line) + '\n',
@@ -161,14 +165,18 @@ export default class YamlTimestamp extends RuleBuilder<YamlTimestampOptions> {
161165

162166
const keyWithValueFound = modified_match.test(text);
163167
if (keyWithValueFound) {
164-
const modifiedDateTime = moment(this.getYAMLTimestampString(text, modified_match, options.dateModifiedKey), options.format, options.locale, true);
168+
// due to how moment validation works for formats, we must check if the output string is the same for the provided value since
169+
// parsing when it has escaped characters in it does not result in a valid value
170+
// see https://github.com/platers/obsidian-linter/issues/1411
171+
const originalString = this.getYAMLTimestampString(text, modified_match, options.dateModifiedKey);
172+
const modifiedDateTime = moment(originalString, options.format, options.locale);
165173
// conditions when update happens for date modified if the key already exists:
166174
// 1. the text has been modified
167175
// 2. the modified date in the frontmatter is not the same locale or format as the settings
168176
// 3. the source of truth is not when a user or the Linter makes a change to the file and
169177
// there is a more than 5 second difference between the date modified in the frontmatter and
170178
// the filesystem
171-
if (textModified || modifiedDateTime == undefined || !modifiedDateTime.isValid() ||
179+
if (textModified || modifiedDateTime == undefined || !modifiedDateTime.isValid() || modifiedDateTime.format(options.format) != originalString ||
172180
(options.dateModifiedSourceOfTruth != 'user or Linter edits' && this.getTimeDifferenceInSeconds(modifiedDateTime, modified_date, options) > 5)
173181
) {
174182
text = text.replace(

0 commit comments

Comments
 (0)