From 327559fdd7c587a709301f5d7e250de60c277149 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Thu, 9 Feb 2023 22:30:36 +0100 Subject: [PATCH] #425 - support added for placeholders in the page folders path --- CHANGELOG.md | 1 + src/commands/Folders.ts | 35 ++++++++++++++++++++++------------- src/helpers/ArticleHelper.ts | 4 +++- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2978cd7d..e5d84dc1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### 🎨 Enhancements +- [#425](https://github.com/estruyf/vscode-front-matter/issues/425): Added support for placeholders in the content paths - [#473](https://github.com/estruyf/vscode-front-matter/issues/473): Allow setting the SEO title name with the `frontMatter.taxonomy.seoTitleField` setting - [#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 diff --git a/src/commands/Folders.ts b/src/commands/Folders.ts index ad22fafc..f5f8fc7d 100644 --- a/src/commands/Folders.ts +++ b/src/commands/Folders.ts @@ -4,6 +4,7 @@ import { SETTING_CONTENT_PAGE_FOLDERS, SETTING_CONTENT_STATIC_FOLDER, SETTING_CONTENT_SUPPORTED_FILETYPES, + SETTING_DATE_FORMAT, TelemetryEvent } from './../constants'; import { commands, Uri, workspace, window } from 'vscode'; @@ -12,7 +13,7 @@ import { ContentFolder, FileInfo, FolderInfo } from '../models'; import uniqBy = require('lodash.uniqby'); import { Template } from './Template'; import { Notifications } from '../helpers/Notifications'; -import { Logger, Settings } from '../helpers'; +import { Logger, processKnownPlaceholders, Settings } from '../helpers'; import { existsSync } from 'fs'; import { format } from 'date-fns'; import { Dashboard } from './Dashboard'; @@ -343,18 +344,26 @@ export class Folders { folder.title = basename(folder.path); } - const folderPath = Folders.absWsFolder(folder, wsFolder); - if (!existsSync(folderPath)) { - Notifications.errorShowOnce( - `Folder "${folder.title} (${folder.path})" does not exist. Please remove it from the settings.`, - 'Remove folder' - ).then((answer) => { - if (answer === 'Remove folder') { - const folders = Folders.get(); - Folders.update(folders.filter((f) => f.path !== folder.path)); - } - }); - return null; + let folderPath: string | undefined = Folders.absWsFolder(folder, wsFolder); + if (folderPath.includes(`{{`) && folderPath.includes(`}}`)) { + const dateFormat = Settings.get(SETTING_DATE_FORMAT) as string; + folderPath = processKnownPlaceholders(folderPath, undefined, dateFormat); + } else { + if (folderPath && !existsSync(folderPath)) { + Notifications.errorShowOnce( + `Folder "${folder.title} (${folder.path})" does not exist. Please remove it from the settings.`, + 'Remove folder', + 'Create folder' + ).then((answer) => { + if (answer === 'Remove folder') { + const folders = Folders.get(); + Folders.update(folders.filter((f) => f.path !== folder.path)); + } else if (answer === 'Create folder') { + mkdirAsync(folderPath as string, { recursive: true }); + } + }); + return null; + } } return { diff --git a/src/helpers/ArticleHelper.ts b/src/helpers/ArticleHelper.ts index ad734506..bacbf6fa 100644 --- a/src/helpers/ArticleHelper.ts +++ b/src/helpers/ArticleHelper.ts @@ -402,7 +402,7 @@ export class ArticleHelper { ); return; } else { - await mkdirAsync(newFolder); + await mkdirAsync(newFolder, { recursive: true }); newFilePath = join(newFolder, `index.${fileExtension || contentType.fileType || fileType}`); } } else { @@ -414,6 +414,8 @@ export class ArticleHelper { newFilePath = join(folderPath, newFileName); + await mkdirAsync(folderPath, { recursive: true }); + if (await existsAsync(newFilePath)) { Notifications.warning(`Content with the title already exists. Please specify a new title.`); return;