import { find, map, filter } from "lodash"; // import { FetchedData } from "../fetchPublicData"; import getLocales from "../helpers/getLocales"; import createPaths from "../helpers/createPaths"; import createFilenames from "../helpers/createFilenames"; import templatePaths from "../helpers/templatePaths"; import chunksByPage from "../helpers/chunksByPage"; import createCommonConfig from "../helpers/createCommonConfig"; import getPageAndGlobalTranslations from "../helpers/getPageAndGlobalTranslations"; import formatDate from "../helpers/formatDate"; import getLatestPostsByLocaleFromPostResponse from "../helpers/getLatestPostsByLocaleFromPostResponse"; import { LatestPostData } from "../types"; import getUncategorizedCategoryProperties from "../helpers/getUncategorizedCategoryProperties"; import createMediaUrl from "../helpers/createMediaUrl"; const HtmlWebpackPlugin = require("html-webpack-plugin"); type SinglePostData = { locale: string; slug: string; title: string; metaDescription: string; metaTitle: string; ogTitle: string; ogDescription: string; ogImage: string | null; blocks: object[]; author: string; updatedAt: string; category: { translation: null | { name: string; link: string; }; slug: string; } | null; availableLocales: string[]; latestPosts: LatestPostData[]; }; const createUncategorizedSinglePostCategory: (locale: string) => SinglePostData["category"] = locale => { const props = getUncategorizedCategoryProperties(locale); return { translation: { name: props.name, link: `${createPaths.getPublicUrl()}/${createPaths.category(locale, props.slug)}` }, slug: props.slug }; }; const singlePosts = (responses: FetchedData): typeof HtmlWebpackPlugin[] => { const { postsResponse, categoriesResponse } = responses; const latestPostsByLocale = getLatestPostsByLocaleFromPostResponse(postsResponse, categoriesResponse, getLocales()); const posts = postsResponse.data.items; const categories = categoriesResponse.data.items; const singlePosts: SinglePostData[] = []; posts.forEach(post => { getLocales().forEach(locale => { const postTranslation = find(post.translations, { locale }); if (!postTranslation) return; const commonPostData = { locale, slug: post.slug, title: postTranslation.title, metaDescription: postTranslation.metaDescription, metaTitle: postTranslation.metaTitle, ogTitle: postTranslation.ogTitle, ogDescription: postTranslation.ogDescription, ogImage: post.ogImage ? createMediaUrl(post.ogImage.fileName) : null, blocks: JSON.parse(postTranslation.content).blocks, author: post.author, updatedAt: formatDate(postTranslation.updatedAt, locale), availableLocales: map(post.translations, postTranslation => postTranslation.locale), latestPosts: latestPostsByLocale[locale].filter(post => post.translationId !== postTranslation.id) }; // no category is associated with it if (!post.category) { singlePosts.push({ ...commonPostData, category: createUncategorizedSinglePostCategory(locale) }); return; } const category = find(categories, { id: post.category }); const categoryTranslation = !category ? null : find(category.translations, { locale }); if (category && categoryTranslation) { singlePosts.push({ ...commonPostData, category: { translation: { name: categoryTranslation.name, link: `${createPaths.getPublicUrl()}/${createPaths.category(locale, category.slug)}` }, slug: category.slug } }); } else if (category) { singlePosts.push({ ...commonPostData, category: { translation: null, slug: category.slug } }); } }); }); return map( singlePosts, singlePost => new HtmlWebpackPlugin({ filename: createFilenames.post(singlePost.locale, singlePost.category.slug, singlePost.slug), template: templatePaths.post, chunks: chunksByPage.post, templateParameters: { ...createCommonConfig(singlePost.locale, singlePost.availableLocales, locale => createPaths.post(locale, singlePost.category.slug, singlePost.slug) ), post: singlePost, i18n: getPageAndGlobalTranslations(singlePost.locale, "single-post") } }) ); }; export default singlePosts;