Repository: platers/obsidian-linter
Pull request #29 (2021-09-08 01:02:20)

yaml timestamp uses file metadata
Passes metadata through options.
Overwrites previous timestamps.


Patch:
diff --git a/docs/docs.ts b/docs/docs.ts
index 4a6bd1b..1325478 100644
--- a/docs/docs.ts
+++ b/docs/docs.ts
@@ -1,10 +1,7 @@
-import MockDate from 'mockdate';
-MockDate.set('2020-01-01');
 import {readFileSync, writeFileSync} from 'fs';
 import dedent from 'ts-dedent';
 import {rules} from '../rules';
 
-
 const autogen_warning = '<!--- This file was automatically generated. See docs.ts and *_template.md files for the source. -->';
 
 // README
diff --git a/docs/rules.md b/docs/rules.md
index 6ab22f5..1b4523a 100644
--- a/docs/rules.md
+++ b/docs/rules.md
@@ -149,7 +149,7 @@ After:
 
 Alias: `yaml-timestamp`
 
-Keep track of the date the file was last edited in the YAML front matter. 
+Keep track of the date the file was last edited in the YAML front matter. Gets dates from file metadata.
 
 Options:
 - format: [date format](https://momentjs.com/docs/#/displaying/format/), default=`"dddd, MMMM Do YYYY, h:mm:ss a"`
@@ -169,7 +169,7 @@ After:
 ```markdown
 ---
 date created: Wednesday, January 1st 2020, 12:00:00 am
-date updated: Wednesday, January 1st 2020, 12:00:00 am
+date updated: Thursday, January 2nd 2020, 12:00:00 am
 ---
 # H1
 ```
diff --git a/jest.config.ts b/jest.config.ts
new file mode 100644
index 0000000..ec4b156
--- /dev/null
+++ b/jest.config.ts
@@ -0,0 +1,1 @@
+process.env.TZ = 'UTC';
diff --git a/main.ts b/main.ts
index de4fd8a..bb5e464 100644
--- a/main.ts
+++ b/main.ts
@@ -2,6 +2,7 @@ import {App, Editor, MarkdownView, Notice, Plugin, PluginSettingTab, Setting} fr
 import dedent from 'ts-dedent';
 import {parseOptions, rulesDict} from './rules';
 import Diff from 'diff';
+import moment from 'moment';
 
 interface LinterSettings {
     enabledRules: string;
@@ -67,6 +68,7 @@ export default class LinterPlugin extends Plugin {
     runLinter(editor: Editor) {
       console.log('running linter');
 
+      const file = this.app.workspace.getActiveFile();
       const oldText = editor.getValue();
       let newText = oldText;
       const enabledRules = this.settings.enabledRules.split('\n');
@@ -80,13 +82,12 @@ export default class LinterPlugin extends Plugin {
         const ruleName = line.split(/\s+/)[0];
 
         if (ruleName in rulesDict) {
-          const options: { [id: string]: string; } = parseOptions(line);
-
-          if (options) {
-            newText = rulesDict[ruleName].apply(newText, options);
-          } else {
-            newText = rulesDict[ruleName].apply(newText);
-          }
+          const options: { [id: string]: string; } =
+            Object.assign({
+              'metadata: file created time': moment(file.stat.ctime).format(),
+              'metadata: file modified time': moment(file.stat.mtime).format(),
+            }, parseOptions(line));
+          newText = rulesDict[ruleName].apply(newText, options);
         } else {
           new Notice(`Rule ${ruleName} not recognized`);
         }
diff --git a/package-lock.json b/package-lock.json
index f7db3a8..a99d735 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -29,7 +29,6 @@
         "eslint": "^7.32.0",
         "eslint-config-google": "^0.14.0",
         "jest": "^27.0.6",
-        "mockdate": "^3.0.5",
         "obsidian": "^0.12.0",
         "rollup": "^2.32.1",
         "ts-node": "^10.1.0",
@@ -5502,12 +5501,6 @@
       "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==",
       "dev": true
     },
-    "node_modules/mockdate": {
-      "version": "3.0.5",
-      "resolved": "https://registry.npmjs.org/mockdate/-/mockdate-3.0.5.tgz",
-      "integrity": "sha512-iniQP4rj1FhBdBYS/+eQv7j1tadJ9lJtdzgOpvsOHng/GbcDh2Fhdeq+ZRldrPYdXvCyfFUmFeEwEGXZB5I/AQ==",
-      "dev": true
-    },
     "node_modules/moment": {
       "version": "2.29.1",
       "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz",
@@ -10991,12 +10984,6 @@
       "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==",
       "dev": true
     },
-    "mockdate": {
-      "version": "3.0.5",
-      "resolved": "https://registry.npmjs.org/mockdate/-/mockdate-3.0.5.tgz",
-      "integrity": "sha512-iniQP4rj1FhBdBYS/+eQv7j1tadJ9lJtdzgOpvsOHng/GbcDh2Fhdeq+ZRldrPYdXvCyfFUmFeEwEGXZB5I/AQ==",
-      "dev": true
-    },
     "moment": {
       "version": "2.29.1",
       "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz",
diff --git a/package.json b/package.json
index 6bd5e1f..3befbd3 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 ./docs/docs.ts",
-    "compile": "npm run lint && npm run build && npm run docs && npm run test",
+    "compile": "npm run build && npm run docs && npm run test",
     "lint": "eslint . --ext .ts --fix"
   },
   "keywords": [],
@@ -30,7 +30,6 @@
     "eslint": "^7.32.0",
     "eslint-config-google": "^0.14.0",
     "jest": "^27.0.6",
-    "mockdate": "^3.0.5",
     "obsidian": "^0.12.0",
     "rollup": "^2.32.1",
     "ts-node": "^10.1.0",
diff --git a/rules.ts b/rules.ts
index 8674d75..374a9d6 100644
--- a/rules.ts
+++ b/rules.ts
@@ -192,7 +192,7 @@ export const rules: Rule[] = [
   ),
   new Rule(
       'YAML Timestamp',
-      'Keep track of the date the file was last edited in the YAML front matter. ',
+      'Keep track of the date the file was last edited in the YAML front matter. Gets dates from file metadata.',
       (text: string, options = {}) => {
         options = Object.assign({
           'format': 'dddd, MMMM Do YYYY, h:mm:ss a',
@@ -201,15 +201,17 @@ export const rules: Rule[] = [
         }, options);
 
         text = initYAML(text);
-        const formatted_date = moment().format(options['format']);
 
-        if (options['dateCreated'] === 'true' && !text.match(/\ndate created:.*\n/)) {
+        if (options['dateCreated'] === 'true') {
+          text = text.replace(/\ndate created:.*\n/, '\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}`);
         }
         if (options['dateUpdated'] === 'true') {
           text = text.replace(/\ndate updated:.*\n/, '\n');
           const yaml_end = text.indexOf('\n---');
+          const formatted_date = moment(options['metadata: file modified time']).format(options['format']);
           text = insert(text, yaml_end, `\ndate updated: ${formatted_date}`);
         }
         return text;
@@ -223,10 +225,14 @@ export const rules: Rule[] = [
             dedent`
         ---
         date created: Wednesday, January 1st 2020, 12:00:00 am
-        date updated: Wednesday, January 1st 2020, 12:00:00 am
+        date updated: Thursday, January 2nd 2020, 12:00:00 am
         ---
         # H1
         `,
+            {
+              'metadata: file created time': '2020-01-01T00:00:00-00:00',
+              'metadata: file modified time': '2020-01-02T00:00:00-00:00',
+            },
         ),
         new Example(
             'dateCreated option is false',
@@ -239,7 +245,11 @@ export const rules: Rule[] = [
         ---
         # H1
         `,
-            {dateCreated: 'false'},
+            {
+              'dateCreated': 'false',
+              'metadata: file created time': '2020-01-01T00:00:00-00:00',
+              'metadata: file modified time': '2020-01-01T00:00:00-00:00',
+            },
         ),
       ],
       [


Test patch:
diff --git a/test.ts b/test.ts
index 5610907..5e428ab 100644
--- a/test.ts
+++ b/test.ts
@@ -1,5 +1,3 @@
-import MockDate from 'mockdate';
-MockDate.set(new Date(2020, 0, 1));
 import dedent from 'ts-dedent';
 import {rules, Rule, rulesDict, Example, parseOptions} from './rules';