From 9c9cbb7dcb22e34aa4ce58b1700b7c40ae9dd6fb Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Tue, 2 Nov 2021 11:56:40 +0100 Subject: [PATCH 1/7] #166 - Add preview button to panel when no markdown file is opened --- src/panelWebView/components/BaseView.tsx | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/panelWebView/components/BaseView.tsx b/src/panelWebView/components/BaseView.tsx index e036d2b0..17c0a51a 100644 --- a/src/panelWebView/components/BaseView.tsx +++ b/src/panelWebView/components/BaseView.tsx @@ -27,6 +27,10 @@ const BaseView: React.FunctionComponent = ({settings, folderAndF MessageHelper.sendMessage(CommandToCode.createContent); }; + const openPreview = () => { + MessageHelper.sendMessage(CommandToCode.openPreview); + }; + return (
@@ -37,6 +41,7 @@ const BaseView: React.FunctionComponent = ({settings, folderAndF +
From 9e8533fbb8cf33bc1fa423661a08a1bf0ce237c2 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Tue, 2 Nov 2021 11:58:43 +0100 Subject: [PATCH 2/7] Updated changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18cbed16..f7004412 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Change Log +## [X.X.X] - 2021-11-XX + +### 🎨 Enhancements + +- [#166](https://github.com/estruyf/vscode-front-matter/issues/166): Added preview button to the panel base view + ## [5.3.1] - 2021-10-29 ### 🐞 Fixes From 6625b69170165b44a08b160392fd6477a8579cd5 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Tue, 2 Nov 2021 13:48:52 +0100 Subject: [PATCH 3/7] #166 - Filename and folder logic for slug --- src/commands/Article.ts | 30 ++++++++++++++++++++++++++++-- src/commands/Preview.ts | 9 +++++++-- src/constants/DefaultFields.ts | 3 ++- src/explorerView/ExplorerView.ts | 9 +++++++++ 4 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/commands/Article.ts b/src/commands/Article.ts index 5fe71708..b169b87d 100644 --- a/src/commands/Article.ts +++ b/src/commands/Article.ts @@ -5,11 +5,12 @@ import { format } from "date-fns"; import { ArticleHelper, Settings, SlugHelper } from '../helpers'; import matter = require('gray-matter'); import { Notifications } from '../helpers/Notifications'; -import { extname, basename } from 'path'; +import { extname, basename, parse, dirname } from 'path'; import { COMMAND_NAME, DefaultFields } from '../constants'; import { DashboardData } from '../models/DashboardData'; import { ExplorerView } from '../explorerView/ExplorerView'; import { DateHelper } from '../helpers/DateHelper'; +import { parseWinPath } from '../helpers/parseWinPath'; export class Article { @@ -183,7 +184,7 @@ export class Article { await vscode.workspace.fs.rename(editor.document.uri, vscode.Uri.file(newPath), { overwrite: false }); - } catch (e) { + } catch (e: any) { Notifications.error(`Failed to rename file: ${e?.message || e}`); } } @@ -191,6 +192,31 @@ export class Article { } } + /** + * Retrieve the slug from the front matter + */ + public static getSlug() { + const editor = vscode.window.activeTextEditor; + if (!editor) { + return; + } + + const file = parseWinPath(editor.document.fileName); + + if (!file.endsWith(`.md`) && !file.endsWith(`.mdx`)) { + return; + } + + const parsedFile = parse(file); + + if (parsedFile.name.toLowerCase() !== "index") { + return parsedFile.name; + } + + const folderName = basename(dirname(file)); + return folderName; + } + /** * Toggle the page its draft mode */ diff --git a/src/commands/Preview.ts b/src/commands/Preview.ts index b38a1764..420d184a 100644 --- a/src/commands/Preview.ts +++ b/src/commands/Preview.ts @@ -6,6 +6,7 @@ import { Settings } from '../helpers'; import { PreviewSettings } from '../models'; import { format } from 'date-fns'; import { DateHelper } from '../helpers/DateHelper'; +import { Article } from '.'; export class Preview { @@ -32,6 +33,10 @@ export class Preview { const article = editor ? ArticleHelper.getFrontMatter(editor) : null; let slug = article?.data ? article.data.slug : ""; + if (!slug) { + slug = Article.getSlug(); + } + if (settings.pathname) { const articleDate = ArticleHelper.getDate(article); try { @@ -113,9 +118,9 @@ export class Preview {
- +
-