Skip to content

Commit

Permalink
Merge branch 'ft/codeforafrica-posts-cms' into ft/codeforafrica-stori…
Browse files Browse the repository at this point in the history
…es-cms

Signed-off-by: Kipruto <[email protected]>
  • Loading branch information
kelvinkipruto committed Sep 22, 2023
2 parents 7642b94 + c55ef6c commit bc150d5
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 4 deletions.
2 changes: 2 additions & 0 deletions apps/codeforafrica/jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ global.TextDecoder = jest.fn().mockImplementation(() => ({
decode: jest.fn(),
}));

process.env.NEXT_PUBLIC_APP_URL = "http://localhost:3000";

jest.mock("next/router", () => ({
useRouter: jest.fn().mockImplementation(() => ({
asPath: "",
Expand Down
3 changes: 1 addition & 2 deletions apps/codeforafrica/payload.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export default buildConfig({
Partners,
Stories,
Tags,
,
] as CollectionConfig[],
globals: [Settings] as GlobalConfig[],
admin: {
Expand Down Expand Up @@ -72,7 +71,7 @@ export default buildConfig({
},
}),
seo({
collections: [],
collections: ["pages"],
globals: [],
uploadsCollection: "media",
generateTitle: ({ doc }: any) => doc?.title?.value as string,
Expand Down
3 changes: 3 additions & 0 deletions apps/codeforafrica/src/lib/data/common/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import blockify from "@/codeforafrica/lib/data/blockify";
import pagify from "@/codeforafrica/lib/data/pagify";
import getPageSeoFromMeta from "@/codeforafrica/lib/data/seo";
import { imageFromMedia } from "@/codeforafrica/lib/data/utils";

function getNavBar(settings) {
Expand Down Expand Up @@ -161,10 +162,12 @@ export async function getPageProps(api, context) {
const navbar = getNavBar(settings);
const footer = getFooter(settings);

const seo = getPageSeoFromMeta(page, settings);
return {
blocks,
footer,
navbar,
seo,
};
}

Expand Down
67 changes: 67 additions & 0 deletions apps/codeforafrica/src/lib/data/seo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import site from "@/codeforafrica/utils/site";

function stringifyDescription(description) {
if (!Array.isArray(description)) {
return "";
}
return description.reduce((result, item) => {
if (item.text) {
// eslint-disable-next-line no-param-reassign
result += item.text;
}

if (Array.isArray(item.children)) {
// eslint-disable-next-line no-param-reassign
result += stringifyDescription(item.children);
}
return result;
}, "");
}

export default function getPageSeoFromMeta(page, settings) {
const { title: pageTitle, meta: pageMeta } = page;
const {
title: metaTitle,
description: metaDescription,
image = {},
} = pageMeta;
const { title: siteTitle, description: siteDescription } = settings;
const title =
metaTitle ||
pageTitle ||
siteTitle ||
process.env.NEXT_PUBLIC_APP_NAME ||
null;
const description =
metaDescription || stringifyDescription(siteDescription) || null;
const titleTemplate = siteTitle ? `%s | ${siteTitle}` : null;
const defaultTitle = siteTitle || null;
const canonical = site.url.replace(/\/+$/, "");
const openGraph = {
title,
description,
type: "website",
site_name: siteTitle,
};
if (image.url) {
const { alt, height, mimeType: type, url, width } = image;
openGraph.images = [
{
alt: alt || title || defaultTitle,
height,
type,
url,
width,
},
];
}

return {
title,
titleTemplate,
defaultTitle,
description,
canonical,
openGraph,
};
}
2 changes: 1 addition & 1 deletion apps/codeforafrica/src/payload/collections/Tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Tags = {
useAsTitle: "name",
},
access: {
read: () => true, // Everyone can read Pages
read: () => true,
},
fields: [
{
Expand Down
2 changes: 1 addition & 1 deletion apps/codeforafrica/turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
"build-next": {
"outputs": [".next/**", "!.next/cache/**", "dist/**"],
"env": []
"env": ["NEXT_PUBLIC_APP_NAME", "NEXT_PUBLIC_APP_URL"]
},
"build-payload": {
"outputs": ["build/**"],
Expand Down

0 comments on commit bc150d5

Please sign in to comment.