mirror of
https://github.com/sergeyyarkov/yarkov.tech.git
synced 2026-10-11 10:29:21 +03:00
50 lines
1.5 KiB
TypeScript
50 lines
1.5 KiB
TypeScript
import { DEFAULT_LANGUAGE } from "@root/constants";
|
|
import { Article_Translations, ArticleQuery } from "../graphql/graphql";
|
|
|
|
/**
|
|
* If there is a translation of an article in the current language of the page,
|
|
* the remaining articles with other translations should be removed.
|
|
*/
|
|
export function filterArticlesByPageLang<T>(
|
|
articles: Array<T & Pick<Article_Translations, "languages_code">>,
|
|
pageLang: LanguageKeys
|
|
) {
|
|
if (!articles) return [];
|
|
if (articles && articles.length >= 2) {
|
|
const pageLangArticles = articles.filter((a) => a && a.languages_code?.code.split("-")[0] === pageLang);
|
|
if (pageLangArticles.length !== 0) return pageLangArticles;
|
|
return articles;
|
|
}
|
|
return articles;
|
|
}
|
|
|
|
/**
|
|
* This will create a relative url based on article entry.
|
|
* `/en/blog/2023-01-01/my-first-article`
|
|
*/
|
|
export function createRelativeArticleUrl(
|
|
params: { slug: string; articleLang: string; pubDate: string },
|
|
prefix: string = "blog"
|
|
): string {
|
|
const { pubDate, articleLang, slug } = params;
|
|
const date = pubDate.split("T")[0];
|
|
const lang = articleLang !== DEFAULT_LANGUAGE ? "/" + articleLang : "";
|
|
return `${lang}/${prefix}/${date}/${slug}`;
|
|
}
|
|
|
|
export function formatToArticleBlocks<T>(articles: Array<T & Pick<Article_Translations, "pub_date">>) {
|
|
const blocks: Record<string, Array<T & Pick<Article_Translations, "pub_date">>> = {};
|
|
|
|
if (!articles) return {};
|
|
|
|
for (const a of articles) {
|
|
if (a) {
|
|
const year = new Date(a?.pub_date).getFullYear();
|
|
if (!blocks[year]) blocks[year] = [];
|
|
blocks[year].push(a);
|
|
}
|
|
}
|
|
|
|
return blocks;
|
|
}
|