mirror of
https://github.com/estruyf/vscode-front-matter.git
synced 2026-08-06 08:52:47 +02:00
New command to update the date property
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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).
|
||||
Generated
+5
@@ -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",
|
||||
|
||||
+14
-3
@@ -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"
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
*
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
export const CONFIG_KEY = "frontMatter";
|
||||
|
||||
export const ACTION_TAXONOMY_TAGS = "taxonomy.tags";
|
||||
export const ACTION_TAXONOMY_CATEGORIES = "taxonomy.categories";
|
||||
export const ACTION_TAXONOMY_CATEGORIES = "taxonomy.categories";
|
||||
export const ACTION_DATE_FORMAT = "taxonomy.dateFormat";
|
||||
@@ -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() {}
|
||||
|
||||
Reference in New Issue
Block a user