From b43664264c7194983fa8a23dc961182ef6d9b7e7 Mon Sep 17 00:00:00 2001 From: Norbert Melzer Date: Tue, 23 Jul 2024 16:39:14 +0200 Subject: [PATCH] create author index page --- src/components/NavBox.astro | 2 +- src/pages/authors/index.astro | 33 +++++++++++++++++++++++++++++++++ src/scripts/authors.ts | 13 +++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/pages/authors/index.astro create mode 100644 src/scripts/authors.ts diff --git a/src/components/NavBox.astro b/src/components/NavBox.astro index 48c641c..9cf0b9f 100644 --- a/src/components/NavBox.astro +++ b/src/components/NavBox.astro @@ -16,7 +16,7 @@ import NavItem from "./NavItem.astro"; >
    Blog - Authors + Authors
  1. Sponsors
  2. diff --git a/src/pages/authors/index.astro b/src/pages/authors/index.astro new file mode 100644 index 0000000..aa68b9e --- /dev/null +++ b/src/pages/authors/index.astro @@ -0,0 +1,33 @@ +--- +import { getCollection } from "astro:content"; + +import NobbzDev from "../../layouts/NobbzDev.astro"; +import { authorPath } from "../../scripts/paths"; +import { withPosts, type AuthorWithPosts } from "../../scripts/authors"; + +const authorSorter = (a: AuthorWithPosts, b: AuthorWithPosts): number => { + const postCountA = a.posts.length; + const postCountB = b.posts.length; + return postCountA - postCountB; +}; + +const authorEntries = await getCollection("authors") + .then((authors) => Promise.all(authors.map(withPosts))) + .then((authors) => authors.sort(authorSorter)); +--- + + +
    +

    Authors

    + +
    +
    diff --git a/src/scripts/authors.ts b/src/scripts/authors.ts new file mode 100644 index 0000000..fb4e1c1 --- /dev/null +++ b/src/scripts/authors.ts @@ -0,0 +1,13 @@ +import { getCollection, type CollectionEntry } from "astro:content"; + +export type Author = CollectionEntry<"authors">; + +export type WithPosts = T & { + posts: CollectionEntry<"blog">[], +}; + +export const withPosts = async (author: T): Promise> => { + const posts = await getCollection("blog", ({ data: { author: postAuthor } }) => author.slug == postAuthor.slug); + + return { ...author, posts } +} -- 2.51.2