From 70316c4c2845838065734f3f95e36c78440dd8a6 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Fri, 23 Dec 2022 15:00:49 +0100 Subject: [PATCH] #474 - Allow to define the file prefix on content types --- CHANGELOG.md | 1 + package.json | 7 ++++++ src/commands/Article.ts | 12 ++++------- src/helpers/ArticleHelper.ts | 41 ++++++++++++++++++++++++++++-------- src/models/PanelSettings.ts | 1 + 5 files changed, 45 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f1880af..5d6e42d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### 🎨 Enhancements +- [#474](https://github.com/estruyf/vscode-front-matter/issues/474): Allow to define the file prefix on content types - [#484](https://github.com/estruyf/vscode-front-matter/issues/484): Support for overriding scripts per environment type ### ⚡️ Optimizations diff --git a/package.json b/package.json index 4015b7f5..107164ed 100644 --- a/package.json +++ b/package.json @@ -1292,6 +1292,13 @@ "type": "string", "default": "", "description": "An optional post script that can be used after new content creation." + }, + "filePrefix": { + "type": [ + "null", + "string" + ], + "description": "Defines a prefix for the file name." } }, "additionalProperties": false, diff --git a/src/commands/Article.ts b/src/commands/Article.ts index a16624f0..48dd675c 100644 --- a/src/commands/Article.ts +++ b/src/commands/Article.ts @@ -198,7 +198,6 @@ export class Article { Telemetry.send(TelemetryEvent.generateSlug); const updateFileName = Settings.get(SETTING_SLUG_UPDATE_FILE_NAME) as string; - let filePrefix = Settings.get(SETTING_TEMPLATES_PREFIX); const editor = vscode.window.activeTextEditor; if (!editor) { @@ -210,13 +209,10 @@ export class Article { return; } - // Retrieve the file prefix from the folder - const filePrefixOnFolder = Folders.getFilePrefixBeFilePath(editor.document.uri.fsPath); - if (typeof filePrefixOnFolder !== "undefined") { - filePrefix = filePrefixOnFolder; - } - + let filePrefix = Settings.get(SETTING_TEMPLATES_PREFIX); const contentType = ArticleHelper.getContentType(article.data); + filePrefix = ArticleHelper.getFilePrefix(editor.document.uri.fsPath, contentType); + const titleField = "title"; const articleTitle: string = article.data[titleField]; const slugInfo = Article.generateSlug(articleTitle); @@ -259,7 +255,7 @@ export class Article { let newFileName = `${slugName}${ext}`; if (filePrefix && typeof filePrefix === "string") { - newFileName = `${format(new Date(), DateHelper.formatUpdate(filePrefix) as string)}-${newFileName}`; + newFileName = `${filePrefix}-${newFileName}`; } const newPath = editor.document.uri.fsPath.replace(fileName, newFileName); diff --git a/src/helpers/ArticleHelper.ts b/src/helpers/ArticleHelper.ts index 258b285b..260142b0 100644 --- a/src/helpers/ArticleHelper.ts +++ b/src/helpers/ArticleHelper.ts @@ -331,17 +331,10 @@ export class ArticleHelper { public static async createContent(contentType: ContentType | undefined, folderPath: string, titleValue: string, fileExtension?: string): Promise { FrontMatterParser.currentContent = null; - let prefix = Settings.get(SETTING_TEMPLATES_PREFIX); const fileType = Settings.get(SETTING_CONTENT_DEFAULT_FILETYPE); - const filePrefixOnFolder = Folders.getFilePrefixByFolderPath(folderPath); - if (typeof filePrefixOnFolder !== "undefined") { - prefix = filePrefixOnFolder; - } - - if (prefix && typeof prefix === "string") { - prefix = `${format(new Date(), DateHelper.formatUpdate(prefix) as string)}`; - } + let prefix = Settings.get(SETTING_TEMPLATES_PREFIX); + prefix = ArticleHelper.getFilePrefix(folderPath, contentType); // Name of the file or folder to create let sanitizedName = ArticleHelper.sanitize(titleValue); @@ -379,6 +372,36 @@ export class ArticleHelper { return newFilePath; } + /** + * Retrieve the file prefix + * @param filePath + * @param contentType + * @returns + */ + public static getFilePrefix(filePath?: string, contentType?: ContentType): string | undefined { + let prefix = undefined; + + // Retrieve the file prefix from the folder + if (filePath) { + const filePrefixOnFolder = Folders.getFilePrefixByFolderPath(filePath); + if (typeof filePrefixOnFolder !== "undefined") { + prefix = filePrefixOnFolder; + } + } + + // Retrieve the file prefix from the content type + if (contentType && typeof contentType.filePrefix !== "undefined") { + prefix = contentType.filePrefix; + } + + // Process the prefix date formatting + if (prefix && typeof prefix === "string") { + prefix = `${format(new Date(), DateHelper.formatUpdate(prefix) as string)}`; + } + + return prefix; + } + /** * Update placeholder values in the front matter content * @param data diff --git a/src/models/PanelSettings.ts b/src/models/PanelSettings.ts index 647e919d..ebd13131 100644 --- a/src/models/PanelSettings.ts +++ b/src/models/PanelSettings.ts @@ -49,6 +49,7 @@ export interface ContentType { pageBundle?: boolean; template?: string; postScript?: string; + filePrefix?: string; } export type FieldType = "string" | "number" | "datetime" | "boolean" | "image" | "choice" | "tags" | "categories" | "draft" | "taxonomy" | "fields" | "json" | "block" | "file" | "dataFile" | "list" | "slug" | "divider" | "heading";