diff --git a/CHANGELOG.md b/CHANGELOG.md index 77b58494..53d15ee4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ - [#447](https://github.com/estruyf/vscode-front-matter/issues/447): Allow to use placeholders on git commit messages - [#449](https://github.com/estruyf/vscode-front-matter/issues/449): Show `filename` if the `title` is not set - [#450](https://github.com/estruyf/vscode-front-matter/issues/450): Additional time placeholders added `{{hour12}}`, `{{hour24}}`, `{{ampm}}`, and `{{minute}}` +- [#458](https://github.com/estruyf/vscode-front-matter/issues/458): Ability to configure the file prefix on folder level ### ⚡️ Optimizations diff --git a/package.json b/package.json index 4ed4c4e8..b2189fb4 100644 --- a/package.json +++ b/package.json @@ -206,6 +206,10 @@ ], "default": null, "description": "Defines a custom preview path for the folder." + }, + "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 fc82963d..a16624f0 100644 --- a/src/commands/Article.ts +++ b/src/commands/Article.ts @@ -198,7 +198,7 @@ export class Article { Telemetry.send(TelemetryEvent.generateSlug); const updateFileName = Settings.get(SETTING_SLUG_UPDATE_FILE_NAME) as string; - const filePrefix = Settings.get(SETTING_TEMPLATES_PREFIX); + let filePrefix = Settings.get(SETTING_TEMPLATES_PREFIX); const editor = vscode.window.activeTextEditor; if (!editor) { @@ -210,6 +210,12 @@ 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; + } + const contentType = ArticleHelper.getContentType(article.data); const titleField = "title"; const articleTitle: string = article.data[titleField]; diff --git a/src/commands/Folders.ts b/src/commands/Folders.ts index d8cc7d9f..d348454c 100644 --- a/src/commands/Folders.ts +++ b/src/commands/Folders.ts @@ -427,6 +427,51 @@ export class Folders { return uniqueFolders.map(folder => relative(wsFolder?.path || "", folder)); } + /** + * Returns the file prefix for the given folder path + * @param folderPath + * @returns + */ + public static getFilePrefixByFolderPath(folderPath: string) { + const folders = Folders.get(); + const pageFolder = folders.find(f => parseWinPath(f.path) === parseWinPath(folderPath)); + + if (pageFolder && typeof pageFolder.filePrefix !== "undefined") { + return pageFolder.filePrefix; + } + + return; + } + + + /** + * Returns the file prefix for the given file path + * @param filePath + * @returns + */ + public static getFilePrefixBeFilePath(filePath: string) { + const folders = Folders.get(); + if (folders.length > 0) { + filePath = parseWinPath(filePath); + + let selectedFolder: ContentFolder | null = null; + for (const folder of folders) { + const folderPath = parseWinPath(folder.path); + if (filePath.startsWith(folderPath)) { + if (!selectedFolder || selectedFolder.path.length < folderPath.length) { + selectedFolder = folder; + } + } + } + + if (selectedFolder && typeof selectedFolder.filePrefix !== "undefined") { + return selectedFolder.filePrefix; + } + } + + return; + } + /** * Retrieve all content folders * @param pattern diff --git a/src/helpers/ArticleHelper.ts b/src/helpers/ArticleHelper.ts index c08880d4..541e7881 100644 --- a/src/helpers/ArticleHelper.ts +++ b/src/helpers/ArticleHelper.ts @@ -331,8 +331,13 @@ export class ArticleHelper { public static async createContent(contentType: ContentType | undefined, folderPath: string, titleValue: string, fileExtension?: string): Promise { FrontMatterParser.currentContent = null; - const prefix = Settings.get(SETTING_TEMPLATES_PREFIX); + 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; + } // Name of the file or folder to create const sanitizedName = ArticleHelper.sanitize(titleValue); diff --git a/src/models/ContentFolder.ts b/src/models/ContentFolder.ts index 00ec7bde..83ef1914 100644 --- a/src/models/ContentFolder.ts +++ b/src/models/ContentFolder.ts @@ -4,4 +4,5 @@ export interface ContentFolder { excludeSubdir?: boolean; previewPath?: string; + filePrefix?: string; } \ No newline at end of file