Skip to content

Commit

Permalink
Replace /blog with an article list.
Browse files Browse the repository at this point in the history
  • Loading branch information
jverkoey committed Sep 2, 2024
1 parent 7f24d6d commit b92e29f
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 7 deletions.
147 changes: 147 additions & 0 deletions Sources/Site/Blog/BlogList.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import Foundation
import Slipstream

func groupBlogPostsByYearAndMonth(posts: [BlogPost]) -> [Int: [Int: [BlogPost]]] {
var groupedPosts: [Int: [Int: [BlogPost]]] = [:]

let calendar = Calendar.current

for post in posts {
let year = calendar.component(.year, from: post.date)
let month = calendar.component(.month, from: post.date)

if groupedPosts[year] == nil {
groupedPosts[year] = [:]
}

if groupedPosts[year]?[month] == nil {
groupedPosts[year]?[month] = []
}

groupedPosts[year]?[month]?.append(post)
}

return groupedPosts
}

private struct YearHeading: View {
let year: Int
var body: some View {
H2("\(year)")
.fontSize(.extraExtraLarge)
.bold()
.fontDesign("rounded")
}
}

private struct YearGroup<Content: View>: View {
let isLastYear: Bool
@ViewBuilder let content: () -> Content

var body: some View {
VStack {
content()
}
.margin(.bottom, 16)
if !isLastYear {
HorizontalRule()
}
}
}

private struct MonthHeading: View {
let month: String
var body: some View {
H3(month)
.fontSize(.large)
.bold()
.fontDesign("rounded")
}
}

private struct MonthGroup<Content: View>: View {
let isLastMonth: Bool
@ViewBuilder let content: () -> Content

var body: some View {
if isLastMonth {
List {
content()
}
} else {
List {
content()
}
.margin(.bottom, 16)
}
}
}

private struct PostLink: View {
let post: BlogPost

var body: some View {
ListItem {
Link(post.url) {
if post.draft {
Text("Draft: " + (post.title ?? post.slug))
.textColor(.palette(.red, darkness: 500))
} else {
Text(post.title ?? post.slug)
}
}
.textColor(.link, darkness: 700)
.textColor(.link, darkness: 400, condition: .dark)
.fontWeight(600)
.underline(condition: .hover)
}
.listStyle(.disc)
.margin(.left, 22)
}
}

struct BlogList: View {
let posts: [BlogPost]
let groupedPosts: [Int: [Int: [BlogPost]]]

init(posts: [BlogPost]) {
self.posts = posts
self.groupedPosts = groupBlogPostsByYearAndMonth(posts: posts.reversed())
}

let dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM"
return dateFormatter
}()

var body: some View {
Page(
path: "/blog/",
description: "Articles on software design and Apple platforms."
) {
NarrowContainer {
Div {
let sortedYears = groupedPosts.sorted(by: { $0.key > $1.key })
for (year, months) in sortedYears {
YearHeading(year: year)
YearGroup(isLastYear: sortedYears.last?.key == year) {
let sortedMonths = months.sorted(by: { $0.key > $1.key })
for (month, posts) in sortedMonths {
let monthName = dateFormatter.monthSymbols[month - 1]
MonthHeading(month: monthName)
MonthGroup(isLastMonth: sortedMonths.last?.key == month) {
for post in posts {
PostLink(post: post)
}
}
}
}
}
}
.textColor(.white)
}
.padding(.bottom, 16)
}
}
}
7 changes: 7 additions & 0 deletions Sources/Site/Posts/Drafts/2024-09-01-Shaders.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//
// 2024-09-01-Shaders.swift
// jeffverkoeyen.com
//
// Created by Jeff Verkoeyen on 9/1/24.
//

9 changes: 2 additions & 7 deletions Sources/Site/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,8 @@ for (index, post) in posts.enumerated() {
previous: previous
)
}
if let mostRecentPost = posts.last {
sitemap["blog/index.html"] = BlogPostView(
post: mostRecentPost,
next: nil,
previous: posts.dropLast().last
)
}

sitemap["blog/index.html"] = BlogList(posts: posts)

try renderSitemap(sitemap, to: siteURL)

Expand Down

0 comments on commit b92e29f

Please sign in to comment.