From 92972ed6c907a2e197af3ca508d8e412b28adb8a Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Mon, 26 Aug 2019 21:04:17 +0200 Subject: [PATCH] New command to update the date property --- CHANGELOG.md | 4 ++++ README.md | 13 +++++++++++++ package-lock.json | 5 +++++ package.json | 17 ++++++++++++++--- src/commands/FrontMatter.ts | 31 ++++++++++++++++++++++++++++++- src/constants/settings.ts | 3 ++- src/extension.ts | 6 ++++++ 7 files changed, 74 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1769e23..ef35863b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## [0.0.4] - 2019-08-26 + +- Added set date command + ## [0.0.3] - 2019-08-26 - Support added for `*.markdown` files diff --git a/README.md b/README.md index d06d04df..7da5eee9 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,12 @@ **Front Matter: Export all tags & categories to your settings** - Export all the already used tags & categories in your articles/posts/... to your user settings +**Front Matter: Set current date** + + - Update the `date` property of the current article/post/... to the current date & time. + +> **Optional**: if you want, you can specify the format of the date property by adding your own preference in your settings. Settings key: `frontMatter.taxonomy.dateFormat`. Check [date-fns formating](https://date-fns.org/v2.0.1/docs/format) for more information which patterns you can use. + ## Where is the data stored? The tags and categories are stored in the project VSCode user settings. You can find them back under: `.vscode/settings.json`. @@ -28,6 +34,13 @@ The tags and categories are stored in the project VSCode user settings. You can } ``` +## Usage + +- Start by opening the command prompt: + - Windows ⇧+ctrl+P + - Mac: ⇧+⌘+P +- Use one of the commands from above + ## Feedback / issues / ideas Please submit them via creating an issue in the project repository: [issue list](https://github.com/estruyf/vscode-front-matter/issues). \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index ffefa465..ae3b5ad1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -995,6 +995,11 @@ "integrity": "sha1-GzN5LhHpFKL9bW7WRHRkRE5fpkA=", "dev": true }, + "date-fns": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.0.1.tgz", + "integrity": "sha512-C14oTzTZy8DH1Eq8N78owrCWvf3+cnJw88BTK/N3DYWVxDJuJzPaNdplzYxDYuuXXGvqBcO4Vy5SOrwAooXSWw==" + }, "date-now": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/date-now/-/date-now-0.1.4.tgz", diff --git a/package.json b/package.json index 8c3c05f1..690c1a1a 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,8 @@ "onCommand:frontMatter.insertCategories", "onCommand:frontMatter.createTag", "onCommand:frontMatter.createCategory", - "onCommand:frontMatter.exportTaxonomy" + "onCommand:frontMatter.exportTaxonomy", + "onCommand:frontMatter.setDate" ], "main": "./dist/extension", "contributes": { @@ -40,6 +41,10 @@ "frontMatter.taxonomy.categories": { "type": "array", "description": "Specifies the categories which can be used in the Front Matter" + }, + "frontMatter.taxonomy.dateFormat": { + "type": "string", + "markdownDescription": "Specify the date format for your articles. Check [date-fns formating](https://date-fns.org/v2.0.1/docs/format) for more information." } } }, @@ -63,6 +68,10 @@ { "command": "frontMatter.exportTaxonomy", "title": "Front Matter: Export all tags & categories to your settings" + }, + { + "command": "frontMatter.setDate", + "title": "Front Matter: Set current date" } ] }, @@ -84,9 +93,11 @@ "typescript": "^3.3.1", "vscode-test": "^1.0.2", "webpack": "^4.39.2", - "webpack-cli": "^3.3.7" + "webpack-cli": "^3.3.7", + "date-fns": "2.0.1", + "gray-matter": "4.0.2" }, "dependencies": { - "gray-matter": "4.0.2" + } } diff --git a/src/commands/FrontMatter.ts b/src/commands/FrontMatter.ts index 0333df79..917c3a01 100644 --- a/src/commands/FrontMatter.ts +++ b/src/commands/FrontMatter.ts @@ -1,7 +1,8 @@ import * as vscode from 'vscode'; import { TaxonomyType } from "../models"; -import { CONFIG_KEY, ACTION_TAXONOMY_TAGS, ACTION_TAXONOMY_CATEGORIES } from "../constants/settings"; +import { CONFIG_KEY, ACTION_TAXONOMY_TAGS, ACTION_TAXONOMY_CATEGORIES, ACTION_DATE_FORMAT } from "../constants/settings"; import * as matter from "gray-matter"; +import { format } from "date-fns"; export class FrontMatter { @@ -204,6 +205,34 @@ export class FrontMatter { }); } + public static async setDate() { + const config = vscode.workspace.getConfiguration(CONFIG_KEY); + const editor = vscode.window.activeTextEditor; + if (!editor) { + return; + } + + const fileTxt = editor.document.getText(); + const article = matter(fileTxt); + if (!article || !article.data) { + return; + } + + const dateFormat = config.get(ACTION_DATE_FORMAT) as string; + try { + if (dateFormat && typeof dateFormat === "string") { + article.data["date"] = format(new Date(), dateFormat); + } else { + article.data["date"] = new Date(); + } + + this.updatePage(editor, article); + } catch (e) { + vscode.window.showErrorMessage(`Front Matter: Something failed while parsing the date format. Check your "${CONFIG_KEY}${ACTION_DATE_FORMAT}" setting.`); + console.log(e.message); + } + } + /** * Store the new information in the file * diff --git a/src/constants/settings.ts b/src/constants/settings.ts index e6d96141..52c87915 100644 --- a/src/constants/settings.ts +++ b/src/constants/settings.ts @@ -1,4 +1,5 @@ export const CONFIG_KEY = "frontMatter"; export const ACTION_TAXONOMY_TAGS = "taxonomy.tags"; -export const ACTION_TAXONOMY_CATEGORIES = "taxonomy.categories"; \ No newline at end of file +export const ACTION_TAXONOMY_CATEGORIES = "taxonomy.categories"; +export const ACTION_DATE_FORMAT = "taxonomy.dateFormat"; \ No newline at end of file diff --git a/src/extension.ts b/src/extension.ts index 0432d3f4..3c4503c6 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -23,11 +23,17 @@ export function activate(context: vscode.ExtensionContext) { let exportTaxonomy = vscode.commands.registerCommand('frontMatter.exportTaxonomy', () => { FrontMatter.export(); }); + + let setDate = vscode.commands.registerCommand('frontMatter.setDate', () => { + FrontMatter.setDate(); + }); context.subscriptions.push(insertTags); context.subscriptions.push(insertCategories); context.subscriptions.push(createTag); context.subscriptions.push(createCategory); + context.subscriptions.push(exportTaxonomy); + context.subscriptions.push(setDate); } export function deactivate() {}