Skip to content

Commit

Permalink
Merge pull request #17 from zankhq/feat/blog-paths
Browse files Browse the repository at this point in the history
Refactor dynamic page paths and localize blog posts
  • Loading branch information
zanhk authored Aug 12, 2023
2 parents 8738b81 + f41ecc0 commit 986e281
Show file tree
Hide file tree
Showing 16 changed files with 235 additions and 241 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "astros",
"type": "module",
"version": "0.0.4",
"version": "1.0.0",
"private": true,
"scripts": {
"dev": "astro dev --host",
Expand Down
2 changes: 1 addition & 1 deletion scripts/change-language-in-dynamic-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from "path";

const locales = ["it"];
const defaultLocale = "en";
const paths = ["[...blog]/[...page]", "[...blog]/[category]/[...page]", "[...blog]/[tag]/[...page]", "[...blog]/index"];
const paths = ["blog/[...page]", "blog/category/[category]", "blog/tag/[tag]", "blog/[slug]"];

async function main() {
const __dirname = path.resolve();
Expand Down
2 changes: 1 addition & 1 deletion src/components/blog/posts.astro
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const { posts } = Astro.props;
</div>

<h3 class="mt-3 text-lg font-semibold leading-6 text-white">
<a href={localizePath(`/${blogPostEntry.slug}`)}>
<a href={`${localizePath(`blog/${blogPostEntry.slug.split("/").slice(1).join("/")}`)}`}>
<span class="absolute inset-0" />
{blogPostEntry.data.title}
</a>
Expand Down
51 changes: 0 additions & 51 deletions src/pages/[...blog]/[category]/[...page].astro

This file was deleted.

46 changes: 0 additions & 46 deletions src/pages/[...blog]/[tag]/[...page].astro

This file was deleted.

File renamed without changes.
42 changes: 21 additions & 21 deletions src/pages/[...blog]/index.astro → src/pages/blog/[slug].astro
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,42 @@ import { getArticleReadingTime } from "@utils/blog";
changeLanguage("en");
// Generate a new path for every collection entry
// Generate a new path for every collection post
export async function getStaticPaths() {changeLanguage('en')
const blogEntries = await getCollection("blog");
return blogEntries.map((entry) => {
let slug = entry.slug;
if (i18next.language == "en" && slug.startsWith("en/")) {
slug = slug.substring(3);
}
var localizedBlogEntries = blogEntries.filter((entry) => entry.slug.startsWith(i18next.language));
return localizedBlogEntries.map((post) => {
let slug = post.slug;
slug = slug.substring(3).replaceAll("blog/", "");
return {
params: { blog: slug },
props: { entry, lang: i18next.language },
params: { slug: slug },
props: { post: post },
};
});
}
// Get the entry directly from the prop on render
const { entry, lang } = Astro.props;
const { Content } = await entry.render();
const readTime = getArticleReadingTime(entry.body);
// Get the post directly from the prop on render
const { post, lang } = Astro.props;
const { data, render } = post;
const { Content } = await render();
const readTime = getArticleReadingTime(post.body);
---

<Layout title={entry.data.title} lang={lang}>
<Layout title={post.data.title} lang={lang}>
<Container>
<div class="mx-auto prose prose-lg dark:prose-invert mt-14">
<a
href={localizePath(`/category/${entry.data.category?.toLowerCase()}`)}
href={localizePath(`/blog/category/${post.data.category?.toLowerCase()}`)}
class="text-blue-400 dark:text-stone-400 uppercase tracking-wider text-sm font-medium no-underline">
{entry.data.category}
{post.data.category}
</a>
<h1 class="text-4xl mb-3 lg:text-5xl font-bold lg:tracking-tight mt-1 lg:leading-tight dark:text-white">
{entry.data.title}
{post.data.title}
</h1>
<div class="flex gap-2 items-center">
<div class="w-full md:w-auto flex flex-wrap gap-3">
{
entry.data.tags.map((tag) => (
<a href={localizePath(`/tag/${tag}`)} class="text-sm text-gray-500 dark:text-stone-300">
post.data.tags.map((tag) => (
<a href={localizePath(`/blog/tag/${tag?.toLowerCase()}`)} class="text-sm text-gray-500 dark:text-stone-300">
#{tag}
</a>
))
Expand All @@ -53,15 +53,15 @@ const readTime = getArticleReadingTime(entry.body);
</div>
<div class="flex gap-2 mt-4 items-center flex-wrap md:flex-nowrap">
<span class="text-gray-400 dark:text-stone-400">
{entry.data.author}
{post.data.author}
</span>
<span class="text-gray-400 dark:text-stone-400">•</span>
<span class="text-gray-400 dark:text-stone-400">
{readTime} min
</span>
<span class="text-gray-400 dark:text-stone-400">•</span>
<time class="text-gray-400 dark:text-stone-400" datetime={entry.data.publishDate.toISOString()}>
{entry.data.publishDate.toDateString()}
<time class="text-gray-400 dark:text-stone-400" datetime={post.data.publishDate.toISOString()}>
{post.data.publishDate.toDateString()}
</time>
</div>
</div>
Expand Down
47 changes: 47 additions & 0 deletions src/pages/blog/category/[category].astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
import i18next, { changeLanguage } from "i18next";
import { getCollection } from "astro:content";
import Layout from "@layouts/Layout.astro";
import Container from "@components/container.astro";
import Posts from "@components/blog/posts.astro";
import Sectionhead from "@components/sectionhead.astro";
import Pagination from "@components/blog/pagination.astro";
changeLanguage("en");
export async function getStaticPaths() {changeLanguage('en')
const posts = await getCollection("blog", ({ data }) => {
return !data.draft && data.publishDate < new Date();
});
const localizedPosts = posts.filter((page) => {
const [lang, ...slug] = page.slug.split("/");
return lang === i18next.language;
});
const categories = new Set();
localizedPosts.map((post) => {
typeof post.data.category === "string" && categories.add(post.data.category.toLowerCase());
});
return Array.from(categories).map((category) => {
return {
params: { category: category },
props: {
posts: localizedPosts.filter((post) => typeof post.data.category === "string"),
category: category,
lang: i18next.language,
},
};
});
}
// Get the post directly from the prop on render
const { posts, category, lang } = Astro.props;
---

<Layout title="Blog">
<Container>
<Sectionhead>
<Fragment slot="title">#{category}</Fragment>
<Fragment slot="desc">We write about building startups and thoughts going on our mind.</Fragment>
</Sectionhead>
<Posts posts={posts} />
</Container>
</Layout>
47 changes: 47 additions & 0 deletions src/pages/blog/tag/[tag].astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
import i18next, { changeLanguage } from "i18next";
import { getCollection } from "astro:content";
import Layout from "@layouts/Layout.astro";
import Container from "@components/container.astro";
import Posts from "@components/blog/posts.astro";
import Sectionhead from "@components/sectionhead.astro";
import Pagination from "@components/blog/pagination.astro";
changeLanguage("en");
export async function getStaticPaths() {changeLanguage('en')
const posts = await getCollection("blog", ({ data }) => {
return !data.draft && data.publishDate < new Date();
});
const localizedPosts = posts.filter((page) => {
const [lang, ...slug] = page.slug.split("/");
return lang === i18next.language;
});
const tags = new Set();
localizedPosts.map((post) => {
Array.isArray(post.data.tags) && post.data.tags.map((tag) => tags.add(tag.toLowerCase()));
});
return Array.from(tags).map((tag) => {
return {
params: { tag: tag },
props: {
posts: localizedPosts.filter((post) => Array.isArray(post.data.tags) && post.data.tags.find((elem) => elem.toLowerCase() === tag)),
tag: tag,
lang: i18next.language,
},
};
});
}
// Get the post directly from the prop on render
const { posts, tag, lang } = Astro.props;
---

<Layout title="Blog">
<Container>
<Sectionhead>
<Fragment slot="title">#{tag}</Fragment>
<Fragment slot="desc">We write about building startups and thoughts going on our mind.</Fragment>
</Sectionhead>
<Posts posts={posts} />
</Container>
</Layout>
51 changes: 0 additions & 51 deletions src/pages/it/[...blog]/[category]/[...page].astro

This file was deleted.

Loading

0 comments on commit 986e281

Please sign in to comment.