From f3fb2fe005c8acba98202561d93e19854d83cf89 Mon Sep 17 00:00:00 2001 From: AdrianoDev Date: Thu, 2 Jul 2026 01:31:59 +0200 Subject: [PATCH] feat: blog pubblico con filtri, paginazione e articolo --- src/components/blog/Sidebar.astro | 26 ++++++++++ src/lib/blog-const.ts | 6 +++ src/pages/blog/[slug].astro | 33 +++++++++++++ src/pages/blog/index.astro | 81 +++++++++++++++++++++++++++++++ 4 files changed, 146 insertions(+) create mode 100644 src/components/blog/Sidebar.astro create mode 100644 src/lib/blog-const.ts create mode 100644 src/pages/blog/[slug].astro create mode 100644 src/pages/blog/index.astro diff --git a/src/components/blog/Sidebar.astro b/src/components/blog/Sidebar.astro new file mode 100644 index 0000000..a89dcc5 --- /dev/null +++ b/src/components/blog/Sidebar.astro @@ -0,0 +1,26 @@ +--- +import { getDb } from '../../lib/db'; +import { listRecent, listArchiveMonths } from '../../lib/posts'; +import { CATEGORIES } from '../../lib/blog-const'; +const db = getDb(); +const recent = listRecent(db, 4); +const months = listArchiveMonths(db); +const monthLabel = (m: string) => new Intl.DateTimeFormat('it-IT', { month: 'long', year: 'numeric' }).format(new Date(`${m}-01`)); +--- + + + diff --git a/src/lib/blog-const.ts b/src/lib/blog-const.ts new file mode 100644 index 0000000..d1b7436 --- /dev/null +++ b/src/lib/blog-const.ts @@ -0,0 +1,6 @@ +export const CATEGORIES = [ + 'Articoli tecnici & scientifici', + 'Contenuti educativi', + 'LinkedIn / Insight', + 'Eventi & Presidi', +] as const; diff --git a/src/pages/blog/[slug].astro b/src/pages/blog/[slug].astro new file mode 100644 index 0000000..fdf30f1 --- /dev/null +++ b/src/pages/blog/[slug].astro @@ -0,0 +1,33 @@ +--- +import Base from '../../layouts/Base.astro'; +import Sidebar from '../../components/blog/Sidebar.astro'; +import { getDb } from '../../lib/db'; +import { getPublishedBySlug } from '../../lib/posts'; +export const prerender = false; + +const post = getPublishedBySlug(getDb(), Astro.params.slug!); +if (!post) return Astro.redirect('/404'); +const fmt = new Intl.DateTimeFormat('it-IT', { dateStyle: 'long' }); +--- + +
+
+
+ {post.cover && } +

{post.published_at && fmt.format(new Date(post.published_at))} · {post.category}

+

{post.title}

+
+
+ +
+
+ + + diff --git a/src/pages/blog/index.astro b/src/pages/blog/index.astro new file mode 100644 index 0000000..dcd8a05 --- /dev/null +++ b/src/pages/blog/index.astro @@ -0,0 +1,81 @@ +--- +import Base from '../../layouts/Base.astro'; +import PageHero from '../../components/PageHero.astro'; +import { getDb } from '../../lib/db'; +import { listPublished } from '../../lib/posts'; +import { CATEGORIES } from '../../lib/blog-const'; +export const prerender = false; + +const PER_PAGE = 9; +const url = Astro.url; +const category = url.searchParams.get('categoria') ?? undefined; +const page = Math.max(1, Number(url.searchParams.get('pagina') ?? '1') || 1); +const { items, total } = listPublished(getDb(), { + category: category && (CATEGORIES as readonly string[]).includes(category) ? category : undefined, + page, perPage: PER_PAGE, +}); +const pages = Math.max(1, Math.ceil(total / PER_PAGE)); +const fmt = (d: string | null) => d ? new Intl.DateTimeFormat('it-IT', { dateStyle: 'long' }).format(new Date(d)) : ''; +const linkFor = (cat?: string, p = 1) => { + const q = new URLSearchParams(); + if (cat) q.set('categoria', cat); + if (p > 1) q.set('pagina', String(p)); + const s = q.toString(); + return `/blog${s ? `?${s}` : ''}`; +}; +--- + + +
+
+
+

Blog & Edugo

+

Edugo è lo spazio di InsanityLab dedicato alla conoscenza: articoli, guide e approfondimenti su allenamento, nutrizione e salute per capire il corpo e migliorare il proprio benessere. Perché la conoscenza è il primo passo del cambiamento.

+
+ + {items.length === 0 ? ( +

Nessun articolo pubblicato{category ? ' in questa categoria' : ''}, torna a trovarci presto.

+ ) : ( +
+ {items.map((post) => ( + + ))} +
+ )} + {pages > 1 && ( + + )} +
+
+ + +