diff --git a/src/i18n/index.ts b/src/i18n/index.ts index 87e07a6..38bc909 100644 --- a/src/i18n/index.ts +++ b/src/i18n/index.ts @@ -38,6 +38,16 @@ export const routeTranslations = { } } as const; +export function getRouteKeyFromSlug(slug: string): keyof typeof routeTranslations { + for (const [key, translations] of Object.entries(routeTranslations)) { + const values = Object.values(translations) as string[]; + if (values.includes(slug)) { + return key as keyof typeof routeTranslations; + } + } + return "news"; +} + export function getLocalizedRoute(route: keyof typeof routeTranslations, locale: string | undefined): string { const l = (locale in routeTranslations[route] ? locale : "es") as Locale; return routeTranslations[route][l as keyof (typeof routeTranslations)[typeof route]] || routeTranslations[route]["en" as keyof (typeof routeTranslations)[typeof route]]; diff --git a/src/pages/[locale]/[editorial_slug]/index.astro b/src/pages/[locale]/[editorial_slug]/index.astro deleted file mode 100644 index 4d9fbc0..0000000 --- a/src/pages/[locale]/[editorial_slug]/index.astro +++ /dev/null @@ -1,58 +0,0 @@ ---- -import MainLayout from "@/layouts/MainLayout.astro" -import Header from "@/components/Header.astro" -import NewsList from "@/components/cards/NewsList.astro"; -import { getCollection } from "astro:content"; -import FooterSection from "@/components/section/FooterSection.astro"; - -import { createTranslator, getLocalizedRoute, routeTranslations } from '@/i18n'; -const tl = createTranslator(Astro.currentLocale); - -export function getStaticPaths() { - const locales = Object.keys(routeTranslations.editorial); - return locales.map((locale) => ({ - params: { - locale, - editorial_slug: getLocalizedRoute('editorial', locale) - }, - })); -} - -const { locale, editorial_slug } = Astro.params; - -const items = await getCollection("editorial", (post)=>{ - const currentLocale = Astro.currentLocale; - return post.data.locale == currentLocale -}); - -const sortedPosts = [...items] - .sort((a, b) => { - const dateDiff = new Date(b.data.date).getTime() - new Date(a.data.date).getTime() - if (dateDiff !== 0) return dateDiff - return (a.data.order ?? 0) - (b.data.order ?? 0) - }); ---- - - -
-
-
-
-
-

{tl("editorial.title")}

-

{tl("editorial.text")}

-
- -
- { - sortedPosts.map((item) => ( -
- -
- )) - } -
-
- - -
diff --git a/src/pages/[locale]/[news_slug]/[id].astro b/src/pages/[locale]/[news_slug]/[id].astro deleted file mode 100644 index 9723d12..0000000 --- a/src/pages/[locale]/[news_slug]/[id].astro +++ /dev/null @@ -1,143 +0,0 @@ ---- -import { YouTube } from "astro-embed"; -import MainLayout from "../../../layouts/MainLayout.astro"; -import Header from "../../../components/Header.astro"; -import CarouselSection from "../../../components/section/CarouselSection.astro"; -import { Image } from "@unpic/astro"; -import { getCollection, render } from "astro:content"; -import TitleSection from "../../../components/section/TitleSection.astro"; -import FooterSection from "../../../components/section/FooterSection.astro"; -import { getLocalizedRoute } from "@/i18n"; -export const prerender = true; -// 1. Generate a new path for every collection entry -export async function getStaticPaths() { - const posts = await getCollection("news"); - return posts.map((post) => ({ - params: { - id: post.id, - locale: post.data.locale, - news_slug: getLocalizedRoute("news", post.data.locale), - }, - props: { post }, - })); -} - -const { locale, news_slug } = Astro.params; - -// 2. For your template, you can get the entry directly from the prop -const { post } = Astro.props; -const { Content } = await render(post); - -const baseSlug = news_slug; - -const rawContent = post.body || ""; -const plainText = rawContent - .replace(/^#.*$/gm, "") - .replace(/^###.*$/gm, "") - .replace(/\*\*([^*]+)\*\*/g, "$1") - .replace(/\*([^*]+)\*/g, "$1") - .replace(/_([^_]+)_/g, "$1") - .replace(/\[([^\]]+)\]\([^)]+\)/g, "$1") - .replace(/^>.*$/gm, "") - .replace(/`[^`]+`/g, "") - .replace(/^[-*]\s+/gm, "") - .trim(); -const words = plainText - .split(/\s+/) - .filter((w) => w.length > 0) - .slice(0, 35); -const excerpt = words.join(" ") + (words.length === 35 ? "..." : ""); - -const canonicalUrl = new URL(`/${locale}/${news_slug}/${post.id}`, Astro.site); -const imageUrl = post.data.thumbnail - ? new URL(post.data.thumbnail, Astro.site).toString() - : null; -const localeDate = new Intl.DateTimeFormat(locale || "es", { - year: "numeric", - month: "long", - day: "numeric", -}).format(post.data.date); -const y = post.data.date.getFullYear(); -const m = String(post.data.date.getMonth() + 1).padStart(2, "0"); -const d = String(post.data.date.getDate()).padStart(2, "0"); -const dateISO = `${y}-${m}-${d}`; ---- - - - -
-
-
- - - - - -
- - { - post.data.gallery?.length ? ( - - ) : post.data.thumbnail ? ( - {post.data.title} - ) : null - } - -
-
-
-
- - -
-
-
- {post.data.youtube && } - - { - post.data.gallery && - post.data.gallery.map((galleryImage, i) => ( - - )) - } -
-
-
- - diff --git a/src/pages/[locale]/[editorial_slug]/[id].astro b/src/pages/[locale]/[section]/[id].astro similarity index 85% rename from src/pages/[locale]/[editorial_slug]/[id].astro rename to src/pages/[locale]/[section]/[id].astro index 6091995..010ce6d 100644 --- a/src/pages/[locale]/[editorial_slug]/[id].astro +++ b/src/pages/[locale]/[section]/[id].astro @@ -9,27 +9,30 @@ import TitleSection from "../../../components/section/TitleSection.astro"; import FooterSection from "../../../components/section/FooterSection.astro"; import { getLocalizedRoute } from "@/i18n"; export const prerender = true; -// 1. Generate a new path for every collection entry + export async function getStaticPaths() { - const posts = await getCollection("editorial"); - return posts.map((post) => ({ - params: { - id: post.id, - locale: post.data.locale, - editorial_slug: getLocalizedRoute("editorial", post.data.locale), - }, - props: { post }, - })); + const paths = []; + for (const collection of ["news", "editorial"]) { + const posts = await getCollection(collection); + for (const post of posts) { + paths.push({ + params: { + id: post.id, + locale: post.data.locale, + section: getLocalizedRoute(collection, post.data.locale), + }, + props: { post }, + }); + } + } + return paths; } -const { locale, editorial_slug } = Astro.params; - -// 2. For your template, you can get the entry directly from the prop +const { locale, section } = Astro.params; const { post } = Astro.props; +const routeKey = post.collection; const { Content } = await render(post); -const baseSlug = editorial_slug; - const rawContent = post.body || ""; const plainText = rawContent .replace(/^#.*$/gm, "") @@ -48,7 +51,7 @@ const words = plainText .slice(0, 35); const excerpt = words.join(" ") + (words.length === 35 ? "..." : ""); -const canonicalUrl = new URL(`/${locale}/${editorial_slug}/${post.id}`, Astro.site); +const canonicalUrl = new URL(`/${locale}/${section}/${post.id}`, Astro.site); const imageUrl = post.data.thumbnail ? new URL(post.data.thumbnail, Astro.site).toString() : null; @@ -61,6 +64,8 @@ const y = post.data.date.getFullYear(); const m = String(post.data.date.getMonth() + 1).padStart(2, "0"); const d = String(post.data.date.getDate()).padStart(2, "0"); const dateISO = `${y}-${m}-${d}`; + +const schemaType = routeKey === "news" ? "NewsArticle" : "Article"; --- ({ - params: { - locale, - news_slug: getLocalizedRoute('news', locale) - }, - })); -} +const { locale, section } = Astro.params; +const routeKey = getRouteKeyFromSlug(section); -const { locale, news_slug } = Astro.params; - -const newsItems = await getCollection("news", (post)=>{ +const items = await getCollection(routeKey, (post)=>{ const currentLocale = Astro.currentLocale; return post.data.locale == currentLocale }); -const sortedPosts = [...newsItems] +const sortedPosts = [...items] .sort((a, b) => { const dateDiff = new Date(b.data.date).getTime() - new Date(a.data.date).getTime() if (dateDiff !== 0) return dateDiff return (a.data.order ?? 0) - (b.data.order ?? 0) }); -const allTags = [...new Set( +const allTags = routeKey === "news" ? [...new Set( sortedPosts .filter(p => p.data.tags && p.data.tags.length > 0) .flatMap(p => p.data.tags) .filter((tag): tag is string => tag !== undefined) -)].sort(); +)].sort() : []; -const allYears = [...new Set( +const allYears = routeKey === "news" ? [...new Set( sortedPosts.map(p => new Date(p.data.date).getFullYear()) -)].sort((a, b) => b - a); +)].sort((a, b) => b - a) : []; --- @@ -51,49 +41,49 @@ const allYears = [...new Set(
-

{tl("news.title")}

-

{tl("news.text")}

-
+

{tl(routeKey + ".title")}

+

{tl(routeKey + ".text")}

+
- {allTags.length > 0 && ( -
-
- - {allTags.map((tag) => ( - - ))} -
-
- )} - -
- {allYears.length > 0 && ( -
-
- +
+ )} + +
+ {routeKey === "news" && allYears.length > 0 && ( +
+
+ +
+
+ )} + { + sortedPosts.map((item) => ( +
+ +
+ )) + }
- )} - { - sortedPosts.map((item) => ( -
- -
- )) - } -
- - + +