From d39bfded014a31030c7e134350ad8717844ebe2b Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 27 Jan 2020 19:31:59 -0500 Subject: [PATCH] Add year index pages --- lib/generate/categories.ts | 6 ++-- lib/generate/index.ts | 4 ++- lib/generate/paginated.ts | 2 +- lib/generate/years.ts | 22 +++++++++++++++ lib/index.ts | 1 + site/category.html.ejs | 39 ++------------------------ site/includes/article-listing.html.ejs | 14 +++++++++ site/includes/pagination.html.ejs | 23 +++++++++++++++ site/index.html.ejs | 39 ++------------------------ site/year.html.ejs | 13 +++++++++ 10 files changed, 84 insertions(+), 79 deletions(-) create mode 100644 lib/generate/years.ts create mode 100644 site/includes/article-listing.html.ejs create mode 100644 site/includes/pagination.html.ejs create mode 100644 site/year.html.ejs diff --git a/lib/generate/categories.ts b/lib/generate/categories.ts index bbae10b..60f953c 100644 --- a/lib/generate/categories.ts +++ b/lib/generate/categories.ts @@ -1,7 +1,7 @@ import { Page, PostMetadata } from "../metadata"; import generatePaginated from "./paginated"; -export default async function homepage(posts: Page[]): Promise> { +export default async function(posts: Page[]): Promise> { const categories = new Map(); for (const post of posts) { @@ -12,8 +12,8 @@ export default async function homepage(posts: Page[]): Promise { - await generatePaginated(categoryPosts, `/${category}/`, "site/category.html.ejs", { + categories.forEach((categoryPosts, category) => { + generatePaginated(categoryPosts, `/${category}/`, "site/category.html.ejs", { category }, { title: `${category} posts` diff --git a/lib/generate/index.ts b/lib/generate/index.ts index bb25ce1..d5a2cce 100644 --- a/lib/generate/index.ts +++ b/lib/generate/index.ts @@ -6,6 +6,7 @@ import homepage from "./homepage"; import posts from "./posts"; import rss from "./rss"; import tutorials from "./tutorials"; +import years from "./years"; export = { categories, @@ -15,5 +16,6 @@ export = { homepage, posts, rss, - tutorials + tutorials, + years, }; diff --git a/lib/generate/paginated.ts b/lib/generate/paginated.ts index 7e3cdcd..0a357fd 100644 --- a/lib/generate/paginated.ts +++ b/lib/generate/paginated.ts @@ -4,7 +4,7 @@ import * as metadata from "../metadata"; import { Page, PostMetadata } from "../metadata"; import layout from "../layout"; -export default async function generatePaginted(posts: Page[], basePath: string, templatePath: string, extraData?: object, extraMetadata?: object) { +export default async function generatePaginated(posts: Page[], basePath: string, templatePath: string, extraData?: object, extraMetadata?: object) { const page = await metadata.get(templatePath); if (extraMetadata) page.metadata = {...page.metadata, ...extraMetadata}; diff --git a/lib/generate/years.ts b/lib/generate/years.ts new file mode 100644 index 0000000..4b2823a --- /dev/null +++ b/lib/generate/years.ts @@ -0,0 +1,22 @@ +import {Page, PostMetadata} from "../metadata"; +import generatePaginated from "./paginated"; + +export default async function(posts: Page[]) { + const years = new Map(); + + for (const post of posts) { + const year = ((post.metadata).date).getFullYear(); + if (!years.has(year)) { + years.set(year, []); + } + years.get(year)!.push(post) + } + + years.forEach((yearPosts, year) => { + generatePaginated(yearPosts, `/${year}/`, "site/year.html.ejs", { + year + }, { + title: `Posts from ${year}` + }); + }); +} \ No newline at end of file diff --git a/lib/index.ts b/lib/index.ts index 8359921..235cddc 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -20,6 +20,7 @@ async function generate(): Promise { const posts = await generators.posts(); generators.homepage(posts); + generators.years(posts); const categories = await generators.categories(posts); await generators.rss(posts, categories, tutorials); diff --git a/site/category.html.ejs b/site/category.html.ejs index c6ac827..f5168f0 100644 --- a/site/category.html.ejs +++ b/site/category.html.ejs @@ -9,43 +9,8 @@ metadata.layout = "default.html.ejs"

<% for (const post of posts) { %> - + <%- include("includes/article-listing.html.ejs", { post }) %> <% } %> - \ No newline at end of file +<%- include("includes/pagination.html.ejs", { pagination }) %> diff --git a/site/includes/article-listing.html.ejs b/site/includes/article-listing.html.ejs new file mode 100644 index 0000000..5fb615e --- /dev/null +++ b/site/includes/article-listing.html.ejs @@ -0,0 +1,14 @@ + \ No newline at end of file diff --git a/site/includes/pagination.html.ejs b/site/includes/pagination.html.ejs new file mode 100644 index 0000000..3245a8a --- /dev/null +++ b/site/includes/pagination.html.ejs @@ -0,0 +1,23 @@ + \ No newline at end of file diff --git a/site/index.html.ejs b/site/index.html.ejs index a14daec..571998a 100644 --- a/site/index.html.ejs +++ b/site/index.html.ejs @@ -5,43 +5,8 @@ metadata.layout = "default.html.ejs"
<% for (const post of posts) { %> - + <%- include("includes/article-listing.html.ejs", { post }) %> <% } %>
- \ No newline at end of file +<%- include("includes/pagination.html.ejs", { pagination }) %> diff --git a/site/year.html.ejs b/site/year.html.ejs new file mode 100644 index 0000000..3afdfad --- /dev/null +++ b/site/year.html.ejs @@ -0,0 +1,13 @@ +``` +metadata.layout = "default.html.ejs" +``` + +
+

Posts from <%= year %>

+ + <% for (const post of posts) { %> + <%- include("includes/article-listing.html.ejs", { post }) %> + <% } %> +
+ +<%- include("includes/pagination.html.ejs", { pagination }) %>