diff --git a/CHANGELOG.md b/CHANGELOG.md index 53d15ee4..9f3f64fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### 🎨 Enhancements +- [#360](https://github.com/estruyf/vscode-front-matter/issues/360): Define which content types can be used on your page folders - [#406](https://github.com/estruyf/vscode-front-matter/issues/406): Added support for single data entries in the data dashboard - [#428](https://github.com/estruyf/vscode-front-matter/issues/428): Improved UX for inserting images to your content - [#430](https://github.com/estruyf/vscode-front-matter/issues/430): Support for HEXO its `post_asset_folder` setting (image location) diff --git a/package.json b/package.json index b2189fb4..a7a8bc8f 100644 --- a/package.json +++ b/package.json @@ -210,6 +210,13 @@ "filePrefix": { "type": [ "null", "string" ], "description": "Defines a prefix for the file name." + }, + "contentTypes": { + "type": "array", + "description": "Defines which content types can be used for the current location. If not defined, all content types will be available.", + "items": { + "type": "string" + } } }, "additionalProperties": false, diff --git a/src/commands/Folders.ts b/src/commands/Folders.ts index d348454c..ed280a5f 100644 --- a/src/commands/Folders.ts +++ b/src/commands/Folders.ts @@ -81,7 +81,7 @@ export class Folders { * Create content in a registered folder * @returns */ - public static async create() { + public static async create() { const selectedFolder = await Questions.SelectContentFolder(); if (!selectedFolder) { return; diff --git a/src/helpers/ContentType.ts b/src/helpers/ContentType.ts index 223b6c9a..351d8493 100644 --- a/src/helpers/ContentType.ts +++ b/src/helpers/ContentType.ts @@ -64,11 +64,6 @@ export class ContentType { * @returns */ public static async createContent() { - const selectedContentType = await Questions.SelectContentType(); - if (!selectedContentType) { - return; - } - const selectedFolder = await Questions.SelectContentFolder(); if (!selectedFolder) { return; @@ -76,10 +71,19 @@ export class ContentType { const contentTypes = ContentType.getAll(); const folders = Folders.get(); + const folder = folders.find(f => f.title === selectedFolder); - const location = folders.find(f => f.title === selectedFolder); - if (contentTypes && location) { - const folderPath = Folders.getFolderPath(Uri.file(location.path)); + if (!folder) { + return; + } + + const selectedContentType = await Questions.SelectContentType(folder.contentTypes || []); + if (!selectedContentType) { + return; + } + + if (contentTypes && folder) { + const folderPath = Folders.getFolderPath(Uri.file(folder.path)); const contentType = contentTypes.find(ct => ct.name === selectedContentType); if (folderPath && contentType) { ContentType.create(contentType, folderPath); diff --git a/src/helpers/Questions.ts b/src/helpers/Questions.ts index 182be966..b3e5fe7d 100644 --- a/src/helpers/Questions.ts +++ b/src/helpers/Questions.ts @@ -46,7 +46,7 @@ export class Questions { * @returns */ public static async SelectContentFolder(showWarning: boolean = true): Promise { - const folders = Folders.get(); + let folders = Folders.get(); let selectedFolder: string | undefined; if (folders.length > 1) { @@ -72,16 +72,22 @@ export class Questions { /** * Select the content type to create new content + * @param allowedCts Allowed content types for the folder * @param showWarning * @returns */ - public static async SelectContentType(showWarning: boolean = true): Promise { - const contentTypes = ContentType.getAll(); + public static async SelectContentType(allowedCts: string[], showWarning: boolean = true): Promise { + let contentTypes = ContentType.getAll(); if (!contentTypes || contentTypes.length === 0) { Notifications.warning("No content types found. Please create a content type first."); return; } + // Only allow content types that are allowed for the folder + if (allowedCts && allowedCts.length > 0) { + contentTypes = contentTypes.filter(ct => allowedCts.find(allowedCt => allowedCt === ct.name)); + } + if (contentTypes.length === 1) { return contentTypes[0].name; } diff --git a/src/models/ContentFolder.ts b/src/models/ContentFolder.ts index 83ef1914..c0dc5516 100644 --- a/src/models/ContentFolder.ts +++ b/src/models/ContentFolder.ts @@ -5,4 +5,5 @@ export interface ContentFolder { excludeSubdir?: boolean; previewPath?: string; filePrefix?: string; + contentTypes?: string[]; } \ No newline at end of file