diff --git a/.gitignore b/.gitignore index 5fe00fea..61bf8cee 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ out node_modules .vscode-test/ *.vsix +.DS_Store \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 551a2663..8bebc71b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,5 @@ # Change Log -All notable changes to the "vscode-hugo-taxonomy" extension will be documented in this file. +## [0.0.1] - 2019-08-26 -Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file. - -## [Unreleased] - -- Initial release \ No newline at end of file +- Initial beta version \ No newline at end of file diff --git a/README.md b/README.md index c25e3654..d06d04df 100644 --- a/README.md +++ b/README.md @@ -1 +1,33 @@ -# VSCode Hugo Helpers \ No newline at end of file +# VSCode Front Matter Helpers + +> **Info**: Extension is still in development, but can already be tested out. + +## Available commands: + +**Front Matter: Create ** + - Creates a new and allows you to automatically include it into your post + +![Create tag or category](./assets/create-tag-category.gif) + +**Front Matter: Insert ** + - Inserts a selected into the front matter of your article/post/... + +![Insert tags or categories](./assets/insert-tag-category.gif) + +**Front Matter: Export all tags & categories to your settings** + - Export all the already used tags & categories in your articles/posts/... to your user settings + +## 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`. + +```json +{ + "frontMatter.taxonomy.tags": [], + "frontMatter.taxonomy.categories": [] +} +``` + +## 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/assets/create-tag-category.gif b/assets/create-tag-category.gif new file mode 100644 index 00000000..530b1aad Binary files /dev/null and b/assets/create-tag-category.gif differ diff --git a/assets/insert-tag-category.gif b/assets/insert-tag-category.gif new file mode 100644 index 00000000..4d12ad80 Binary files /dev/null and b/assets/insert-tag-category.gif differ diff --git a/package-lock.json b/package-lock.json index 39a6b939..bcc1c955 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "vscode-hugo-helpers", + "name": "vscode-front-matter", "version": "0.0.1", "lockfileVersion": 1, "requires": true, diff --git a/package.json b/package.json index 6af8a77b..c8cb1907 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "vscode-hugo-helpers", - "displayName": "vscode-hugo-helpers", - "description": "", + "name": "vscode-front-matter", + "displayName": "vscode-front-matter", + "description": "Simplifies front matter configuration for your articles/posts/...", "version": "0.0.1", "publisher": "eliostruyf", "engines": { @@ -10,43 +10,59 @@ "categories": [ "Other" ], + "keywords": [ + "Front Matter", + "Hugo", + "Jekyll", + "Taxonomy" + ], + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/estruyf/vscode-front-matter" + }, "activationEvents": [ - "onCommand:hugo.insertTags", - "onCommand:hugo.insertCategories", - "onCommand:hugo.createTag", - "onCommand:hugo.createCategory" + "onCommand:frontMatter.insertTags", + "onCommand:frontMatter.insertCategories", + "onCommand:frontMatter.createTag", + "onCommand:frontMatter.createCategory", + "onCommand:frontMatter.exportTaxonomy" ], "main": "./out/extension.js", "contributes": { "configuration": { - "title": "Hugo: Helpers Configuration", + "title": "Front Matter: Configuration", "properties": { - "hugo.taxonomy.tags": { + "frontMatter.taxonomy.tags": { "type": "array", - "description": "Specifies the tags which can be used for Hugo" + "description": "Specifies the tags which can be used in the Front Matter" }, - "hugo.taxonomy.categories": { + "frontMatter.taxonomy.categories": { "type": "array", - "description": "Specifies the categories which can be used for Hugo" + "description": "Specifies the categories which can be used in the Front Matter" } } }, "commands": [ { - "command": "hugo.insertTags", - "title": "Hugo: Insert tags" + "command": "frontMatter.insertTags", + "title": "Front Matter: Insert tags" }, { - "command": "hugo.insertCategories", - "title": "Hugo: Insert categories" + "command": "frontMatter.insertCategories", + "title": "Front Matter: Insert categories" }, { - "command": "hugo.createTag", - "title": "Hugo: Create tag" + "command": "frontMatter.createTag", + "title": "Front Matter: Create tag" }, { - "command": "hugo.createCategory", - "title": "Hugo: Create category" + "command": "frontMatter.createCategory", + "title": "Front Matter: Create category" + }, + { + "command": "frontMatter.exportTaxonomy", + "title": "Front Matter: Export all tags & categories to your settings" } ] }, diff --git a/src/commands/Hugo.ts b/src/commands/FrontMatter.ts similarity index 62% rename from src/commands/Hugo.ts rename to src/commands/FrontMatter.ts index 55d9004e..06da6363 100644 --- a/src/commands/Hugo.ts +++ b/src/commands/FrontMatter.ts @@ -3,7 +3,7 @@ import { TaxonomyType } from "../models"; import { CONFIG_KEY, ACTION_TAXONOMY_TAGS, ACTION_TAXONOMY_CATEGORIES } from "../constants/settings"; import * as matter from "gray-matter"; -export class Hugo { +export class FrontMatter { /** * Insert taxonomy @@ -125,6 +125,80 @@ export class Hugo { } } + /** + * Export the tags/categories front matter to the user settings + */ + public static async export() { + const config = vscode.workspace.getConfiguration(CONFIG_KEY); + + // Retrieve all the Markdown files + const mdFiles = await vscode.workspace.findFiles('**/*.md', "**/node_modules/**"); + if (!mdFiles) { + vscode.window.showInformationMessage(`No MD files found.`); + return; + } + + vscode.window.withProgress({ + location: vscode.ProgressLocation.Notification, + title: `Front Matter: exporting tags and categories`, + cancellable: false + }, async (progress) => { + // Fetching all tags and categories from MD files + let tags: string[] = []; + let categories: string[] = []; + + // Set the initial progress + const progressNr = mdFiles.length/100; + progress.report({ increment: 0}); + + let i = 0; + for (const file of mdFiles) { + progress.report({ increment: (++i/progressNr) }); + const mdFile = await vscode.workspace.openTextDocument(file); + if (mdFile) { + const txtData = mdFile.getText(); + if (txtData) { + try { + const article = matter(txtData); + if (article && article.data) { + const { data } = article; + const mdTags = data["tags"]; + const mdCategories = data["categories"]; + if (mdTags) { + tags = [...tags, ...mdTags]; + } + if (mdCategories) { + categories = [...categories, ...mdCategories]; + } + } + } catch (e) { + // Continue with the next file + } + } + } + } + + // Retrieve the currently known tags, and add the new ones + let crntTags: string[] = config.get(ACTION_TAXONOMY_TAGS) as string[]; + if (!crntTags) { crntTags = []; } + crntTags = [...crntTags, ...tags]; + // Update the tags and filter out the duplicates + crntTags = [...new Set(crntTags)]; + config.update(ACTION_TAXONOMY_TAGS, crntTags); + + // Retrieve the currently known tags, and add the new ones + let crntCategories: string[] = config.get(ACTION_TAXONOMY_CATEGORIES) as string[]; + if (!crntCategories) { crntCategories = []; } + crntCategories = [...crntCategories, ...categories]; + // Update the categories and filter out the duplicates + crntCategories = [...new Set(crntCategories)]; + config.update(ACTION_TAXONOMY_CATEGORIES, crntCategories); + + // Done + vscode.window.showInformationMessage(`Front Matter: export completed. Tags: ${crntTags.length} - Categories: ${crntCategories.length}.`); + }); + } + /** * Store the new information in the file * diff --git a/src/commands/index.ts b/src/commands/index.ts index 9cc6d374..692adf72 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -1 +1 @@ -export * from './Hugo'; +export * from './FrontMatter'; diff --git a/src/constants/settings.ts b/src/constants/settings.ts index c601a64e..e6d96141 100644 --- a/src/constants/settings.ts +++ b/src/constants/settings.ts @@ -1,4 +1,4 @@ -export const CONFIG_KEY = "hugo"; +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 diff --git a/src/extension.ts b/src/extension.ts index d07bfb4f..0432d3f4 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,23 +1,27 @@ import * as vscode from 'vscode'; -import { Hugo } from './commands'; +import { FrontMatter } from './commands'; import { TaxonomyType } from './models'; export function activate(context: vscode.ExtensionContext) { - let insertTags = vscode.commands.registerCommand('hugo.insertTags', () => { - Hugo.insert(TaxonomyType.Tag); + let insertTags = vscode.commands.registerCommand('frontMatter.insertTags', () => { + FrontMatter.insert(TaxonomyType.Tag); }); - let insertCategories = vscode.commands.registerCommand('hugo.insertCategories', () => { - Hugo.insert(TaxonomyType.Category); + let insertCategories = vscode.commands.registerCommand('frontMatter.insertCategories', () => { + FrontMatter.insert(TaxonomyType.Category); }); - let createTag = vscode.commands.registerCommand('hugo.createTag', () => { - Hugo.create(TaxonomyType.Tag); + let createTag = vscode.commands.registerCommand('frontMatter.createTag', () => { + FrontMatter.create(TaxonomyType.Tag); }); - let createCategory = vscode.commands.registerCommand('hugo.createCategory', () => { - Hugo.create(TaxonomyType.Category); + let createCategory = vscode.commands.registerCommand('frontMatter.createCategory', () => { + FrontMatter.create(TaxonomyType.Category); + }); + + let exportTaxonomy = vscode.commands.registerCommand('frontMatter.exportTaxonomy', () => { + FrontMatter.export(); }); context.subscriptions.push(insertTags); diff --git a/src/helpers/StringHelpers.ts b/src/helpers/StringHelpers.ts new file mode 100644 index 00000000..8a3267e6 --- /dev/null +++ b/src/helpers/StringHelpers.ts @@ -0,0 +1,3 @@ +export function firstToUpper(value: string) { + return value.charAt(0).toUpperCase() + value.slice(1); +} \ No newline at end of file diff --git a/src/helpers/index.ts b/src/helpers/index.ts new file mode 100644 index 00000000..a40b6607 --- /dev/null +++ b/src/helpers/index.ts @@ -0,0 +1 @@ +export * from './StringHelpers';