#474 - Allow to define the file prefix on content types

This commit is contained in:
Elio Struyf
2022-12-23 15:00:49 +01:00
parent 50f2e7ea72
commit 70316c4c28
5 changed files with 45 additions and 17 deletions
+1
View File
@@ -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
+7
View File
@@ -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,
+4 -8
View File
@@ -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<string>(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<string>(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);
+32 -9
View File
@@ -331,17 +331,10 @@ export class ArticleHelper {
public static async createContent(contentType: ContentType | undefined, folderPath: string, titleValue: string, fileExtension?: string): Promise<string | undefined> {
FrontMatterParser.currentContent = null;
let prefix = Settings.get<string>(SETTING_TEMPLATES_PREFIX);
const fileType = Settings.get<string>(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<string>(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
+1
View File
@@ -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";