diff --git a/CHANGELOG.md b/CHANGELOG.md index 18cbed16..7d5da71e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Change Log +## [5.4.0] - 2021-11-05 + +### 🎨 Enhancements + +- [#166](https://github.com/estruyf/vscode-front-matter/issues/166): Added preview button to the panel base view +- [#167](https://github.com/estruyf/vscode-front-matter/issues/167): Allow to set the preview path per content type + ## [5.3.1] - 2021-10-29 ### 🐞 Fixes diff --git a/package-lock.json b/package-lock.json index 0bfdbd32..63d38f78 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "vscode-front-matter-beta", - "version": "5.3.1", + "version": "5.4.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -6386,6 +6386,12 @@ } } }, + "url-join-ts": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/url-join-ts/-/url-join-ts-1.0.5.tgz", + "integrity": "sha512-u+5gi7JyOwhj58ZKwkmkzFGHuepTpmwjqfUDGVjsJJstsCz63CJAINixgJaDcMbmuyWPJIxbtBpIfaDgOQ9KMQ==", + "dev": true + }, "use": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/use/-/use-3.1.1.tgz", diff --git a/package.json b/package.json index 314e3e59..88ac143b 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "displayName": "Front Matter", "description": "An essential Visual Studio Code extension when you want to manage the markdown pages of your static site like: Hugo, Jekyll, Hexo, NextJs, Gatsby, and many more...", "icon": "assets/frontmatter-teal-128x128.png", - "version": "5.3.1", + "version": "5.4.0", "preview": false, "publisher": "eliostruyf", "galleryBanner": { @@ -384,6 +384,14 @@ "type": "boolean", "default": false, "description": "Specify if you want to create a folder when creating new content." + }, + "previewPath": { + "type": [ + "null", + "string" + ], + "default": null, + "description": "Defines a custom preview path for the content type." } }, "additionalProperties": false, @@ -776,6 +784,7 @@ "ts-loader": "8.0.3", "tslint": "6.1.3", "typescript": "4.0.2", + "url-join-ts": "^1.0.5", "wc-react": "github:estruyf/wc-react", "webpack": "4.44.2", "webpack-cli": "3.3.12" 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..4b559335 100644 --- a/src/commands/Preview.ts +++ b/src/commands/Preview.ts @@ -6,6 +6,8 @@ import { Settings } from '../helpers'; import { PreviewSettings } from '../models'; import { format } from 'date-fns'; import { DateHelper } from '../helpers/DateHelper'; +import { Article } from '.'; +import { urlJoin } from 'url-join-ts'; export class Preview { @@ -32,12 +34,25 @@ export class Preview { const article = editor ? ArticleHelper.getFrontMatter(editor) : null; let slug = article?.data ? article.data.slug : ""; - if (settings.pathname) { + let pathname = settings.pathname; + if (article?.data) { + const contentType = ArticleHelper.getContentType(article.data); + if (contentType && contentType.previewPath) { + pathname = contentType.previewPath; + } + } + + if (!slug) { + slug = Article.getSlug(); + } + + if (pathname) { const articleDate = ArticleHelper.getDate(article); + try { - slug = join(format(articleDate || new Date(), DateHelper.formatUpdate(settings.pathname) as string), slug); + slug = join(format(articleDate || new Date(), DateHelper.formatUpdate(pathname) as string), slug); } catch (error) { - slug = join(settings.pathname, slug); + slug = join(pathname, slug); } } @@ -113,9 +128,9 @@ export class Preview {
- +
-