Skip to content

Commit 8ae5325

Browse files
authored
Merge pull request #59 from pachicodes/feat/blog-phase-2-helpers
feat(blog): phase 2 helpers and i18n
2 parents a0332a8 + 0e15084 commit 8ae5325

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

src/lib/blog.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { getCollection, type CollectionEntry } from 'astro:content';
2+
import type { Lang } from './i18n';
3+
4+
export type BlogEntry = CollectionEntry<'blog'>;
5+
6+
export async function getPublishedPosts(lang: Lang): Promise<BlogEntry[]> {
7+
const posts = await getCollection('blog');
8+
return posts
9+
.filter((post) => post.data.lang === lang && !post.data.draft)
10+
.sort(
11+
(a, b) =>
12+
b.data.publishedAt.getTime() - a.data.publishedAt.getTime(),
13+
);
14+
}
15+
16+
export async function getPostBySlug(
17+
lang: Lang,
18+
slug: string,
19+
): Promise<BlogEntry | undefined> {
20+
const posts = await getCollection('blog');
21+
const post = posts.find(
22+
(entry) => entry.data.lang === lang && entry.data.slug === slug,
23+
);
24+
if (!post || post.data.draft) return undefined;
25+
return post;
26+
}

src/lib/i18n.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ export const ui = {
9999
sessions: 'Aulas',
100100
sessionsAnchor: '#sessoes',
101101
},
102+
blog: {
103+
title: 'Blog',
104+
breadcrumb: 'Blog',
105+
publishedAt: 'Publicado em',
106+
tags: 'Tags',
107+
},
102108
project: {
103109
title: 'Projeto Prático',
104110
heroLabel: 'Mão na massa',
@@ -233,6 +239,12 @@ export const ui = {
233239
sessions: 'Lessons',
234240
sessionsAnchor: '#sessions',
235241
},
242+
blog: {
243+
title: 'Blog',
244+
breadcrumb: 'Blog',
245+
publishedAt: 'Published',
246+
tags: 'Tags',
247+
},
236248
project: {
237249
title: 'Hands-on Project',
238250
heroLabel: 'Hands-on',
@@ -323,6 +335,12 @@ export function getSessionHref(lang: Lang, slug: string): string {
323335
return withBase(`${prefix}sessions/${slug}`);
324336
}
325337

338+
export function getBlogHref(lang: Lang, slug?: string): string {
339+
const prefix = lang === defaultLang ? '' : `${languages[lang].pathPrefix}/`;
340+
const base = `${prefix}blog`;
341+
return slug ? withBase(`${base}/${slug}`) : withBase(`${base}/`);
342+
}
343+
326344
const projectSlugs: Record<Lang, string> = {
327345
'pt-BR': 'projeto',
328346
en: 'project',

0 commit comments

Comments
 (0)