From f9a77c5c27aede4e5978eb55d9b7af781b680a1d Mon Sep 17 00:00:00 2001 From: João Augusto Costa Branco Marado Torres Date: Tue, 24 Jun 2025 12:08:41 -0300 Subject: feat!: initial commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: João Augusto Costa Branco Marado Torres --- src/pages/blog/[...year].astro | 165 ++++++++++++++++ src/pages/blog/keywords/[...slug].astro | 40 ++++ src/pages/blog/keywords/index.astro | 21 ++ src/pages/blog/read/[...slug].astro | 333 ++++++++++++++++++++++++++++++++ src/pages/index.astro | 28 +++ src/pages/robots.txt.ts | 13 ++ src/pages/rss.xml.js | 16 ++ 7 files changed, 616 insertions(+) create mode 100644 src/pages/blog/[...year].astro create mode 100644 src/pages/blog/keywords/[...slug].astro create mode 100644 src/pages/blog/keywords/index.astro create mode 100644 src/pages/blog/read/[...slug].astro create mode 100644 src/pages/index.astro create mode 100644 src/pages/robots.txt.ts create mode 100644 src/pages/rss.xml.js (limited to 'src/pages') diff --git a/src/pages/blog/[...year].astro b/src/pages/blog/[...year].astro new file mode 100644 index 0000000..f148a76 --- /dev/null +++ b/src/pages/blog/[...year].astro @@ -0,0 +1,165 @@ +--- +import { getCollection } from "astro:content"; +import type { CollectionEntry } from "astro:content"; +import Base from "@layouts/Base.astro"; +import DateSelector from "@components/DateSelector.astro"; +import BlogCard from "@components/BlogCard.astro"; + +type Props = { + posts: CollectionEntry<"blog">[]; + next: string; + previous: string; + years: number[]; + months: number[]; + days?: number[]; +}; + +export async function getStaticPaths() { + const posts = await getCollection("blog"); + + const archive = { + years: new Set(), + monthsByYear: new Map>(), + daysByMonth: new Map>(), + postsByDate: new Map(), + sortedDates: [] as string[], + }; + + const getYMD = (date: Date) => { + const y = date.getFullYear(); + const m = date.getMonth() + 1; + const d = date.getDate(); + return { y, m, d }; + }; + + for (const post of posts) { + const { y, m, d } = getYMD(post.data.dateCreated); + + archive.years.add(y); + + if (!archive.monthsByYear.has(y.toString())) { + archive.monthsByYear.set(y.toString(), new Set()); + } + archive.monthsByYear.get(y.toString())!.add(m); + + const ym = `${y}/${String(m).padStart(2, "0")}`; + if (!archive.daysByMonth.has(ym)) archive.daysByMonth.set(ym, new Set()); + archive.daysByMonth.get(ym)!.add(d); + + const ymd = `${ym}/${String(d).padStart(2, "0")}`; + if (!archive.postsByDate.has(ymd)) archive.postsByDate.set(ymd, []); + archive.postsByDate.get(ymd)!.push(post); + } + + archive.sortedDates = Array.from(archive.postsByDate.keys()).sort(); + + const paths = []; + + const sortedYears = Array.from(archive.years).sort(); + + const lastYear = Math.max(...sortedYears.map(Number)); + paths.push({ + params: { year: undefined }, + props: { + posts: posts.filter((p) => + p.data.dateCreated.getFullYear() === lastYear + ), + next: undefined, + previous: sortedYears?.[sortedYears.length - 2], + years: sortedYears, + months: Array.from(archive.monthsByYear.get(lastYear.toString()) ?? []), + }, + }); + + for (const y of sortedYears) { + const yearPosts = posts.filter((p) => + p.data.dateCreated.getFullYear() === Number(y) + ); + const idx = sortedYears.indexOf(y); + paths.push({ + params: { year: y }, + props: { + posts: yearPosts, + next: sortedYears?.[idx + 1], + previous: sortedYears?.[idx - 1], + years: sortedYears, + months: Array.from(archive.monthsByYear.get(y.toString()) ?? []), + }, + }); + } + + const allMonths = Array.from(archive.monthsByYear.entries()) + .flatMap(([year, mset]) => + Array.from(mset).map((m) => `${year}/${String(m).padStart(2, "0")}`) + ) + .sort(); + + for (const [y, months] of archive.monthsByYear) { + const sortedMonths = Array.from(months).sort(); + for (const m of sortedMonths) { + const monthPosts = posts.filter((p) => { + const d = p.data.dateCreated; + return ( + d.getFullYear() === Number(y) && + d.getMonth() + 1 === m + ); + }); + + const ym = `${y}/${String(m).padStart(2, "0")}`; + const idx = allMonths.indexOf(ym); + + paths.push({ + params: { year: ym }, + props: { + posts: monthPosts, + next: allMonths?.[idx + 1], + previous: allMonths?.[idx - 1], + years: sortedYears, + months: Array.from(months).sort(), + days: Array.from(archive.daysByMonth.get(ym) ?? []).sort(), + }, + }); + } + } + + for (let i = 0; i < archive.sortedDates.length; i++) { + const ymd = archive.sortedDates[i]; + const [y, m] = ymd.split("/"); + paths.push({ + params: { year: ymd }, + props: { + posts: archive.postsByDate.get(ymd), + next: archive.sortedDates?.[i + 1], + previous: archive.sortedDates?.[i - 1], + years: sortedYears, + months: Array.from(archive.monthsByYear.get(y) ?? []).sort(), + days: Array.from(archive.daysByMonth.get(`${y}/${m}`) ?? []).sort(), + }, + }); + } + + return paths; +} + +const title = "Blog"; +const description = "Latest articles."; + +let { posts, previous, next, years, months, days } = Astro.props; +posts = posts.sort((a, b) => + new Date(b.data.dateCreated).valueOf() - + new Date(a.data.dateCreated).valueOf() +); +const date = posts[0].data.dateCreated as Date; +--- + + +
+

Blogue

+ {date && } + {posts.map((post) => )} +
+ {previous && Previous} + {next && Next} +
+
+ diff --git a/src/pages/blog/keywords/[...slug].astro b/src/pages/blog/keywords/[...slug].astro new file mode 100644 index 0000000..724e8b7 --- /dev/null +++ b/src/pages/blog/keywords/[...slug].astro @@ -0,0 +1,40 @@ +--- +import { type CollectionEntry, getCollection } from "astro:content"; +import Base from "@layouts/Base.astro"; +import BlogCard from "@components/BlogCard.astro"; + +type Props = { posts: CollectionEntry<"blog">[] }; + +export async function getStaticPaths() { + const posts = await getCollection("blog"); + const keywords = [ + ...new Set( + await getCollection("blog").then((x) => + x.flatMap((x) => x.data.keywords) + ), + ).values(), + ]; + return keywords.map((k) => ({ + params: { slug: k }, + props: { + posts: posts.filter((post) => + post.data.keywords.some((i) => i.localeCompare(k) === 0) + ), + }, + })); +} + +const title = "Blog"; +const description = "Latest articles."; + +const posts = Astro.props.posts.sort((a, b) => + new Date(b.data.dateCreated).valueOf() - + new Date(a.data.dateCreated).valueOf() +); +--- + + +
+

Blogue

{posts.map((post) => )} +
+ diff --git a/src/pages/blog/keywords/index.astro b/src/pages/blog/keywords/index.astro new file mode 100644 index 0000000..255fbf4 --- /dev/null +++ b/src/pages/blog/keywords/index.astro @@ -0,0 +1,21 @@ +--- +import { getCollection } from "astro:content"; +import Base from "@layouts/Base.astro"; + +const title = "Keywords"; +const description = "Keywords"; + +const blogs = await getCollection("blog"); +let keywords = [ + ...new Set([ + ...blogs.flatMap(({ data }) => [...(data.keywords ?? [])]), + ]), +]; +--- + + +

Keywords

+
    + {keywords.map((k) =>
  • {k}
  • )} +
+ diff --git a/src/pages/blog/read/[...slug].astro b/src/pages/blog/read/[...slug].astro new file mode 100644 index 0000000..05d68e8 --- /dev/null +++ b/src/pages/blog/read/[...slug].astro @@ -0,0 +1,333 @@ +--- +import { type CollectionEntry, getCollection } from "astro:content"; +import { render } from "astro:content"; +import BaseHead from "@components/BaseHead.astro"; +import Translations from "@components/Translations.astro"; +import { toIso8601Full } from "@utils/datetime"; +import ReadingTime from "@components/ReadingTime.astro"; +import Keywords from "@components/Keywords.astro"; +import Citations from "@components/Citations.astro"; +import Signature from "@components/signature/Signature.astro"; +import CopyrightNotice from "@components/CopyrightNotice.astro"; +import { getEntries } from "astro:content"; +import { verifier as verifierPrototype } from "@lib/pgp/verify"; +import { defined, get } from "@utils/anonymous"; +import Authors from "@components/signature/Authors.astro"; +import { getEntry } from "astro:content"; + +export async function getStaticPaths() { + const posts = await getCollection("blog"); + return posts.map((post) => ({ + params: { slug: post.id }, + props: post, + })); +} + +type Props = CollectionEntry<"blog">; + +const post = Astro.props; + +if (defined(post.data.translationOf)) { + const original = await getEntry( + post.data.translationOf as CollectionEntry<"blog">, + ); + + if (!original) { + throw new Error(`Original post not found for ${post.id}`); + } + + const originalAuthor = (original.data.signers ?? []).filter( + (s) => s.role === "author", + ).map((s) => s.entity.id)?.[0]; + const originalCoAuthors = new Set( + (original.data.signer ?? []).filter( + (s) => s.role === "co-author", + ).map((s) => s.entity.id), + ); + const translationAuthor = (post.data.signer ?? []).filter( + (s) => s.role === "author", + ).map((s) => s.entity.id)?.[0]; + const translationCoAuthors = new Set( + (post.data.signer ?? []).filter( + (s) => s.role === "co-author", + ).map((s) => s.entity.id), + ); + + if ( + (translationAuthor !== undefined && + translationAuthor !== originalAuthor) || + !translationCoAuthors.isSubsetOf(originalCoAuthors) + ) { + throw new Error( + `Post ${post.id} has mismatched (co-)authors from original post ${original.id}`, + ); + } + + const translators = (post.data.signer ?? []).filter( + (s) => s.role === "translator", + ).map((s) => s.entity.id); + + for (const translator of translators) { + if ( + originalAuthor === translator || originalCoAuthors.has(translator) + ) { + throw new Error( + `Translator ${translator} in ${post.id} is already a (co-)author in original post`, + ); + } + } +} else { + if (post.data.signer?.some((x) => x.role === "translator")) { + throw new Error( + `Post ${post.id} is not a translation but has translators defined`, + ); + } +} + +// Add own post as a translation +const translationsSet = new Set( + (await getCollection( + "blog", + (x) => + x.data.translationOf?.id === + (post.data.translationOf !== undefined + ? post.data.translationOf.id + : post.id), + ) ?? []).map(({ id }) => id), +); + +translationsSet.add(post.id); +const translations = [...translationsSet.values()].map((id) => ({ + collection: post.collection, + id, +})); + +const signers = await getEntries( + post.data.signer?.map(get("entity")) ?? [], +).then((x) => x.filter(defined)) + .then((x) => + x.map((x) => ({ + entity: x, + role: post.data.signer?.find((y) => y.entity.id === x.id)?.role, + })) + ) + .then((x) => x.filter((x) => x.role !== undefined)); + +const verifier = await verifierPrototype.then((x) => x.clone()); + +// Add signers public keys to keyring +for (const { data } of signers.map(get("entity"))) { + if (data.publickey.armor !== undefined) { + verifier.addKeyFromArmor(data.publickey.armor); + } +} + +const verification = post.filePath !== undefined + ? await verifier.verify([ + new URL(`file://${Deno.cwd()}/${post.filePath}`), + ]) + : undefined; + +const { Content } = await render(post); + +const { lang } = post.data; + +const commit = await verification?.commit; +--- + + + + + + + +
+
+ +
+

{post.data.title}

+ { + post.data.subtitle && ( +

+ {post.data.subtitle} +

+ ) + } +
+ { + post.data.description && ( +
+

Resumo

+ { + post.data.description.split(new RegExp("\\s{2,}")) + .map(( + x, + ) => ( +

{x}

+ )) + } +
+ ) + } + {verification && } +
+ { + verification?.verifications && + ( + + ) + } +
+
Data de criação
+
+ +
+ { + post.data.dateUpdated && ( +
Última atualização
+ +
+ ) + } + { + post.data.locationCreated && ( +
+ Local de criação +
+ {post.data.locationCreated} +
+ ) + } +
+ +
+
+
+
+ + + +
+
+ + + + + + + + diff --git a/src/pages/index.astro b/src/pages/index.astro new file mode 100644 index 0000000..e1e97ef --- /dev/null +++ b/src/pages/index.astro @@ -0,0 +1,28 @@ +--- +import Base from "@layouts/Base.astro"; +import { SITE_TITLE } from "src/consts"; +--- + + +
+
+

Viva abril!

+
+
+ «Los que le cierran el camino a la revolución + pacífica le abren al mismo tiempo el camino a la + revolución violenta». +
+
+ — Hugo Chávez. +

+ Tradução: “Aqueles que fecham o caminho para a + revolução pacífica abrem, ao mesmo tempo, o + caminho para a revolução violenta.” +

+
+
+

Portugal fez diferente!

+
+
+ diff --git a/src/pages/robots.txt.ts b/src/pages/robots.txt.ts new file mode 100644 index 0000000..4edef8b --- /dev/null +++ b/src/pages/robots.txt.ts @@ -0,0 +1,13 @@ +import type { APIRoute } from "astro"; + +const getRobotsTxt = (sitemapURL: URL) => ` +User-agent: * +Allow: / + +Sitemap: ${sitemapURL.href} +`; + +export const GET: APIRoute = ({ site }) => { + const sitemapURL = new URL("sitemap-index.xml", site); + return new Response(getRobotsTxt(sitemapURL)); +}; diff --git a/src/pages/rss.xml.js b/src/pages/rss.xml.js new file mode 100644 index 0000000..de5685b --- /dev/null +++ b/src/pages/rss.xml.js @@ -0,0 +1,16 @@ +import rss from "@astrojs/rss"; +import { getCollection } from "astro:content"; +import { SITE_DESCRIPTION, SITE_TITLE } from "../consts"; + +export async function GET(context) { + const posts = await getCollection("blog"); + return rss({ + title: SITE_TITLE, + description: SITE_DESCRIPTION, + site: context.site, + items: posts.map((post) => ({ + ...post.data, + link: `/blog/${post.id}/`, + })), + }); +} -- cgit v1.2.3