From e11e194625d95892745ae23e1b8c522a6fda3258 Mon Sep 17 00:00:00 2001 From: Giulio Zanchetta Date: Fri, 11 Aug 2023 21:59:13 +0200 Subject: [PATCH 1/7] Refactor dynamic page paths and localize blog posts - Refactored dynamic page paths in the `change-language-in-dynamic-pages.js` script to use a simplified format. - Updated the path for blog post links in the `posts.astro` component to include localized paths. - Renamed and reorganized files related to dynamic blog pages, including categories and tags, to follow a consistent structure. - Modified the logic for generating static paths in the `[...page].astro` files to include the "blog" prefix. - Updated rendering logic in the `[slug].astro` files to retrieve and display post data correctly based on language selection. These changes improve code organization and ensure that blog posts are properly localized. --- scripts/change-language-in-dynamic-pages.js | 2 +- src/components/blog/posts.astro | 2 +- src/pages/{[...blog] => blog}/[...page].astro | 1 + .../[category]/[...page].astro | 0 .../index.astro => blog/[slug].astro} | 41 ++++++++++--------- .../{[...blog] => blog}/[tag]/[...page].astro | 0 .../it/{[...blog] => blog}/[...page].astro | 1 + .../[category]/[...page].astro | 0 .../index.astro => blog/[slug].astro} | 41 ++++++++++--------- .../{[...blog] => blog}/[tag]/[...page].astro | 0 10 files changed, 46 insertions(+), 42 deletions(-) rename src/pages/{[...blog] => blog}/[...page].astro (98%) rename src/pages/{[...blog] => blog}/[category]/[...page].astro (100%) rename src/pages/{[...blog]/index.astro => blog/[slug].astro} (75%) rename src/pages/{[...blog] => blog}/[tag]/[...page].astro (100%) rename src/pages/it/{[...blog] => blog}/[...page].astro (98%) rename src/pages/it/{[...blog] => blog}/[category]/[...page].astro (100%) rename src/pages/it/{[...blog]/index.astro => blog/[slug].astro} (75%) rename src/pages/it/{[...blog] => blog}/[tag]/[...page].astro (100%) diff --git a/scripts/change-language-in-dynamic-pages.js b/scripts/change-language-in-dynamic-pages.js index e70bf9c6..2973771f 100644 --- a/scripts/change-language-in-dynamic-pages.js +++ b/scripts/change-language-in-dynamic-pages.js @@ -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]/[...page]", "blog/[tag]/[...page]", "blog/[slug]"]; async function main() { const __dirname = path.resolve(); diff --git a/src/components/blog/posts.astro b/src/components/blog/posts.astro index 83f51ca1..a18d253f 100644 --- a/src/components/blog/posts.astro +++ b/src/components/blog/posts.astro @@ -38,7 +38,7 @@ const { posts } = Astro.props;

- + {blogPostEntry.data.title} diff --git a/src/pages/[...blog]/[...page].astro b/src/pages/blog/[...page].astro similarity index 98% rename from src/pages/[...blog]/[...page].astro rename to src/pages/blog/[...page].astro index 2b458435..67d4384e 100644 --- a/src/pages/[...blog]/[...page].astro +++ b/src/pages/blog/[...page].astro @@ -21,6 +21,7 @@ export async function getStaticPaths({ paginate }) {changeLanguage('en') if (firstSlug) { lang = firstSlug[1]; } + slug.splice(1, 0, "blog"); return { ...post, slug: slug.join("/"), diff --git a/src/pages/[...blog]/[category]/[...page].astro b/src/pages/blog/[category]/[...page].astro similarity index 100% rename from src/pages/[...blog]/[category]/[...page].astro rename to src/pages/blog/[category]/[...page].astro diff --git a/src/pages/[...blog]/index.astro b/src/pages/blog/[slug].astro similarity index 75% rename from src/pages/[...blog]/index.astro rename to src/pages/blog/[slug].astro index 736d6269..129ff749 100644 --- a/src/pages/[...blog]/index.astro +++ b/src/pages/blog/[slug].astro @@ -9,41 +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); + console.log(slug); 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); --- - +
- {entry.data.category} + {post.data.category}

- {entry.data.title} + {post.data.title}

{ - entry.data.tags.map((tag) => ( + post.data.tags.map((tag) => ( #{tag} @@ -53,15 +54,15 @@ const readTime = getArticleReadingTime(entry.body);
- {entry.data.author} + {post.data.author} {readTime} min -
diff --git a/src/pages/[...blog]/[tag]/[...page].astro b/src/pages/blog/[tag]/[...page].astro similarity index 100% rename from src/pages/[...blog]/[tag]/[...page].astro rename to src/pages/blog/[tag]/[...page].astro diff --git a/src/pages/it/[...blog]/[...page].astro b/src/pages/it/blog/[...page].astro similarity index 98% rename from src/pages/it/[...blog]/[...page].astro rename to src/pages/it/blog/[...page].astro index 35eb57c5..388ed561 100644 --- a/src/pages/it/[...blog]/[...page].astro +++ b/src/pages/it/blog/[...page].astro @@ -21,6 +21,7 @@ export async function getStaticPaths({ paginate }) {changeLanguage('it') if (firstSlug) { lang = firstSlug[1]; } + slug.splice(1, 0, "blog"); return { ...post, slug: slug.join("/"), diff --git a/src/pages/it/[...blog]/[category]/[...page].astro b/src/pages/it/blog/[category]/[...page].astro similarity index 100% rename from src/pages/it/[...blog]/[category]/[...page].astro rename to src/pages/it/blog/[category]/[...page].astro diff --git a/src/pages/it/[...blog]/index.astro b/src/pages/it/blog/[slug].astro similarity index 75% rename from src/pages/it/[...blog]/index.astro rename to src/pages/it/blog/[slug].astro index a97a7b73..a97f3853 100644 --- a/src/pages/it/[...blog]/index.astro +++ b/src/pages/it/blog/[slug].astro @@ -9,41 +9,42 @@ import { getArticleReadingTime } from "@utils/blog"; changeLanguage("it"); -// Generate a new path for every collection entry +// Generate a new path for every collection post export async function getStaticPaths() {changeLanguage('it') 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); + console.log(slug); 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); --- - +
- {entry.data.category} + {post.data.category}

- {entry.data.title} + {post.data.title}

{ - entry.data.tags.map((tag) => ( + post.data.tags.map((tag) => ( #{tag} @@ -53,15 +54,15 @@ const readTime = getArticleReadingTime(entry.body);
- {entry.data.author} + {post.data.author} {readTime} min -
diff --git a/src/pages/it/[...blog]/[tag]/[...page].astro b/src/pages/it/blog/[tag]/[...page].astro similarity index 100% rename from src/pages/it/[...blog]/[tag]/[...page].astro rename to src/pages/it/blog/[tag]/[...page].astro From 2755a7bd3aa73af4e9a2eba9b4e574c1da43a240 Mon Sep 17 00:00:00 2001 From: Giulio Zanchetta Date: Fri, 11 Aug 2023 23:08:51 +0200 Subject: [PATCH 2/7] Refactor blog category and tag paths - Refactored the paths in the `change-language-in-dynamic-pages.js` file to use updated path patterns for blog categories and tags. - Updated the `getStaticPaths` function in the `[slug].astro`, `[category]/[...page].astro`, and `[tag]/[...page].astro` files to use the new path patterns for localized posts. - Renamed the `[category]/[...page].astro` file to `category/[category].astro`. - Renamed the `[tag]/[...page].astro` file to `tag/[tag].astro`. - Updated links in the `[slug].astro`, `category/[category].astro`, and `tag/[tag].astro` files to reflect changes in path patterns. --- scripts/change-language-in-dynamic-pages.js | 2 +- src/pages/blog/[category]/[...page].astro | 51 -------------------- src/pages/blog/[slug].astro | 28 +++++------ src/pages/blog/[tag]/[...page].astro | 46 ------------------ src/pages/blog/category/[category].astro | 48 ++++++++++++++++++ src/pages/blog/tag/[tag].astro | 47 ++++++++++++++++++ src/pages/it/blog/[category]/[...page].astro | 51 -------------------- src/pages/it/blog/[slug].astro | 28 +++++------ src/pages/it/blog/[tag]/[...page].astro | 46 ------------------ src/pages/it/blog/category/[category].astro | 48 ++++++++++++++++++ src/pages/it/blog/tag/[tag].astro | 47 ++++++++++++++++++ src/pwa.ts | 4 +- 12 files changed, 221 insertions(+), 225 deletions(-) delete mode 100644 src/pages/blog/[category]/[...page].astro delete mode 100644 src/pages/blog/[tag]/[...page].astro create mode 100644 src/pages/blog/category/[category].astro create mode 100644 src/pages/blog/tag/[tag].astro delete mode 100644 src/pages/it/blog/[category]/[...page].astro delete mode 100644 src/pages/it/blog/[tag]/[...page].astro create mode 100644 src/pages/it/blog/category/[category].astro create mode 100644 src/pages/it/blog/tag/[tag].astro diff --git a/scripts/change-language-in-dynamic-pages.js b/scripts/change-language-in-dynamic-pages.js index 2973771f..d59441f9 100644 --- a/scripts/change-language-in-dynamic-pages.js +++ b/scripts/change-language-in-dynamic-pages.js @@ -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/[slug]"]; +const paths = ["blog/[...page]", "blog/category/[category]", "blog/tag/[tag]", "blog/[slug]"]; async function main() { const __dirname = path.resolve(); diff --git a/src/pages/blog/[category]/[...page].astro b/src/pages/blog/[category]/[...page].astro deleted file mode 100644 index 63ec9229..00000000 --- a/src/pages/blog/[category]/[...page].astro +++ /dev/null @@ -1,51 +0,0 @@ ---- -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({ paginate }) {changeLanguage('en') - // get post collections - var 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; - }); - // Sort content entries by publication date - localizedPosts.sort(function (a, b) { - return b.data.publishDate.valueOf() - a.data.publishDate.valueOf(); - }); - const categories = new Set(); - posts.map((post) => { - typeof post.data.category === "string" && - categories.add(post.data.category.toLowerCase()); - }); - return Array.from(categories).map((category: string) => paginate(localizedPosts.filter((post) => typeof post.data.category === "string" && - category === post.data.category.toLowerCase()), { - params: { category: category, blog: "category" || undefined }, - pageSize: 5, - props: { category }, - })); -} -const { page, category } = Astro.props; ---- - - - - - {category} - - We write about building startups and thoughts going on our mind. - - - - - - diff --git a/src/pages/blog/[slug].astro b/src/pages/blog/[slug].astro index 129ff749..a17c67f4 100644 --- a/src/pages/blog/[slug].astro +++ b/src/pages/blog/[slug].astro @@ -10,18 +10,18 @@ import { getArticleReadingTime } from "@utils/blog"; changeLanguage("en"); // Generate a new path for every collection post -export async function getStaticPaths() {changeLanguage('en') - const blogEntries = await getCollection("blog"); - var localizedBlogEntries = blogEntries.filter((entry) => entry.slug.startsWith(i18next.language)); - return localizedBlogEntries.map((post) => { - let slug = post.slug; - slug = slug.substring(3); - console.log(slug); - return { - params: { slug: slug }, - props: { post: post }, - }; - }); +export async function getStaticPaths() { + changeLanguage("en"); + const blogEntries = await getCollection("blog"); + var localizedBlogEntries = blogEntries.filter((entry) => entry.slug.startsWith(i18next.language)); + return localizedBlogEntries.map((post) => { + let slug = post.slug; + slug = slug.substring(3); + return { + params: { slug: slug }, + props: { post: post }, + }; + }); } // Get the post directly from the prop on render const { post, lang } = Astro.props; @@ -34,7 +34,7 @@ const readTime = getArticleReadingTime(post.body);
{post.data.category} @@ -45,7 +45,7 @@ const readTime = getArticleReadingTime(post.body);
{ post.data.tags.map((tag) => ( - + #{tag} )) diff --git a/src/pages/blog/[tag]/[...page].astro b/src/pages/blog/[tag]/[...page].astro deleted file mode 100644 index 49c91a1e..00000000 --- a/src/pages/blog/[tag]/[...page].astro +++ /dev/null @@ -1,46 +0,0 @@ ---- -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({ paginate }) {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: string) => paginate(localizedPosts.filter((post) => Array.isArray(post.data.tags) && - post.data.tags.find((elem) => elem.toLowerCase() === tag)), { - params: { tag: tag, blog: "tag" || undefined }, - pageSize: 5, - props: { tag }, - })); -} -const { page, tag } = Astro.props; ---- - - - - - #{tag} - - We write about building startups and thoughts going on our mind. - - - - - - diff --git a/src/pages/blog/category/[category].astro b/src/pages/blog/category/[category].astro new file mode 100644 index 00000000..dada2cf0 --- /dev/null +++ b/src/pages/blog/category/[category].astro @@ -0,0 +1,48 @@ +--- +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()); + post.slug = post.slug.replace(/^(\w{2})\//, "$1/blog/"); + }); + 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; +--- + + + + + #{category} + We write about building startups and thoughts going on our mind. + + + + diff --git a/src/pages/blog/tag/[tag].astro b/src/pages/blog/tag/[tag].astro new file mode 100644 index 00000000..d8b84e6b --- /dev/null +++ b/src/pages/blog/tag/[tag].astro @@ -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; +--- + + + + + #{tag} + We write about building startups and thoughts going on our mind. + + + + diff --git a/src/pages/it/blog/[category]/[...page].astro b/src/pages/it/blog/[category]/[...page].astro deleted file mode 100644 index ffc5b92c..00000000 --- a/src/pages/it/blog/[category]/[...page].astro +++ /dev/null @@ -1,51 +0,0 @@ ---- -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("it"); - -export async function getStaticPaths({ paginate }) {changeLanguage('it') - // get post collections - var 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; - }); - // Sort content entries by publication date - localizedPosts.sort(function (a, b) { - return b.data.publishDate.valueOf() - a.data.publishDate.valueOf(); - }); - const categories = new Set(); - posts.map((post) => { - typeof post.data.category === "string" && - categories.add(post.data.category.toLowerCase()); - }); - return Array.from(categories).map((category: string) => paginate(localizedPosts.filter((post) => typeof post.data.category === "string" && - category === post.data.category.toLowerCase()), { - params: { category: category, blog: "category" || undefined }, - pageSize: 5, - props: { category }, - })); -} -const { page, category } = Astro.props; ---- - - - - - {category} - - We write about building startups and thoughts going on our mind. - - - - - - diff --git a/src/pages/it/blog/[slug].astro b/src/pages/it/blog/[slug].astro index a97f3853..d116ef56 100644 --- a/src/pages/it/blog/[slug].astro +++ b/src/pages/it/blog/[slug].astro @@ -10,18 +10,18 @@ import { getArticleReadingTime } from "@utils/blog"; changeLanguage("it"); // Generate a new path for every collection post -export async function getStaticPaths() {changeLanguage('it') - const blogEntries = await getCollection("blog"); - var localizedBlogEntries = blogEntries.filter((entry) => entry.slug.startsWith(i18next.language)); - return localizedBlogEntries.map((post) => { - let slug = post.slug; - slug = slug.substring(3); - console.log(slug); - return { - params: { slug: slug }, - props: { post: post }, - }; - }); +export async function getStaticPaths() { + changeLanguage("it"); + const blogEntries = await getCollection("blog"); + var localizedBlogEntries = blogEntries.filter((entry) => entry.slug.startsWith(i18next.language)); + return localizedBlogEntries.map((post) => { + let slug = post.slug; + slug = slug.substring(3); + return { + params: { slug: slug }, + props: { post: post }, + }; + }); } // Get the post directly from the prop on render const { post, lang } = Astro.props; @@ -34,7 +34,7 @@ const readTime = getArticleReadingTime(post.body);
{post.data.category} @@ -45,7 +45,7 @@ const readTime = getArticleReadingTime(post.body);
{ post.data.tags.map((tag) => ( - + #{tag} )) diff --git a/src/pages/it/blog/[tag]/[...page].astro b/src/pages/it/blog/[tag]/[...page].astro deleted file mode 100644 index 8f526d5e..00000000 --- a/src/pages/it/blog/[tag]/[...page].astro +++ /dev/null @@ -1,46 +0,0 @@ ---- -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("it"); - -export async function getStaticPaths({ paginate }) {changeLanguage('it') - 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: string) => paginate(localizedPosts.filter((post) => Array.isArray(post.data.tags) && - post.data.tags.find((elem) => elem.toLowerCase() === tag)), { - params: { tag: tag, blog: "tag" || undefined }, - pageSize: 5, - props: { tag }, - })); -} -const { page, tag } = Astro.props; ---- - - - - - #{tag} - - We write about building startups and thoughts going on our mind. - - - - - - diff --git a/src/pages/it/blog/category/[category].astro b/src/pages/it/blog/category/[category].astro new file mode 100644 index 00000000..94f6632c --- /dev/null +++ b/src/pages/it/blog/category/[category].astro @@ -0,0 +1,48 @@ +--- +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("it"); + +export async function getStaticPaths() {changeLanguage('it') + 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()); + post.slug = post.slug.replace(/^(\w{2})\//, "$1/blog/"); + }); + 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; +--- + + + + + #{category} + We write about building startups and thoughts going on our mind. + + + + diff --git a/src/pages/it/blog/tag/[tag].astro b/src/pages/it/blog/tag/[tag].astro new file mode 100644 index 00000000..df02bb41 --- /dev/null +++ b/src/pages/it/blog/tag/[tag].astro @@ -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("it"); + +export async function getStaticPaths() {changeLanguage('it') + 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; +--- + + + + + #{tag} + We write about building startups and thoughts going on our mind. + + + + diff --git a/src/pwa.ts b/src/pwa.ts index dba7e77d..815fd38b 100644 --- a/src/pwa.ts +++ b/src/pwa.ts @@ -4,10 +4,10 @@ registerSW({ immediate: true, onRegisteredSW(swScriptUrl) { // eslint-disable-next-line no-console - console.log("SW registered: ", swScriptUrl); + // console.log("SW registered: ", swScriptUrl); }, onOfflineReady() { // eslint-disable-next-line no-console - console.log("PWA application ready to work offline"); + // console.log("PWA application ready to work offline"); }, }); From 86d09385adff65d6f95c53f62fa74afdfa40fcbf Mon Sep 17 00:00:00 2001 From: Giulio Zanchetta Date: Fri, 11 Aug 2023 23:11:10 +0200 Subject: [PATCH 3/7] Refactor getStaticPaths function in blog/[slug].astro The getStaticPaths function in both the English and Italian versions of the blog/[slug].astro file has been refactored. The code now sets the language to 'en' or 'it' respectively, retrieves the collection of blog entries, filters them based on the language, and generates a new path for each post. The slug is modified by removing the first three characters. Finally, an object with params and props is returned for each post. Additionally, a minor change was made in line 45 of the Italian version to handle cases where tag is null or undefined. These changes improve code readability and maintainability. --- src/pages/blog/[slug].astro | 23 +++++++++++------------ src/pages/it/blog/[slug].astro | 25 ++++++++++++------------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/pages/blog/[slug].astro b/src/pages/blog/[slug].astro index a17c67f4..45aa4837 100644 --- a/src/pages/blog/[slug].astro +++ b/src/pages/blog/[slug].astro @@ -10,18 +10,17 @@ import { getArticleReadingTime } from "@utils/blog"; changeLanguage("en"); // Generate a new path for every collection post -export async function getStaticPaths() { - changeLanguage("en"); - const blogEntries = await getCollection("blog"); - var localizedBlogEntries = blogEntries.filter((entry) => entry.slug.startsWith(i18next.language)); - return localizedBlogEntries.map((post) => { - let slug = post.slug; - slug = slug.substring(3); - return { - params: { slug: slug }, - props: { post: post }, - }; - }); +export async function getStaticPaths() {changeLanguage('en') + const blogEntries = await getCollection("blog"); + var localizedBlogEntries = blogEntries.filter((entry) => entry.slug.startsWith(i18next.language)); + return localizedBlogEntries.map((post) => { + let slug = post.slug; + slug = slug.substring(3); + return { + params: { slug: slug }, + props: { post: post }, + }; + }); } // Get the post directly from the prop on render const { post, lang } = Astro.props; diff --git a/src/pages/it/blog/[slug].astro b/src/pages/it/blog/[slug].astro index d116ef56..d98c6419 100644 --- a/src/pages/it/blog/[slug].astro +++ b/src/pages/it/blog/[slug].astro @@ -10,18 +10,17 @@ import { getArticleReadingTime } from "@utils/blog"; changeLanguage("it"); // Generate a new path for every collection post -export async function getStaticPaths() { - changeLanguage("it"); - const blogEntries = await getCollection("blog"); - var localizedBlogEntries = blogEntries.filter((entry) => entry.slug.startsWith(i18next.language)); - return localizedBlogEntries.map((post) => { - let slug = post.slug; - slug = slug.substring(3); - return { - params: { slug: slug }, - props: { post: post }, - }; - }); +export async function getStaticPaths() {changeLanguage('it') + const blogEntries = await getCollection("blog"); + var localizedBlogEntries = blogEntries.filter((entry) => entry.slug.startsWith(i18next.language)); + return localizedBlogEntries.map((post) => { + let slug = post.slug; + slug = slug.substring(3); + return { + params: { slug: slug }, + props: { post: post }, + }; + }); } // Get the post directly from the prop on render const { post, lang } = Astro.props; @@ -45,7 +44,7 @@ const readTime = getArticleReadingTime(post.body);
{ post.data.tags.map((tag) => ( - + #{tag} )) From e42b777731d25284d188591aec18869484b53c86 Mon Sep 17 00:00:00 2001 From: Giulio Zanchetta Date: Fri, 11 Aug 2023 23:28:59 +0200 Subject: [PATCH 4/7] Refactor slug extraction logic in blog pages - Update the code to remove "blog/" from the beginning of slugs - Improve consistency and readability in slug extraction process The commit message summarizes the changes made to the code for extracting slugs in blog pages. It mentions that the code has been refactored to remove "blog/" from the beginning of slugs, resulting in improved consistency and readability. --- src/pages/blog/[slug].astro | 2 +- src/pages/it/blog/[slug].astro | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pages/blog/[slug].astro b/src/pages/blog/[slug].astro index 45aa4837..b809a4bb 100644 --- a/src/pages/blog/[slug].astro +++ b/src/pages/blog/[slug].astro @@ -15,7 +15,7 @@ export async function getStaticPaths() {changeLanguage('en') var localizedBlogEntries = blogEntries.filter((entry) => entry.slug.startsWith(i18next.language)); return localizedBlogEntries.map((post) => { let slug = post.slug; - slug = slug.substring(3); + slug = slug.substring(3).replaceAll("blog/", ""); return { params: { slug: slug }, props: { post: post }, diff --git a/src/pages/it/blog/[slug].astro b/src/pages/it/blog/[slug].astro index d98c6419..f94c5ace 100644 --- a/src/pages/it/blog/[slug].astro +++ b/src/pages/it/blog/[slug].astro @@ -15,7 +15,7 @@ export async function getStaticPaths() {changeLanguage('it') var localizedBlogEntries = blogEntries.filter((entry) => entry.slug.startsWith(i18next.language)); return localizedBlogEntries.map((post) => { let slug = post.slug; - slug = slug.substring(3); + slug = slug.substring(3).replaceAll("blog/", ""); return { params: { slug: slug }, props: { post: post }, From 761a241feb0dfb0be60bb2100190e98b19cbe27f Mon Sep 17 00:00:00 2001 From: Giulio Zanchetta Date: Fri, 11 Aug 2023 23:45:01 +0200 Subject: [PATCH 5/7] Refactor blog post URLs for better localization handling - Update the URL structure for blog post links in the `posts.astro` component - Modify the `getStaticPaths` function in `page.astro` and `category.astro` files to handle localized slugs correctly - Remove unnecessary code that replaces language prefixes in slug paths --- src/components/blog/posts.astro | 2 +- src/pages/blog/[...page].astro | 1 - src/pages/blog/category/[category].astro | 1 - src/pages/it/blog/[...page].astro | 1 - src/pages/it/blog/category/[category].astro | 1 - 5 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/components/blog/posts.astro b/src/components/blog/posts.astro index a18d253f..6b664446 100644 --- a/src/components/blog/posts.astro +++ b/src/components/blog/posts.astro @@ -38,7 +38,7 @@ const { posts } = Astro.props;

- + {blogPostEntry.data.title} diff --git a/src/pages/blog/[...page].astro b/src/pages/blog/[...page].astro index 67d4384e..2b458435 100644 --- a/src/pages/blog/[...page].astro +++ b/src/pages/blog/[...page].astro @@ -21,7 +21,6 @@ export async function getStaticPaths({ paginate }) {changeLanguage('en') if (firstSlug) { lang = firstSlug[1]; } - slug.splice(1, 0, "blog"); return { ...post, slug: slug.join("/"), diff --git a/src/pages/blog/category/[category].astro b/src/pages/blog/category/[category].astro index dada2cf0..241a9ece 100644 --- a/src/pages/blog/category/[category].astro +++ b/src/pages/blog/category/[category].astro @@ -20,7 +20,6 @@ export async function getStaticPaths() {changeLanguage('en') const categories = new Set(); localizedPosts.map((post) => { typeof post.data.category === "string" && categories.add(post.data.category.toLowerCase()); - post.slug = post.slug.replace(/^(\w{2})\//, "$1/blog/"); }); return Array.from(categories).map((category) => { return { diff --git a/src/pages/it/blog/[...page].astro b/src/pages/it/blog/[...page].astro index 388ed561..35eb57c5 100644 --- a/src/pages/it/blog/[...page].astro +++ b/src/pages/it/blog/[...page].astro @@ -21,7 +21,6 @@ export async function getStaticPaths({ paginate }) {changeLanguage('it') if (firstSlug) { lang = firstSlug[1]; } - slug.splice(1, 0, "blog"); return { ...post, slug: slug.join("/"), diff --git a/src/pages/it/blog/category/[category].astro b/src/pages/it/blog/category/[category].astro index 94f6632c..6f093001 100644 --- a/src/pages/it/blog/category/[category].astro +++ b/src/pages/it/blog/category/[category].astro @@ -20,7 +20,6 @@ export async function getStaticPaths() {changeLanguage('it') const categories = new Set(); localizedPosts.map((post) => { typeof post.data.category === "string" && categories.add(post.data.category.toLowerCase()); - post.slug = post.slug.replace(/^(\w{2})\//, "$1/blog/"); }); return Array.from(categories).map((category) => { return { From 6ae3156d035ca0b5c7830a34c0415c8ef8a9d9fc Mon Sep 17 00:00:00 2001 From: Giulio Zanchetta Date: Sat, 12 Aug 2023 03:09:03 +0200 Subject: [PATCH 6/7] Update package.json version to 1.0.4 The commit updates the version in the package.json file from 0.0.4 to 1.0.4, reflecting a significant change in the codebase. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e2d7057c..8ab5771d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "astros", "type": "module", - "version": "0.0.4", + "version": "1.0.4", "private": true, "scripts": { "dev": "astro dev --host", From f41ecc07aeca7e17638f76f88385cc663783993b Mon Sep 17 00:00:00 2001 From: Giulio Zanchetta Date: Sat, 12 Aug 2023 03:09:31 +0200 Subject: [PATCH 7/7] Update package.json version to 1.0.0 The commit updates the version in package.json from 1.0.4 to 1.0.0, aligning it with the project's current state. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8ab5771d..d951d8ad 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "astros", "type": "module", - "version": "1.0.4", + "version": "1.0.0", "private": true, "scripts": { "dev": "astro dev --host",