Skip to content

Commit

Permalink
getImageId and findImageIdInContent
Browse files Browse the repository at this point in the history
  • Loading branch information
ComLock committed Feb 5, 2024
1 parent a310245 commit 1e9b2b1
Show file tree
Hide file tree
Showing 28 changed files with 216 additions and 114 deletions.
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default {
'/guillotine/(.*)': '<rootDir>/src/main/resources/guillotine/$1',
'/lib/common/(.*)': '<rootDir>/src/main/resources/lib/common/$1',
'/lib/metadata/(.*)': '<rootDir>/src/main/resources/lib/metadata/$1',
'/lib/brand(.*)': '<rootDir>/src/main/resources/lib/brand$1',
'/lib/types(.*)': '<rootDir>/src/main/resources/lib/types$1',
},
preset: 'ts-jest/presets/js-with-babel-legacy',
testEnvironment: 'node',
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/guillotine/guillotine.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type {
Extensions,
GraphQL,
} from '/guillotine/guillotine.d';
} from '../lib/types/Guillotine';


import {GraphQLFieldName, GraphQLTypeName} from '/guillotine/guillotine.d';
import {GraphQLFieldName, GraphQLTypeName} from '../lib/types/Guillotine';
import {contentMetaFieldsResolver} from '/guillotine/typeFieldResolvers/contentMetaFieldsResolver';
import {metaFieldsImagesResolver} from '/guillotine/typeFieldResolvers/metaFieldsImagesResolver';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {Content} from '/lib/xp/content';
import type {Resolver} from '/guillotine/guillotine.d';
import type {Resolver} from '../../lib/types/Guillotine';


import {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type {Content} from '/lib/xp/content';
import type {Content, Site} from '/lib/xp/content';

import type {
// Content,
Resolver
} from '/guillotine/guillotine.d';
import type {MetafieldsSiteConfig} from '/lib/common/MetafieldsSiteConfig.d';
} from '../../lib/types/Guillotine';
import type {MetafieldsSiteConfig} from '../../lib/types/MetafieldsSiteConfig';


import {get as getContentByKey} from '/lib/xp/content';
Expand All @@ -14,13 +14,14 @@ import {
} from '/lib/xp/context';
import {commaStringToArray} from '/lib/common/commaStringToArray';
import {findStringValueInObject} from '/lib/common/findStringValueInObject';
import {getImageId} from '/lib/common/getImageId';


export const metaFieldsImagesResolver: Resolver<
{}, // args
{ // source
_content: Content
_site: Record<string, unknown>
_site: Site<MetafieldsSiteConfig>
_siteConfig: MetafieldsSiteConfig
// images: string[]
}
Expand Down Expand Up @@ -63,43 +64,20 @@ export const metaFieldsImagesResolver: Resolver<
}, () => {

const images = [];
if (_siteConfig.seoImage) {
const imageContent = getContentByKey({ key: _siteConfig.seoImage });
const imageId = getImageId({
content: _content,
site: _site,
siteConfig: _siteConfig
});
if (imageId) {
const imageContent = getContentByKey({ key: imageId });
if (imageContent) {
images.push(imageContent);
} else {
log.error(`_siteConfig.seoImage for site with _path:${_site._path} references a non-existing image with key:${_siteConfig.seoImage}`);
}
} else {
// Try to find image contentKey in content
const userDefinedPaths = _siteConfig.pathsImages || '';
const userDefinedArray = userDefinedPaths ? commaStringToArray(userDefinedPaths) : [];
const userDefinedValue = userDefinedPaths ? findStringValueInObject(_content, userDefinedArray, _siteConfig.fullPath) : null;
if (userDefinedValue) {
const imageContent = getContentByKey({ key: userDefinedValue });
if (imageContent) {
images.push(imageContent);
} else {
log.error(`content with _path:${_content._path} references a non-existing image with key:${userDefinedValue}}`);
}
} else {
if (_content.data.image) {
const imageContent = getContentByKey({ key: _content.data.image as string });
if (imageContent) {
images.push(imageContent);
} else {
log.error(`content with _path:${_content._path} references a non-existing image with key:${_content.data.image}}`);
}
} else if (_content.data.images) {
const imageContent = getContentByKey({ key: _content.data.images as string });
if (imageContent) {
images.push(imageContent);
} else {
log.error(`content with _path:${_content._path} references a non-existing image with key:${_content.data.images}}`);
}
}
log.error(`content with _path:${_content._path} or site with path: ${_site._path} references a non-existing image with key:${imageId}`);
}
}

if (_siteConfig.frontpageImage) {
const imageContent = getContentByKey({ key: _siteConfig.frontpageImage });
if (imageContent) {
Expand All @@ -108,6 +86,7 @@ export const metaFieldsImagesResolver: Resolver<
log.error(`siteConfig.frontpageImage for site with _path:${_site._path} references a non-existing image with key:${_siteConfig.frontpageImage}`);
}
}

return images;
});
}
6 changes: 3 additions & 3 deletions src/main/resources/lib/common/commaStringToArray.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {forceArray} from '@enonic/js-utils/array/forceArray';


export function commaStringToArray(str) {
var commas = str || '';
var arr = commas.split(',');
export function commaStringToArray(str: string): string[] {
const commas = str || '';
let arr = commas.split(',');
if (arr) {
arr = arr.map(function (s) { return s.trim() });
} else {
Expand Down
26 changes: 26 additions & 0 deletions src/main/resources/lib/common/findImageIdInContent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type {Content} from '/lib/xp/content';
import type {ImageId} from '/lib/types';
import type {MetafieldsSiteConfig} from '../types/MetafieldsSiteConfig';


import {commaStringToArray} from '/lib/common/commaStringToArray';
import {findStringValueInObject} from '/lib/common/findStringValueInObject';
import {ImageIdBuilder} from '/lib/types';


export function findImageIdInContent({
content,
siteConfig
}: {
content: Content
siteConfig: MetafieldsSiteConfig
}): ImageId|undefined {
const userDefinedPaths = siteConfig.pathsImages || '';
const userDefinedArray = userDefinedPaths ? commaStringToArray(userDefinedPaths) : [];
const userDefinedValue = userDefinedPaths ? findStringValueInObject(content, userDefinedArray, siteConfig.fullPath) : null;
return ImageIdBuilder.from(
userDefinedValue
|| content.data.image as string
|| content.data.images as string
) || undefined;
}
2 changes: 1 addition & 1 deletion src/main/resources/lib/common/getAppendix.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {Site} from '@enonic-types/lib-portal';
import type {MetafieldsSiteConfig} from '/lib/common/MetafieldsSiteConfig.d';
import type {MetafieldsSiteConfig} from '../types/MetafieldsSiteConfig';


import {getTheConfig} from '/lib/common/getTheConfig';
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/lib/common/getFullTitle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {Content} from '/lib/xp/content';
import type {Site} from '/lib/xp/portal';
import type {MetafieldsSiteConfig} from '/lib/common/MetafieldsSiteConfig.d';
import type {MetafieldsSiteConfig} from '../types/MetafieldsSiteConfig';


import {getAppendix} from '/lib/common/getAppendix';
Expand Down
38 changes: 38 additions & 0 deletions src/main/resources/lib/common/getImageId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type {Content, Site} from '/lib/xp/content';
import type {ImageId} from '/lib/types';
import type {MetafieldsSiteConfig} from '../types/MetafieldsSiteConfig';


import {APP_NAME_PATH, MIXIN_PATH} from '/lib/common/constants';
import {findImageIdInContent} from '/lib/common/findImageIdInContent';
import {ImageIdBuilder} from '/lib/types';


export function getImageId({
content,
site,
siteConfig
} :{
content: Content
site: Site<MetafieldsSiteConfig>
siteConfig: MetafieldsSiteConfig
}): ImageId|undefined {
// 1. Override image set on content
if(content.x?.[APP_NAME_PATH]?.[MIXIN_PATH]?.seoImage) {
return ImageIdBuilder.from(content.x[APP_NAME_PATH][MIXIN_PATH].seoImage as string);
}

// 2. Try to find an image within the content isself
const imageId = findImageIdInContent({content, siteConfig});
if (imageId) {
return imageId;
}

// 3. Fallback to siteConfig image
if (siteConfig.seoImage) {
return ImageIdBuilder.from(siteConfig.seoImage);
}

// 4. Fallback to image on siteContent
return findImageIdInContent({content: site, siteConfig})
}
2 changes: 1 addition & 1 deletion src/main/resources/lib/common/getImageUrl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {Content, Site} from '/lib/xp/content';
import type {ImageUrlParams} from '/lib/xp/portal';
// import type {MediaImage} from '/guillotine/guillotine.d';
import type {MetafieldsSiteConfig} from '/lib/common/MetafieldsSiteConfig';
import type {MetafieldsSiteConfig} from '../types/MetafieldsSiteConfig';


import {forceArray} from '@enonic/js-utils/array/forceArray';
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/lib/common/getMetaDescription.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {Content} from '/lib/xp/content';
import type {Site} from '/lib/xp/portal';
import type {MetafieldsSiteConfig} from '/lib/common/MetafieldsSiteConfig.d';
import type {MetafieldsSiteConfig} from '../types/MetafieldsSiteConfig';
// import type {Content} from '/guillotine/guillotine.d';


Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/lib/common/getPageTitle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {Content} from '/lib/xp/content';
import type {Site} from '@enonic-types/lib-portal';
import type {MetafieldsSiteConfig} from '/lib/common/MetafieldsSiteConfig.d';
import type {MetafieldsSiteConfig} from '../types/MetafieldsSiteConfig';


import {commaStringToArray} from '/lib/common/commaStringToArray';
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/lib/common/getSite.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {Site} from '/lib/xp/portal';
import type { MetafieldsSiteConfig } from '/lib/common/MetafieldsSiteConfig.d';
import type { MetafieldsSiteConfig } from '../types/MetafieldsSiteConfig';


import {query} from '/lib/xp/content';
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/lib/common/getSiteConfigFromSite.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {Site} from '/lib/xp/portal';
import type {MetafieldsSiteConfig} from '/lib/common/MetafieldsSiteConfig.d';
import type {MetafieldsSiteConfig} from '../types/MetafieldsSiteConfig';


import {forceArray} from '@enonic/js-utils/array/forceArray';
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/lib/common/getTheConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {Site} from '/lib/xp/portal';
import type { MetafieldsSiteConfig } from '/lib/common/MetafieldsSiteConfig.d';
import type { MetafieldsSiteConfig } from '../types/MetafieldsSiteConfig';


import {getSiteConfig as libPortalGetSiteConfig} from '/lib/xp/portal';
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/lib/metadata/getMetaData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {Content} from '/lib/xp/content';
import type {Site} from '/lib/xp/portal';
import type {MetafieldsSiteConfig} from '/lib/common/MetafieldsSiteConfig.d';
import type {MetafieldsSiteConfig} from '../types/MetafieldsSiteConfig';


import {pageUrl} from '/lib/xp/portal';
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/lib/metadata/getTitleHtml.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {Content} from '/lib/xp/content';
import type {Site} from '/lib/xp/portal';
import type {MetafieldsSiteConfig} from '/lib/common/MetafieldsSiteConfig.d';
import type {MetafieldsSiteConfig} from '../types/MetafieldsSiteConfig';


import {getFullTitle} from '/lib/common/getFullTitle';
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,6 @@ import type {Content} from '@enonic-types/lib-content';
import type {Branded} from '/lib/brand.d';


export declare type BaseFolder = Content<{}, 'base:folder'>;

export declare type MediaImage = Content<{
media: {
altText?: string
artist?: string
attachment: string
caption?: string
copyright?: string
focalPoint: {
x: number
y: number
}
tags?: string // with comma?
}
}, 'media:image'>;

export const enum GraphQLTypeName {
CONTENT = 'Content',
// IMAGE = 'Image',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type {BrandBase, BrandBuilder, BrandBuilderOptions, Branded} from '/lib/brand.d';
import type {BrandBase, BrandBuilder, BrandBuilderOptions, Branded} from '/lib/types/Branded';


export function brand<T extends Branded<Base, any>, Base = BrandBase<T>>({
validate = () => true,
}: BrandBuilderOptions<Base> = {}): BrandBuilder<T, Base> {
export function brand<T extends Branded<Base, any>, Base = BrandBase<T>>(
{
validate = () => true,
}: BrandBuilderOptions<Base> = {}
): BrandBuilder<T, Base> {
function assertIsBrand(value: Base): asserts value is T {
const result = validate(value)
if (typeof result === 'string') {
Expand Down
71 changes: 71 additions & 0 deletions src/main/resources/lib/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//──────────────────────────────────────────────────────────────────────────────
// Type imports
//──────────────────────────────────────────────────────────────────────────────
import type {Content} from '@enonic-types/lib-content';
import type {Branded} from '/lib/types/Branded';
import type {
GraphQLBoolean,
GraphQLContent,
GraphQLID,
GraphQLInt,
GraphQLFloat,
GraphQLJson,
GraphQLDateTime,
GraphQLDate,
GraphQLLocalTime,
GraphQLLocalDateTime,
GraphQLMediaImage,
GraphQLMetaFields,
GraphQLString,
} from '/lib/types/Guillotine';


//──────────────────────────────────────────────────────────────────────────────
// Value imports
//──────────────────────────────────────────────────────────────────────────────
import {brand} from '/lib/types/brand';


//──────────────────────────────────────────────────────────────────────────────
// ExportedtTypes
//──────────────────────────────────────────────────────────────────────────────
export type {MetafieldsSiteConfig} from '/lib/types/MetafieldsSiteConfig';

export declare type BaseFolder = Content<{}, 'base:folder'>;

export declare type ImageId = Branded<string, 'ImageId'>;

export declare type MediaImage = Content<{
media: {
altText?: string
artist?: string
attachment: string
caption?: string
copyright?: string
focalPoint: {
x: number
y: number
}
tags?: string // with comma?
}
}, 'media:image'>;

//──────────────────────────────────────────────────────────────────────────────
// Exported values
//──────────────────────────────────────────────────────────────────────────────
export const ImageIdBuilder = brand<ImageId>();

export const GraphQLBooleanBuilder = brand<GraphQLBoolean>();
export const GraphQLDateBuilder = brand<GraphQLDate>();
export const GraphQLDateTimeBuilder = brand<GraphQLDateTime>();
export const GraphQLFloatBuilder = brand<GraphQLFloat>();
export const GraphQLIDBuilder = brand<GraphQLID>();
export const GraphQLIntBuilder = brand<GraphQLInt>();
export const GraphQLJsonBuilder = brand<GraphQLJson>();
export const GraphQLLocalDateTimeBuilder = brand<GraphQLLocalDateTime>();
export const GraphQLLocalTimeBuilder = brand<GraphQLLocalTime>();
export const GraphQLStringBuilder = brand<GraphQLString>();

export const GraphQLContentBuilder = brand<GraphQLContent>();
export const GraphQLMediaImageBuilder = brand<GraphQLMediaImage>();
export const GraphQLMetaFieldsBuilder = brand<GraphQLMetaFields>();
Loading

0 comments on commit 1e9b2b1

Please sign in to comment.