relative article link

This commit is contained in:
Sergey Yarkov 2025-11-16 21:55:01 +03:00
parent a9061822f0
commit d0bfccd42f
6 changed files with 34 additions and 23 deletions

View File

@ -6,16 +6,19 @@ import "./ArticleItem.scss";
export type ArticleItemProps = {
id: number;
title: string;
pubDate: Date;
pubDate: string;
pageLang: string;
articleLang: string;
slug: string;
};
const ArticleItem: Component<ArticleItemProps> = (props) => {
const articleLang = "ru";
// const href = createRelativeArticleUrl(
// { id: props.id, title: props.title, pubDate: props.pubDate },
// URL_BLOG_PREFIX
// );
const href = createRelativeArticleUrl(
{ pubDate: props.pubDate, slug: props.slug, articleLang: props.articleLang },
URL_BLOG_PREFIX
);
return (
<article
@ -24,14 +27,14 @@ const ArticleItem: Component<ArticleItemProps> = (props) => {
itemtype="http://schema.org/BlogPosting"
class="article-item"
>
<a href={""}>
<a href={href}>
<div class="flex">
<h3 itemprop="headline">{props.title}</h3>
{articleLang !== props.pageLang && <sup>{articleLang.toLocaleUpperCase()}</sup>}
</div>
<p>
<time datetime={`props.pubDate.toISOString()`}>
{/* {createDateFormatter(props.pageLang).format(props.pubDate)} */}
<time datetime={props.pubDate}>
{createDateFormatter(props.pageLang).format(new Date(props.pubDate))}
</time>
</p>
</a>

View File

@ -75,7 +75,7 @@ const ArticlesList: Component<ArticlesListProps> = (props) => {
{(year) => (
<div class="articles-list__wrapper">
<h2>{year}</h2>
<For each={articles()[year]}>
{/* <For each={articles()[year]}>
{({ id, title, pubDate }) => (
<ArticleItem
id={id}
@ -84,7 +84,7 @@ const ArticlesList: Component<ArticlesListProps> = (props) => {
pageLang={props.pageLang}
/>
)}
</For>
</For> */}
</div>
)}
</For>

View File

@ -21,15 +21,13 @@ const { articles } = Astro.props;
{articles
.filter((a) => !a.id.startsWith(`${pageLang}/`))
.map(({ id, data: { title, pubDate } }) => (
<a href={`${createRelativeArticleUrl({ id, title, pubDate }, URL_BLOG_PREFIX)}`}>{`${
languages[id.split("/")[0] as LanguageKeys]
}`}</a>
<a href={`...`}>{`${languages[id.split("/")[0] as LanguageKeys]}`}</a>
))}
</div>
)
}
<style lang="scss">
<style lang='scss'>
div {
margin: 25px 0;
font-size: var(--font-size-lg);

View File

@ -14,7 +14,10 @@ export interface Props {
}
type RecentArticlesQueryResult = {
article_translations: Pick<ArticleTranslation, "id" | "title" | "pub_date">[];
article_translations: Pick<
ArticleTranslation,
"id" | "title" | "slug" | "pub_date" | "languages_code"
>[];
};
const data = await directus.query<RecentArticlesQueryResult>(`
@ -22,18 +25,23 @@ const data = await directus.query<RecentArticlesQueryResult>(`
article_translations {
id
title
slug
pub_date
languages_code {
code
name
}
}
}
`);
const { article_translations: articles } = data;
console.log(data);
// console.log();
const { length = siteconfig.articles.recentLimit } = Astro.props as Props;
// const { length = siteconfig.articles.recentLimit } = Astro.props as Props;
const pageLang = getLanguageFromURL(Astro.url.pathname);
const filtered = utils.removeDuplicates(articles.slice(0, length), pageLang);
// const filtered = utils.removeDuplicates(articles.slice(0, length), pageLang);
const isEmpty = articles.length === 0;
---
@ -54,6 +62,8 @@ const isEmpty = articles.length === 0;
id={article.id}
title={article.title}
pubDate={article.pub_date}
slug={article.slug}
articleLang={article.languages_code.code.split("-")[0]}
pageLang={pageLang}
/>
))

View File

@ -16,7 +16,7 @@ export async function GET() {
title,
pubDate,
description: `${description}${coverImage && `<img src="${coverImage}" alt="${title}">`}`,
link: createRelativeArticleUrl({ id, title, pubDate }, URL_BLOG_PREFIX),
// link: createRelativeArticleUrl({ id, title, pubDate }, URL_BLOG_PREFIX),
customData: `<language>en-us</language>`,
};
}),

View File

@ -36,13 +36,13 @@ export function removeDuplicates(
* `/en/blog/2023-01-01/my-first-article`
*/
export function createRelativeArticleUrl(
params: { id: string; title: string; pubDate: Date },
params: { slug: string; articleLang: string; pubDate: string },
prefix: string = "blog"
): string {
const articleLang = params.id.split("/")[0];
const date = params.pubDate.toISOString().split("T")[0];
const { pubDate, articleLang, slug } = params;
const date = pubDate.split("T")[0];
const lang = articleLang !== DEFAULT_LANGUAGE ? "/" + articleLang : "";
return `${lang}/${prefix}/${date}/${slugify(params.title, { lowercase: true })}`;
return `${lang}/${prefix}/${date}/${slug}`;
}
export function formatToArticleBlocks(articles: CollectionEntry<"blog">[]) {