Skip to content

Commit

Permalink
Fix #122 Remove disableAppConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
ComLock committed Feb 15, 2024
1 parent 0cc1541 commit 468f06d
Show file tree
Hide file tree
Showing 22 changed files with 263 additions and 329 deletions.
1 change: 1 addition & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default {
},
moduleNameMapper: {
'/guillotine/(.*)': '<rootDir>/src/main/resources/guillotine/$1',
'/lib/app-metafields/(.*)': '<rootDir>/src/main/resources/lib/app-metafields/$1',
'/lib/common/(.*)': '<rootDir>/src/main/resources/lib/common/$1',
'/lib/metadata/(.*)': '<rootDir>/src/main/resources/lib/metadata/$1',
'/lib/types(.*)': '<rootDir>/src/main/resources/lib/types$1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { MetafieldsSiteConfig } from '/lib/types/MetafieldsSiteConfig';
import {query} from '/lib/xp/content';


export const getSite = ({
export const queryForFirstSiteWithAppAndUrl = ({
applicationKey, // Avoid app.name so it can be used in Guillotine Extension Context
siteUrl
} : {
Expand Down
42 changes: 19 additions & 23 deletions src/main/resources/admin/widgets/seo/seo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
// @ts-expect-error // No types yet
import {render} from '/lib/thymeleaf';

import {queryForFirstSiteWithAppAndUrl} from '/admin/widgets/seo/queryForFirstSiteWithAppAndUrl';

import {prependBaseUrl} from '/lib/app-metafields/url/prependBaseUrl';
import {getAppendix} from '/lib/common/getAppendix';
import {getBlockRobots} from '/lib/common/getBlockRobots';
Expand All @@ -19,8 +21,7 @@ import {getImageUrl} from '/lib/common/getImageUrl';
import {getLang} from '/lib/common/getLang';
import {getMetaDescription} from '/lib/common/getMetaDescription';
import {getPageTitle} from '/lib/common/getPageTitle';
import {getSite} from '/lib/common/getSite';
import {getTheConfig} from '/lib/common/getTheConfig';
import {getAppOrSiteConfig} from '/lib/common/getAppOrSiteConfig';


/*
Expand Down Expand Up @@ -60,33 +61,29 @@ export const get = (req: Request) => {
if (content) {
// The first part of the content '_path' is the site's URL, make sure to fetch current site!
const parts = content._path.split('/');
const site = getSite({
const site = queryForFirstSiteWithAppAndUrl({
applicationKey: app.name, // NOTE: Using app.name is fine, since it's outside Guillotine Execution Context
siteUrl: parts[1]
}); // Send the first /x/-part of the content's path.
if (site) {
const siteConfig = getTheConfig({
const appOrSiteConfig = getAppOrSiteConfig({
applicationConfig: app.config, // NOTE: Using app.config is fine, since it's outside Guillotine Execution Context
applicationKey: app.name, // NOTE: Using app.name is fine, since it's outside Guillotine Execution Context
site
});
if (siteConfig) {
if (appOrSiteConfig) {
const isFrontpage = site._path === content._path;
const pageTitle = getPageTitle({
applicationConfig: app.config, // NOTE: Using app.config is fine, since it's outside Guillotine Execution Context
applicationKey: app.name, // NOTE: Using app.name is fine, since it's outside Guillotine Execution Context
appOrSiteConfig,
content,
site
});
const titleAppendix = getAppendix({
applicationConfig: app.config, // NOTE: Using app.config is fine, since it's outside Guillotine Execution Context
applicationKey: app.name, // NOTE: Using app.name is fine, since it's outside Guillotine Execution Context
appOrSiteConfig,
isFrontpage,
site,
});
let description = getMetaDescription({
applicationConfig: app.config, // NOTE: Using app.config is fine, since it's outside Guillotine Execution Context
applicationKey: app.name, // NOTE: Using app.name is fine, since it's outside Guillotine Execution Context
appOrSiteConfig,
content,
site
});
Expand All @@ -96,9 +93,9 @@ export const get = (req: Request) => {
const absoluteUrl = pageUrl({ path: content._path, type: "absolute" });

let ogUrl: string;
if (siteConfig.baseUrl) {
if (appOrSiteConfig.baseUrl) {
ogUrl = prependBaseUrl({
baseUrl: siteConfig.baseUrl,
baseUrl: appOrSiteConfig.baseUrl,
contentPath: content._path,
sitePath: site._path
});
Expand All @@ -110,9 +107,9 @@ export const get = (req: Request) => {
let canonical = null;
const contentForCanonicalUrl = getContentForCanonicalUrl(content);
if (contentForCanonicalUrl) {
if (siteConfig.baseUrl) {
if (appOrSiteConfig.baseUrl) {
canonical = prependBaseUrl({
baseUrl: siteConfig.baseUrl,
baseUrl: appOrSiteConfig.baseUrl,
contentPath: contentForCanonicalUrl
? contentForCanonicalUrl._path
: content._path,
Expand All @@ -128,12 +125,11 @@ export const get = (req: Request) => {
}

const imageUrl = getImageUrl({
applicationConfig: app.config, // NOTE: Using app.config is fine, since it's outside Guillotine Execution Context
applicationKey: app.name, // NOTE: Using app.name is fine, since it's outside Guillotine Execution Context
appOrSiteConfig,
content,
site,
defaultImg: siteConfig.seoImage,
defaultImgPrescaled: siteConfig.seoImageIsPrescaled
defaultImg: appOrSiteConfig.seoImage,
defaultImgPrescaled: appOrSiteConfig.seoImageIsPrescaled
});

params = {
Expand All @@ -143,7 +139,7 @@ export const get = (req: Request) => {
description: description,
image: imageUrl,
canonical,
blockRobots: (siteConfig.blockRobots || getBlockRobots(content))
blockRobots: (appOrSiteConfig.blockRobots || getBlockRobots(content))
},
og: {
type: (isFrontpage ? 'website' : 'article'),
Expand All @@ -159,11 +155,11 @@ export const get = (req: Request) => {
}
},
twitter: {
active: (siteConfig.twitterUsername ? true : false),
active: (appOrSiteConfig.twitterUsername ? true : false),
title: pageTitle,
description: description,
image: imageUrl,
site: siteConfig.twitterUsername || null
site: appOrSiteConfig.twitterUsername || null
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,22 @@ import type {Resolver} from '/lib/types/guillotine';

import {startsWith} from '@enonic/js-utils/string/startsWith';
import {includes as arrayIncludes} from '@enonic/js-utils/array/includes';
import {
get as getContentByKey,
getSite as libsContentGetSite,
} from '/lib/xp/content';
import {getSite as libsContentGetSite} from '/lib/xp/content';
import {
get as getContext,
run as runInContext
} from '/lib/xp/context';

import {siteRelativePath} from '/lib/app-metafields/path/siteRelativePath';
import {prependBaseUrl} from '/lib/app-metafields/url/prependBaseUrl';

import {getAppOrSiteConfig} from '/lib/common/getAppOrSiteConfig';
import {getBlockRobots} from '/lib/common/getBlockRobots';
import {getContentForCanonicalUrl} from '/lib/common/getContentForCanonicalUrl';
import {getLang} from '/lib/common/getLang';
import {getMetaDescription} from '/lib/common/getMetaDescription';
import {getFullTitle} from '/lib/common/getFullTitle';
import {getSiteConfigFromSite} from '/lib/common/getSiteConfigFromSite';
import {getTheConfig} from '/lib/common/getTheConfig';
import {APP_CONFIG, APP_NAME, APP_NAME_PATH, MIXIN_PATH} from '/lib/common/constants';
import { siteRelativePath } from '/lib/app-metafields/path/siteRelativePath';
import {APP_CONFIG, APP_NAME} from '/lib/common/constants';


export const contentMetaFieldsResolver: Resolver<
Expand Down Expand Up @@ -75,37 +72,31 @@ export const contentMetaFieldsResolver: Resolver<
principals
}, () => {
const site = libsContentGetSite({ key: _path });
const description = getMetaDescription({
const appOrSiteConfig = getAppOrSiteConfig({
applicationConfig: APP_CONFIG,
applicationKey: APP_NAME,
content,
site
});
const appOrSiteConfig = getTheConfig({
applicationConfig: APP_CONFIG,
applicationKey: APP_NAME,
const description = getMetaDescription({
appOrSiteConfig,
content,
site
});

const title = getFullTitle({
applicationConfig: APP_CONFIG,
applicationKey: APP_NAME,
appOrSiteConfig,
content,
site
});
const isFrontpage = site._path === _path;
const siteConfig = getSiteConfigFromSite({
applicationKey: APP_NAME,
site
});
const blockRobots = siteConfig.blockRobots || getBlockRobots(content)
const blockRobots = appOrSiteConfig.blockRobots || getBlockRobots(content)

let canonical: string|null = null;
const contentForCanonicalUrl = getContentForCanonicalUrl(content);
if (contentForCanonicalUrl) {
if (appOrSiteConfig.baseUrl) {
canonical = prependBaseUrl({
baseUrl: siteConfig.baseUrl,
baseUrl: appOrSiteConfig.baseUrl,
contentPath: contentForCanonicalUrl._path,
sitePath: site._path
});
Expand All @@ -119,7 +110,7 @@ export const contentMetaFieldsResolver: Resolver<

const url: string = appOrSiteConfig.baseUrl
? prependBaseUrl({
baseUrl: siteConfig.baseUrl,
baseUrl: appOrSiteConfig.baseUrl,
contentPath: content._path,
sitePath: site._path
})

Check warning on line 116 in src/main/resources/guillotine/typeFieldResolvers/contentMetaFieldsResolver.ts

View check run for this annotation

Codecov / codecov/patch

src/main/resources/guillotine/typeFieldResolvers/contentMetaFieldsResolver.ts#L113-L116

Added lines #L113 - L116 were not covered by tests
Expand All @@ -130,9 +121,9 @@ export const contentMetaFieldsResolver: Resolver<

// return <Partial<GraphQLTypeToResolverResult<GraphQLMetafields>>>{
return {
_appOrSiteConfig: appOrSiteConfig,
_content: content,
_site: site,
_siteConfig: siteConfig,
canonical,
description,
locale: getLang(content, site),
Expand All @@ -152,7 +143,7 @@ export const contentMetaFieldsResolver: Resolver<
site: appOrSiteConfig.twitterUsername,
},
verification: {
google: siteConfig.siteVerification || null
google: appOrSiteConfig.siteVerification || null
},
url,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@ import {
get as getContext,
run as runInContext
} 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
_appOrSiteConfig: MetafieldsSiteConfig
_content: Content
_site: Site<MetafieldsSiteConfig>
_siteConfig: MetafieldsSiteConfig
}
> = (env) => {
// log.info(`resolvers content metafields ${JSON.stringify(env, null, 4)}`);
Expand All @@ -37,9 +35,9 @@ export const metaFieldsImagesResolver: Resolver<
// siteKey // NOTE: Can be undefined when x-guillotine-sitekey is missing
} = localContext;
const {
_appOrSiteConfig,
_content,
_site,
_siteConfig
} = source;
const context = getContext();
// log.info('metaFieldsImagesResolver context: %s', JSON.stringify(context, null, 4));
Expand All @@ -64,9 +62,9 @@ export const metaFieldsImagesResolver: Resolver<
}, () => {

const imageId = getImageId({
appOrSiteConfig: _appOrSiteConfig,
content: _content,
site: _site,
siteConfig: _siteConfig
});
if (imageId) {
const imageContent = getContentByKey({ key: imageId });
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/lib/common/findImageIdInContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ import {


export function findImageIdInContent({
appOrSiteConfig,
content,
siteConfig
}: {
appOrSiteConfig: MetafieldsSiteConfig
content: Content
siteConfig: MetafieldsSiteConfig
}): ImageId|undefined {
if(content.x?.[APP_NAME_PATH]?.[MIXIN_PATH]?.seoImage) {
return ImageIdBuilder.from(content.x[APP_NAME_PATH][MIXIN_PATH].seoImage as string);
}

Check warning on line 30 in src/main/resources/lib/common/findImageIdInContent.ts

View check run for this annotation

Codecov / codecov/patch

src/main/resources/lib/common/findImageIdInContent.ts#L29-L30

Added lines #L29 - L30 were not covered by tests

const userDefinedPaths = CommaSeparatedStringBuilder.from(siteConfig.pathsImages || '');
const userDefinedPaths = CommaSeparatedStringBuilder.from(appOrSiteConfig.pathsImages || '');
const userDefinedArray = userDefinedPaths ? commaStringToArray(userDefinedPaths) : [];
const userDefinedValue = userDefinedPaths ? findValueInObject(content, userDefinedArray, siteConfig.fullPath) : null;
const userDefinedValue = userDefinedPaths ? findValueInObject(content, userDefinedArray, appOrSiteConfig.fullPath) : null;

const firstItem = forceArray(userDefinedValue)[0];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type {Site} from '/lib/xp/portal';
import type {MetafieldsSiteConfig} from '/lib/types/MetafieldsSiteConfig';


import {isSet} from '@enonic/js-utils/value/isSet';
// import {toStr} from '@enonic/js-utils/value/toStr';
import {getSiteConfig as libPortalGetSiteConfig} from '/lib/xp/portal';
import {getSiteConfigFromSite} from '/lib/common/getSiteConfigFromSite';

Expand All @@ -14,28 +16,42 @@ interface GetTheConfigParams {


// The configuration needs to be fetched first from site config (using current content if site context is not available - like for widgets), and lastly we'll check for any config files and use these to overwrite.
export const getTheConfig = ({
export const getAppOrSiteConfig = ({
applicationConfig,
applicationKey,
site,
}: GetTheConfigParams): MetafieldsSiteConfig => {
let config = libPortalGetSiteConfig<MetafieldsSiteConfig>();
if (!config) {
config = getSiteConfigFromSite({
applicationKey,
site,
});
}
if (applicationConfig && !config.disableAppConfig) {
let appOrSiteConfig: MetafieldsSiteConfig = {}

if (applicationConfig) {
// log.info('applicationConfig:%s', toStr(applicationConfig));
for (let prop in applicationConfig) {
let value: string|boolean = applicationConfig[prop];
if (prop !== 'config.filename' && prop !== 'service.pid') { // Default props for .cfg-files, not to use further.
if (value === 'true' || value === 'false') {
value = value === 'true';
}
(config as Record<typeof prop, typeof value>)[prop] = value;
(appOrSiteConfig as Record<typeof prop, typeof value>)[prop] = value;
}
}

Check warning on line 36 in src/main/resources/lib/common/getAppOrSiteConfig.ts

View check run for this annotation

Codecov / codecov/patch

src/main/resources/lib/common/getAppOrSiteConfig.ts#L29-L36

Added lines #L29 - L36 were not covered by tests
}
return config;

let siteConfig = libPortalGetSiteConfig<MetafieldsSiteConfig>();
if (!siteConfig) {
siteConfig = getSiteConfigFromSite({
applicationKey,
site,
});
}

Check warning on line 45 in src/main/resources/lib/common/getAppOrSiteConfig.ts

View check run for this annotation

Codecov / codecov/patch

src/main/resources/lib/common/getAppOrSiteConfig.ts#L41-L45

Added lines #L41 - L45 were not covered by tests

// log.info('siteConfig:%s', toStr(siteConfig));
for (let key in siteConfig) {
const value = siteConfig[key as keyof typeof siteConfig];
if (isSet(value)) {
(appOrSiteConfig as Record<typeof key, typeof value>)[key] = value;
}
}

// log.info('config:%s', toStr(config));
return appOrSiteConfig;
};
Loading

0 comments on commit 468f06d

Please sign in to comment.