#458 - Ability to configure the file prefix on folder level

This commit is contained in:
Elio Struyf
2022-11-09 10:31:49 +01:00
parent 33e294d702
commit f1a8e0d425
6 changed files with 64 additions and 2 deletions
+1
View File
@@ -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
+4
View File
@@ -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,
+7 -1
View File
@@ -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<string>(SETTING_TEMPLATES_PREFIX);
let filePrefix = Settings.get<string>(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];
+45
View File
@@ -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
+6 -1
View File
@@ -331,8 +331,13 @@ export class ArticleHelper {
public static async createContent(contentType: ContentType | undefined, folderPath: string, titleValue: string, fileExtension?: string): Promise<string | undefined> {
FrontMatterParser.currentContent = null;
const prefix = Settings.get<string>(SETTING_TEMPLATES_PREFIX);
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;
}
// Name of the file or folder to create
const sanitizedName = ArticleHelper.sanitize(titleValue);
+1
View File
@@ -4,4 +4,5 @@ export interface ContentFolder {
excludeSubdir?: boolean;
previewPath?: string;
filePrefix?: string;
}