Repository: platers/obsidian-linter (ts)

Issue:
Date created gets reset when vault is cloned on another device
Since the date created uses the file creation date, it gets updated when the vault is cloned again. 

This can be fixed by only changing date created if it doesn't exist yet. I dont see a way to fix date modified.

Pull request:
only add date created if its not there
fixes #86 

Patch:
diff --git a/package.json b/package.json
index 7972774..08ac6a9 100644
--- a/package.json
+++ b/package.json
@@ -8,7 +8,7 @@
     "build": "rollup --config rollup.config.js --environment BUILD:production",
     "test": "jest",
     "docs": "ts-node --project ./tsconfig-docs.json ./src/docs.ts",
-    "compile": "npm run build && npm run docs && npm run test",
+    "compile": "npm run build && npm run docs && npm run lint && npm run test",
     "lint": "eslint . --ext .ts --fix"
   },
   "keywords": [],
diff --git a/src/rules.ts b/src/rules.ts
index 710fae6..d5adf70 100644
--- a/src/rules.ts
+++ b/src/rules.ts
@@ -449,8 +449,7 @@ export const rules: Rule[] = [
         text = initYAML(text);
 
         return formatYAML(text, (text) => {
-          if (options['Date Created'] === true) {
-            text = text.replace(/\ndate created:.*\n/, '\n');
+          if (options['Date Created'] === true && !text.match(/\ndate created:.*\n/)) {
             const yaml_end = text.indexOf('\n---');
             const formatted_date = moment(options['metadata: file created time']).format(options['Format']);
             text = insert(text, yaml_end, `\ndate created: ${formatted_date}`);


Test patch:
diff --git a/src/test.ts b/src/test.ts
index f7b8e47..5d5d9a4 100644
--- a/src/test.ts
+++ b/src/test.ts
@@ -377,6 +377,21 @@ describe('Move Footnotes to the bottom', () => {
     expect(rulesDict['move-footnotes-to-the-bottom'].apply(before)).toBe(after);
   });
 });
+describe('yaml timestamp', () => {
+  it('Doesnt add date created if already there', () => {
+    const before = dedent`
+    ---
+    date created: 2019-01-01
+    ---
+    `;
+    const after = dedent`
+    ---
+    date created: 2019-01-01
+    ---
+    `;
+    expect(rulesDict['yaml-timestamp'].apply(before, {'Date Created': true})).toBe(after);
+  });
+});
 
 describe('Disabled rules parsing', () => {
   it('No YAML', () => {