#752 - Support placeholders in list field

This commit is contained in:
Elio Struyf
2024-02-12 14:59:25 +01:00
parent 0ad0179a4b
commit 1315602bcc
4 changed files with 54 additions and 12 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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[];

View File

@@ -361,7 +361,7 @@ export const WrapperField: React.FunctionComponent<IWrapperFieldProps> = ({
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);
}