diff --git a/CHANGELOG.md b/CHANGELOG.md index b5b4cad5..0aba8048 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - [#690](https://github.com/estruyf/vscode-front-matter/issues/690): Added the ability to filter values in the `contentRelationship` field - [#700](https://github.com/estruyf/vscode-front-matter/issues/700): Added the `{{pathToken.relPath}}` placeholder for the `previewPath` property - [#706](https://github.com/estruyf/vscode-front-matter/issues/706): Show the error of scripts failing in the Front Matter output panel +- [#709](https://github.com/estruyf/vscode-front-matter/issues/709): Take "where clause" into account on content creation ### ⚡️ Optimizations diff --git a/src/helpers/ContentType.ts b/src/helpers/ContentType.ts index dccedf32..53fa7d1f 100644 --- a/src/helpers/ContentType.ts +++ b/src/helpers/ContentType.ts @@ -1,6 +1,6 @@ import { ModeListener } from './../listeners/general/ModeListener'; import { PagesListener } from './../listeners/dashboard'; -import { ArticleHelper, CustomScript, Settings } from '.'; +import { ArticleHelper, CustomScript, Logger, Settings } from '.'; import { DefaultFieldValues, EXTENSION_NAME, @@ -29,7 +29,7 @@ import { Telemetry } from './Telemetry'; import { processKnownPlaceholders } from './PlaceholderHelper'; import { basename } from 'path'; import { ParsedFrontMatter } from '../parsers'; -import { encodeEmoji, existsAsync, writeFileAsync } from '../utils'; +import { encodeEmoji, existsAsync, fieldWhenClause, writeFileAsync } from '../utils'; import * as l10n from '@vscode/l10n'; import { LocalizationKey } from '../localization'; @@ -918,6 +918,11 @@ export class ContentType { const dateFormat = Settings.get(SETTING_DATE_FORMAT) as string; for (const field of obj.fields) { + if (!fieldWhenClause(field, data)) { + Logger.info(`Field ${field.name} not added because of when clause`); + continue; + } + if (field.name === 'title') { if (field.default) { data[field.name] = processKnownPlaceholders( diff --git a/src/utils/fieldWhenClause.ts b/src/utils/fieldWhenClause.ts index 0bd0aa85..376b84b5 100644 --- a/src/utils/fieldWhenClause.ts +++ b/src/utils/fieldWhenClause.ts @@ -3,10 +3,10 @@ import { Field, WhenOperator } from '../models'; import { IMetadata } from '../panelWebView/components/Metadata'; /** - * Validate the field its "when" clause - * @param field - * @param parent - * @returns + * Determines whether a field should be displayed based on its "when" clause. + * @param field - The field to check. + * @param parent - The parent metadata object. + * @returns A boolean indicating whether the field should be displayed. */ export const fieldWhenClause = (field: Field, parent: IMetadata): boolean => { const when = field.when; @@ -23,11 +23,11 @@ export const fieldWhenClause = (field: Field, parent: IMetadata): boolean => { }; /** - * Case sensitive checks - * @param when - * @param field - * @param whenValue - * @returns + * Returns a boolean indicating whether the given `when` clause matches the given `field` and `whenValue`, ignoring case sensitivity. + * @param when - The `WhenClause` to match against. + * @param field - The `Field` to match against. + * @param whenValue - The value to match against the `when` clause. + * @returns A boolean indicating whether the `when` clause matches the `field` and `whenValue`, ignoring case sensitivity. */ const caseInsensitive = ( when: WhenClause, @@ -43,11 +43,11 @@ const caseInsensitive = ( }; /** - * Case insensitive checks - * @param when - * @param field - * @param whenValue - * @returns + * Determines if a given field matches a when clause with case sensitivity. + * @param when - The when clause to match against. + * @param field - The field to match. + * @param whenValue - The value to match against the when clause. + * @returns True if the field matches the when clause, false otherwise. */ const caseSensitive = ( when: WhenClause, @@ -119,9 +119,9 @@ const caseSensitive = ( }; /** - * Lower the value(s) - * @param value - * @returns + * Converts the given string or array of strings to lowercase. + * @param value - The string or array of strings to convert to lowercase. + * @returns The converted string or array of strings. */ const lowerValue = (value: string | string[] | any) => { if (typeof value === 'string') {