From 1a6acce77fea7fb5bd15fed7f32e86bff7965ba7 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Fri, 10 Jun 2022 10:52:10 +0200 Subject: [PATCH] #350 - New `previewPath` property for page folders --- CHANGELOG.md | 1 + package.json | 8 ++++++++ src/commands/Preview.ts | 27 +++++++++++++++++++++++++-- src/models/ContentFolder.ts | 1 + 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 852aaf39..0ee07b5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - [#307](https://github.com/estruyf/vscode-front-matter/issues/307): New `list` field which allows to create a list of items - [#345](https://github.com/estruyf/vscode-front-matter/issues/345): Media dashboard UI improvements to visualize the content and public folders - [#349](https://github.com/estruyf/vscode-front-matter/issues/349): New `slug` field which allows you to manage the slug of your post from the Front Matter panel +- [#350](https://github.com/estruyf/vscode-front-matter/issues/350): New `previewPath` property for the `frontMatter.content.pageFolders` setting. This allows you to specify a section prefix for all content created in that directory. - [#351](https://github.com/estruyf/vscode-front-matter/issues/351): New `template` property for content types which allows you to combine templates and content types for content creation ### 🐞 Fixes diff --git a/package.json b/package.json index 8a1bad70..793478bc 100644 --- a/package.json +++ b/package.json @@ -183,6 +183,14 @@ "type": "boolean", "default": false, "description": "Exclude sub-directories" + }, + "previewPath": { + "type": [ + "null", + "string" + ], + "default": null, + "description": "Defines a custom preview path for the folder." } }, "additionalProperties": false, diff --git a/src/commands/Preview.ts b/src/commands/Preview.ts index 8dc3298f..e372a93d 100644 --- a/src/commands/Preview.ts +++ b/src/commands/Preview.ts @@ -3,13 +3,14 @@ import { SETTING_PREVIEW_HOST, SETTING_PREVIEW_PATHNAME, CONTEXT, TelemetryEvent import { ArticleHelper } from './../helpers/ArticleHelper'; import { join } from "path"; import { commands, env, Uri, ViewColumn, window } from "vscode"; -import { Extension, Settings } from '../helpers'; -import { PreviewSettings } from '../models'; +import { Extension, parseWinPath, Settings } from '../helpers'; +import { ContentFolder, PreviewSettings } from '../models'; import { format } from 'date-fns'; import { DateHelper } from '../helpers/DateHelper'; import { Article } from '.'; import { urlJoin } from 'url-join-ts'; import { WebviewHelper } from '@estruyf/vscode'; +import { Folders } from './Folders'; export class Preview { @@ -37,6 +38,28 @@ export class Preview { let slug = article?.data ? article.data.slug : ""; let pathname = settings.pathname; + + // Check if there is a pathname defined on content folder level + const folders = Folders.get(); + if (folders.length > 0) { + const foldersWithPath = folders.filter(folder => folder.previewPath); + const filePath = parseWinPath(editor?.document.uri.fsPath); + + let selectedFolder: ContentFolder | null = null; + for (const folder of foldersWithPath) { + if (filePath.startsWith(folder.path)) { + if (!selectedFolder || selectedFolder.path.length < folder.path.length) { + selectedFolder = folder; + } + } + } + + if (selectedFolder) { + pathname = selectedFolder.previewPath; + } + } + + // Check if there is a pathname defined on content type level if (article?.data) { const contentType = ArticleHelper.getContentType(article.data); if (contentType && contentType.previewPath) { diff --git a/src/models/ContentFolder.ts b/src/models/ContentFolder.ts index 115c6713..00ec7bde 100644 --- a/src/models/ContentFolder.ts +++ b/src/models/ContentFolder.ts @@ -3,4 +3,5 @@ export interface ContentFolder { path: string; excludeSubdir?: boolean; + previewPath?: string; } \ No newline at end of file