mirror of
https://github.com/estruyf/vscode-front-matter.git
synced 2026-07-06 18:01:24 +02:00
Added update draft
This commit is contained in:
+5
-1
@@ -1,6 +1,10 @@
|
||||
# Change Log
|
||||
|
||||
## [0.0.5] - 2019-08-26
|
||||
## [0.0.6] - 2019-08-28
|
||||
|
||||
- Updated `package.json` file to include preview label
|
||||
|
||||
## [0.0.5] - 2019-08-27
|
||||
|
||||
- Updated title, description and logo
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
> **Info**: Extension is still in development, but can already be tested out.
|
||||
|
||||
This VSCode extension simplifies working with front matter of your markdown articles when using a static site generator like: Hugo, Jekyll, Hexo, NextJs, Gatsby, and many more... For example, with the extension you can keep a list of used tags, categories and add/remove them from your article.
|
||||
|
||||
## Available commands:
|
||||
|
||||
**Front Matter: Create <tag | category>**
|
||||
|
||||
@@ -4,7 +4,19 @@
|
||||
"description": "Simplifies working with front matter of your articles. Useful extension when you are using a static site generator like: Hugo, Jekyll, Hexo, NextJs, Gatsby, and many more...",
|
||||
"icon": "assets/front-matter.png",
|
||||
"version": "0.0.5",
|
||||
"preview": true,
|
||||
"publisher": "eliostruyf",
|
||||
"galleryBanner": {
|
||||
"color": "#011627",
|
||||
"theme": "dark"
|
||||
},
|
||||
"badges": [
|
||||
{
|
||||
"description": "version",
|
||||
"url": "https://img.shields.io/github/package-json/v/estruyf/vscode-front-matter?color=green&label=vscode-front-matter&style=flat-square",
|
||||
"href": "https://github.com/estruyf/vscode-front-matter"
|
||||
}
|
||||
],
|
||||
"engines": {
|
||||
"vscode": "^1.37.0"
|
||||
},
|
||||
@@ -16,6 +28,7 @@
|
||||
"Hugo",
|
||||
"Jekyll",
|
||||
"Gatsby",
|
||||
"Hexo",
|
||||
"Taxonomy"
|
||||
],
|
||||
"license": "MIT",
|
||||
@@ -24,6 +37,7 @@
|
||||
"url": "https://github.com/estruyf/vscode-front-matter"
|
||||
},
|
||||
"activationEvents": [
|
||||
"*",
|
||||
"onCommand:frontMatter.insertTags",
|
||||
"onCommand:frontMatter.insertCategories",
|
||||
"onCommand:frontMatter.createTag",
|
||||
|
||||
+76
-12
@@ -4,8 +4,9 @@ import { CONFIG_KEY, ACTION_TAXONOMY_TAGS, ACTION_TAXONOMY_CATEGORIES, ACTION_DA
|
||||
import * as matter from "gray-matter";
|
||||
import { format } from "date-fns";
|
||||
|
||||
|
||||
export class FrontMatter {
|
||||
|
||||
|
||||
/**
|
||||
* Insert taxonomy
|
||||
*
|
||||
@@ -18,9 +19,8 @@ export class FrontMatter {
|
||||
return;
|
||||
}
|
||||
|
||||
const fileTxt = editor.document.getText();
|
||||
const article = matter(fileTxt);
|
||||
if (!article || !article.data) {
|
||||
const article = this.getArticleData(editor);
|
||||
if (!article) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -104,9 +104,8 @@ export class FrontMatter {
|
||||
return;
|
||||
}
|
||||
|
||||
const fileTxt = editor.document.getText();
|
||||
const article = matter(fileTxt);
|
||||
if (!article || !article.data) {
|
||||
const article = this.getArticleData(editor);
|
||||
if (!article) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -205,6 +204,9 @@ export class FrontMatter {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the article date
|
||||
*/
|
||||
public static async setDate() {
|
||||
const config = vscode.workspace.getConfiguration(CONFIG_KEY);
|
||||
const editor = vscode.window.activeTextEditor;
|
||||
@@ -212,9 +214,8 @@ export class FrontMatter {
|
||||
return;
|
||||
}
|
||||
|
||||
const fileTxt = editor.document.getText();
|
||||
const article = matter(fileTxt);
|
||||
if (!article || !article.data) {
|
||||
const article = this.getArticleData(editor);
|
||||
if (!article) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -233,15 +234,78 @@ export class FrontMatter {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the text of the status bar
|
||||
*
|
||||
* @param frontMatterStatusBar
|
||||
*/
|
||||
public static async showDraftStatus(frontMatterSB: vscode.StatusBarItem) {
|
||||
const draftMsg = "in draft";
|
||||
const publishMsg = "to publish";
|
||||
|
||||
let editor = vscode.window.activeTextEditor;
|
||||
if (editor && editor.document && editor.document.languageId.toLowerCase() === "markdown") {
|
||||
try {
|
||||
const article = this.getArticleData(editor);
|
||||
if (article && typeof article.data["draft"] !== "undefined") {
|
||||
console.log(`Draft status: ${article.data["draft"]}`);
|
||||
if (article.data["draft"] === true) {
|
||||
frontMatterSB.text = `$(book) ${draftMsg}`;
|
||||
frontMatterSB.show();
|
||||
} else if (article.data["draft"] === false) {
|
||||
frontMatterSB.text = `$(book) ${publishMsg}`;
|
||||
frontMatterSB.show();
|
||||
}
|
||||
return;
|
||||
}
|
||||
} catch (e) {
|
||||
// Nothing to do
|
||||
}
|
||||
}
|
||||
|
||||
frontMatterSB.hide();
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle the page its draft mode
|
||||
*/
|
||||
public static async toggleDraft() {
|
||||
const editor = vscode.window.activeTextEditor;
|
||||
if (!editor) {
|
||||
return;
|
||||
}
|
||||
const article = this.getArticleData(editor);
|
||||
if (!article) {
|
||||
return;
|
||||
}
|
||||
|
||||
const newDraftStatus = !article.data["draft"];
|
||||
article.data["draft"] = newDraftStatus;
|
||||
this.updatePage(editor, article);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the contents of the current article
|
||||
*
|
||||
* @param editor
|
||||
*/
|
||||
private static getArticleData(editor: vscode.TextEditor) {
|
||||
const article = matter(editor.document.getText());
|
||||
if (article && article.data) {
|
||||
return article;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the new information in the file
|
||||
*
|
||||
* @param editor
|
||||
* @param article
|
||||
*/
|
||||
private static updatePage(editor: vscode.TextEditor, article: matter.GrayMatterFile<string>) {
|
||||
private static async updatePage(editor: vscode.TextEditor, article: matter.GrayMatterFile<string>) {
|
||||
const newMarkdown = matter.stringify(article.content, article.data);
|
||||
const nrOfLines = editor.document.lineCount as number;
|
||||
editor.edit(builder => builder.replace(new vscode.Range(new vscode.Position(0, 0), new vscode.Position(nrOfLines, 0)), newMarkdown));
|
||||
await editor.edit(builder => builder.replace(new vscode.Range(new vscode.Position(0, 0), new vscode.Position(nrOfLines, 0)), newMarkdown));
|
||||
}
|
||||
}
|
||||
+43
-7
@@ -2,7 +2,10 @@ import * as vscode from 'vscode';
|
||||
import { FrontMatter } from './commands';
|
||||
import { TaxonomyType } from './models';
|
||||
|
||||
export function activate(context: vscode.ExtensionContext) {
|
||||
let frontMatterStatusBar: vscode.StatusBarItem;
|
||||
let debouncer: { (fnc: any, time: number): void; };
|
||||
|
||||
export function activate({ subscriptions }: vscode.ExtensionContext) {
|
||||
|
||||
let insertTags = vscode.commands.registerCommand('frontMatter.insertTags', () => {
|
||||
FrontMatter.insert(TaxonomyType.Tag);
|
||||
@@ -28,12 +31,45 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
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);
|
||||
const toggleDraftCommand = 'frontMatter.toggleDraft';
|
||||
const toggleDraft = vscode.commands.registerCommand(toggleDraftCommand, async () => {
|
||||
await FrontMatter.toggleDraft();
|
||||
triggerShowDraftStatus();
|
||||
});
|
||||
|
||||
// Create the status bar
|
||||
frontMatterStatusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 100);
|
||||
frontMatterStatusBar.command = toggleDraftCommand;
|
||||
subscriptions.push(frontMatterStatusBar);
|
||||
debouncer = debounceShowDraftTrigger();
|
||||
// Register listeners that make sure the status bar
|
||||
subscriptions.push(vscode.window.onDidChangeActiveTextEditor(triggerShowDraftStatus));
|
||||
subscriptions.push(vscode.window.onDidChangeTextEditorSelection(triggerShowDraftStatus));
|
||||
// Automatically run the command
|
||||
triggerShowDraftStatus();
|
||||
|
||||
// Subscribe all commands
|
||||
subscriptions.push(insertTags);
|
||||
subscriptions.push(insertCategories);
|
||||
subscriptions.push(createTag);
|
||||
subscriptions.push(createCategory);
|
||||
subscriptions.push(exportTaxonomy);
|
||||
subscriptions.push(setDate);
|
||||
subscriptions.push(toggleDraft);
|
||||
}
|
||||
|
||||
export function deactivate() {}
|
||||
|
||||
const triggerShowDraftStatus = () => {
|
||||
debouncer(() => { FrontMatter.showDraftStatus(frontMatterStatusBar); }, 1000);
|
||||
};
|
||||
|
||||
const debounceShowDraftTrigger = () => {
|
||||
let timeout: NodeJS.Timeout;
|
||||
|
||||
return (fnc: any, time: number) => {
|
||||
const functionCall = (...args: any[]) => fnc.apply(args);
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(functionCall, time);
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user