-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
103 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<span class="mx-1 my-2 bg-accent/70 rounded-xl px-2 w-fit"> | ||
<span class="mx-1 my-2 bg-accent/70 rounded-xl py-1 px-2"> | ||
<slot /> | ||
</span> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
import IPost from '../../../types/post'; | ||
import { getPostsCategories, filterPosts } from '../../../repositories/blog'; | ||
import Layout from '../../../layouts/Layout.astro'; | ||
import { format } from 'date-fns'; | ||
export const getStaticPaths = async () => { | ||
const posts = await Astro.glob<IPost>('../posts/[!_]*.md'); | ||
const tags = getPostsCategories(posts); | ||
return Object.entries(tags).map(([tag, n]) => { | ||
return { | ||
params: { tag } | ||
} | ||
}); | ||
}; | ||
const { tag } = Astro.params; | ||
const posts = filterPosts(await Astro.glob<IPost>('../posts/[!_]*.md'), { tag }).reverse(); | ||
--- | ||
|
||
<Layout title="Blog"> | ||
<div class="mt-10 mb-16"> | ||
<h1 class="font-pixel text-3xl text-center mb-2">Gioele's blog</h1> | ||
<span class="text-sm text-white/60 text-center block">aka: a nerd's blog</span> | ||
</div> | ||
<div class="mt-10"> | ||
<div> | ||
<h3 class="font-pixel text-xl mb-3 leading-loose sm:leading-normal">Latest posts tagged <span class="bg-white/5 -ml-2 pl-3 p-2 rounded">{tag}</span></h3> | ||
<div class="space-y-5 divide-y divide-white/10"> | ||
{ posts.map((p) => ( | ||
<article class="pt-5 first:pt-0"> | ||
<a href={p.url}> | ||
<h3 class="text-2xl mb-2">{ p.frontmatter.title } <span class="text-xs text-white/60"> - { format(p.frontmatter.pubDate, 'dd/MM/yyyy') }</span></h3> | ||
<p class="text-white/80 text-sm">{ p.frontmatter.description }</p> | ||
</a> | ||
</article> | ||
)) } | ||
</div> | ||
</div> | ||
</div> | ||
</Layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import type { MarkdownInstance } from 'astro'; | ||
import type IPost from '../types/post'; | ||
import { isAfter, isBefore } from 'date-fns'; | ||
|
||
interface ITags { | ||
[name: string]: number | ||
} | ||
|
||
type Posts = MarkdownInstance<IPost>[]; | ||
|
||
export const getPostsCategories = (posts: Posts) : ITags => { | ||
const tags: ITags = {}; | ||
|
||
posts.forEach((p) => { | ||
p.frontmatter.tags.forEach((t) => { | ||
if (tags[t] === undefined) { | ||
tags[t] = 1; | ||
} else { | ||
tags[t]++; | ||
} | ||
}); | ||
}); | ||
|
||
return tags; | ||
} | ||
|
||
interface PostFilter { | ||
tag?: string; | ||
date?: { | ||
after?: string | number | Date; | ||
before?: string | number | Date | ||
} | ||
} | ||
|
||
export const filterPosts = (posts: Posts, filter?: PostFilter): Posts => { | ||
return posts.filter((p) => | ||
(filter?.tag === undefined ? true : p.frontmatter.tags.includes(filter.tag)) && | ||
(filter?.date?.after === undefined ? true : isAfter(p.frontmatter.pubDate, filter.date.after)) && | ||
(filter?.date?.before === undefined ? true : isBefore(p.frontmatter.pubDate, filter.date.before)) | ||
); | ||
} |