From 0be91c17d079cb10299619b68f421ed61c8d17a9 Mon Sep 17 00:00:00 2001 From: Elio Struyf Date: Mon, 11 Oct 2021 21:40:05 +0200 Subject: [PATCH] #141 - Added support for page bundles with templates --- package.json | 1 + src/commands/Template.ts | 42 ++++++++++++++++++++++++++++++------ src/constants/ContentType.ts | 1 + src/helpers/ContentType.ts | 4 +++- 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 8b9e5f49..07eec6e0 100644 --- a/package.json +++ b/package.json @@ -330,6 +330,7 @@ "default": [ { "name": "default", + "pageBundle": false, "fields": [ { "title": "Title", diff --git a/src/commands/Template.ts b/src/commands/Template.ts index da9c7120..3aaeb59a 100644 --- a/src/commands/Template.ts +++ b/src/commands/Template.ts @@ -11,6 +11,9 @@ import { Notifications } from '../helpers/Notifications'; import { CONTEXT } from '../constants'; import { Project } from './Project'; import { Folders } from './Folders'; +import { ContentType } from '../helpers/ContentType'; +import { join } from 'path'; +import { existsSync, mkdirSync } from 'fs'; export class Template { @@ -96,6 +99,7 @@ export class Template { public static async create(folderPath: string) { const folder = Settings.get(SETTING_TEMPLATES_FOLDER); const prefix = Settings.get(SETTING_TEMPLATES_PREFIX); + const contentTypes = ContentType.getAll(); if (!folderPath) { Notifications.warning(`Incorrect project folder path retrieved.`); @@ -133,16 +137,40 @@ export class Template { return; } - const fileExt = path.parse(selectedTemplate).ext; + // Name of the file or folder to create const sanitizedName = sanitize(titleValue.toLowerCase().replace(/ /g, "-")); - let newFileName = `${sanitizedName}${fileExt}`; - if (prefix && typeof prefix === "string") { - newFileName = `${format(new Date(), prefix)}-${newFileName}`; + let newFilePath: string | undefined; + + const templateData = ArticleHelper.getFrontMatterByPath(template.fsPath); + if (templateData && templateData.data && templateData.data.type) { + const type = contentTypes?.find(t => t.name === templateData.data.type); + if (type && type.pageBundle) { + const newFolder = join(folderPath, sanitizedName); + if (existsSync(newFolder)) { + Notifications.error(`A page bundle with the name ${sanitizedName} already exists in ${folderPath}`); + return; + } else { + mkdirSync(newFolder); + newFilePath = join(newFolder, `index.md`); + } + } else { + const fileExt = path.parse(selectedTemplate).ext; + let newFileName = `${sanitizedName}${fileExt}`; + + if (prefix && typeof prefix === "string") { + newFileName = `${format(new Date(), prefix)}-${newFileName}`; + } + + newFilePath = path.join(folderPath, newFileName); + + if (fs.existsSync(newFilePath)) { + Notifications.warning(`File already exists, please remove it before creating a new one with the same title.`); + return; + } + } } - const newFilePath = path.join(folderPath, newFileName); - if (fs.existsSync(newFilePath)) { - Notifications.warning(`File already exists, please remove it before creating a new one with the same title.`); + if (!newFilePath) { return; } diff --git a/src/constants/ContentType.ts b/src/constants/ContentType.ts index 6041395e..181993f2 100644 --- a/src/constants/ContentType.ts +++ b/src/constants/ContentType.ts @@ -4,6 +4,7 @@ export const DEFAULT_CONTENT_TYPE_NAME = 'default'; export const DEFAULT_CONTENT_TYPE: ContentType = { "name": "default", + "pageBundle": false, "fields": [ { "title": "Title", diff --git a/src/helpers/ContentType.ts b/src/helpers/ContentType.ts index 0ffabedf..1603d05d 100644 --- a/src/helpers/ContentType.ts +++ b/src/helpers/ContentType.ts @@ -90,7 +90,7 @@ export class ContentType { return; } - const data: any = {}; + let data: any = {}; for (const field of contentType.fields) { if (field.name === "title") { @@ -100,6 +100,8 @@ export class ContentType { } } + data = ArticleHelper.updateDates(Object.assign({}, data)); + if (contentType.name !== DEFAULT_CONTENT_TYPE_NAME) { data['type'] = contentType.name; }