Skip to content

Commit

Permalink
#709 - Where clause on content creation
Browse files Browse the repository at this point in the history
  • Loading branch information
estruyf committed Nov 15, 2023
1 parent 9724168 commit 520bdf2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 7 additions & 2 deletions src/helpers/ContentType.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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';

Expand Down Expand Up @@ -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(
Expand Down
34 changes: 17 additions & 17 deletions src/utils/fieldWhenClause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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') {
Expand Down

0 comments on commit 520bdf2

Please sign in to comment.