diff --git a/CHANGELOG.md b/CHANGELOG.md index eb187e55..6ec33d09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ - [#739](https://github.com/estruyf/vscode-front-matter/pull/739): New Git settings to disable and require a commit message - [#741](https://github.com/estruyf/vscode-front-matter/issues/741): Added message on the content dashboard when content is processed - [#747](https://github.com/estruyf/vscode-front-matter/issues/747): The `@frontmatter/extensibility` dependency now supports scripts for placeholders +- [#752](https://github.com/estruyf/vscode-front-matter/issues/752): Placeholder support in default `list` field values ### ⚡️ Optimizations diff --git a/src/helpers/ContentType.ts b/src/helpers/ContentType.ts index be3a83e5..c39f1d7f 100644 --- a/src/helpers/ContentType.ts +++ b/src/helpers/ContentType.ts @@ -1003,7 +1003,11 @@ export class ContentType { if (field.name === 'title') { if (field.default) { - data[field.name] = processArticlePlaceholdersFromData(field.default, data, contentType); + data[field.name] = processArticlePlaceholdersFromData( + field.default as string, + data, + contentType + ); data[field.name] = processTimePlaceholders( data[field.name], field.dateFormat || dateFormat @@ -1035,22 +1039,33 @@ export class ContentType { } } else { const defaultValue = field.default; + console.log(field.name, defaultValue, Array.isArray(defaultValue)); if (typeof defaultValue === 'string') { - data[field.name] = processArticlePlaceholdersFromData( + data[field.name] = ContentType.processFieldPlaceholders( defaultValue, data, - contentType - ); - data[field.name] = processTimePlaceholders( - data[field.name], - field.dateFormat || dateFormat - ); - data[field.name] = await ArticleHelper.processCustomPlaceholders( - data[field.name], + contentType, + field.dateFormat || dateFormat, titleValue, filePath ); + } else if (defaultValue && Array.isArray(defaultValue)) { + let defaultValues = []; + for (let value of defaultValue as string[]) { + if (typeof value === 'string') { + value = await ContentType.processFieldPlaceholders( + value, + data, + contentType, + field.dateFormat || dateFormat, + titleValue, + filePath + ); + } + defaultValues.push(value); + } + data[field.name] = defaultValues; } else if (typeof defaultValue !== 'undefined') { data[field.name] = defaultValue; } else { @@ -1113,6 +1128,32 @@ export class ContentType { return data; } + /** + * Processes the field placeholders in the given value. + * + * @param defaultValue - The default value for the field. + * @param data - The data object containing the field values. + * @param contentType - The content type object. + * @param dateFormat - The date format string. + * @param title - The title string. + * @param filePath - The file path string. + * @returns The processed value with field placeholders replaced. + */ + private static async processFieldPlaceholders( + defaultValue: string, + data: any, + contentType: IContentType, + dateFormat: string, + title: string, + filePath: string + ) { + let value = processArticlePlaceholdersFromData(defaultValue, data, contentType); + value = processTimePlaceholders(value, dateFormat); + value = await ArticleHelper.processCustomPlaceholders(value, title, filePath); + + return value; + } + /** * Verify if the content type feature is enabled * @returns diff --git a/src/models/PanelSettings.ts b/src/models/PanelSettings.ts index 561e44e7..a13f082c 100644 --- a/src/models/PanelSettings.ts +++ b/src/models/PanelSettings.ts @@ -106,7 +106,7 @@ export interface Field { isPreviewImage?: boolean; hidden?: boolean; taxonomyId?: string; - default?: string; + default?: string | number | string[] | boolean; fields?: Field[]; fieldGroup?: string | string[]; dataType?: string | string[]; diff --git a/src/panelWebView/components/Fields/WrapperField.tsx b/src/panelWebView/components/Fields/WrapperField.tsx index 6d3c29d5..7f9601cb 100644 --- a/src/panelWebView/components/Fields/WrapperField.tsx +++ b/src/panelWebView/components/Fields/WrapperField.tsx @@ -361,7 +361,7 @@ export const WrapperField: React.FunctionComponent = ({ let draftValue = parent[field.name]; if (!draftValue && typeof parent[field.name] === 'undefined' && field.default) { - draftValue = field.default; + draftValue = field.default as string; onSendUpdate(field.name, draftValue, parentFields); }