feat: Unifies article lists to have similar view styles.
All checks were successful
CI / release (push) Successful in 5m36s
All checks were successful
CI / release (push) Successful in 5m36s
This commit is contained in:
56
Sources/Docs/Templates/ArticleGrid.swift
Normal file
56
Sources/Docs/Templates/ArticleGrid.swift
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
import Foundation
|
||||||
|
import HTML
|
||||||
|
import Saga
|
||||||
|
|
||||||
|
/// Displays lists of articles sectioned by a key.
|
||||||
|
///
|
||||||
|
///
|
||||||
|
struct ArticleGrid: NodeConvertible {
|
||||||
|
|
||||||
|
let articles: [(key: String, value: [Item<ArticleMetadata>])]
|
||||||
|
let canocicalURL: String
|
||||||
|
let title: String
|
||||||
|
let rssLink: String
|
||||||
|
let extraHeader: NodeConvertible
|
||||||
|
let renderArticle: (Item<ArticleMetadata>) -> Node
|
||||||
|
let header: (String) -> Node
|
||||||
|
|
||||||
|
init(
|
||||||
|
articles: [(key: String, value: [Item<ArticleMetadata>])],
|
||||||
|
canocicalURL: String,
|
||||||
|
title: String,
|
||||||
|
rssLink: String,
|
||||||
|
extraHeader: any NodeConvertible = Node.fragment([]),
|
||||||
|
renderArticle: @escaping (Item<ArticleMetadata>) -> Node = { renderArticleForGrid(article: $0, border: false) },
|
||||||
|
header: @escaping (String) -> Node
|
||||||
|
) {
|
||||||
|
self.articles = articles
|
||||||
|
self.canocicalURL = canocicalURL
|
||||||
|
self.title = title
|
||||||
|
self.rssLink = rssLink
|
||||||
|
self.extraHeader = extraHeader
|
||||||
|
self.renderArticle = renderArticle
|
||||||
|
self.header = header
|
||||||
|
}
|
||||||
|
|
||||||
|
func asNode() -> Node {
|
||||||
|
baseLayout(
|
||||||
|
canocicalURL: canocicalURL,
|
||||||
|
section: .articles,
|
||||||
|
title: title,
|
||||||
|
rssLink: rssLink,
|
||||||
|
extraHeader: extraHeader
|
||||||
|
) {
|
||||||
|
div(class: "mt-8 mb-10 bg-slate-800 border border-slate-200 rounded-lg") {
|
||||||
|
articles.map { key, articles in
|
||||||
|
section {
|
||||||
|
header(key)
|
||||||
|
div(class: "grid gap-10 mx-6 mb-16") {
|
||||||
|
articles.map(renderArticle)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,7 +24,7 @@ func baseLayout(
|
|||||||
body(class: "text-white text-lg font-avenir \(section.rawValue)") {
|
body(class: "text-white text-lg font-avenir \(section.rawValue)") {
|
||||||
siteHeader(section)
|
siteHeader(section)
|
||||||
|
|
||||||
div(class: "mb-auto\(section == .articles ? " mx-10" : "")") {
|
div(class: "mb-auto mx-10") {
|
||||||
children()
|
children()
|
||||||
}
|
}
|
||||||
if section == .articles {
|
if section == .articles {
|
||||||
@@ -57,7 +57,7 @@ private func siteHeader(_ section: Section) -> Node {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
div(class: "font-avenir w-full pt-4", id: "search") {}
|
div(class: "font-avenir w-full pt-4 px-8", id: "search") {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,32 +2,24 @@ import Foundation
|
|||||||
import HTML
|
import HTML
|
||||||
import Saga
|
import Saga
|
||||||
|
|
||||||
func renderArticles(context: ItemsRenderingContext<ArticleMetadata>) -> Node {
|
func renderArticles(context: ItemsRenderingContext<ArticleMetadata>) -> NodeConvertible {
|
||||||
let dateFormatter = DateFormatter()
|
let dateFormatter = DateFormatter()
|
||||||
dateFormatter.dateFormat = "yyyy"
|
dateFormatter.dateFormat = "yyyy"
|
||||||
|
|
||||||
let articlesPerYear = Dictionary(grouping: context.items, by: { dateFormatter.string(from: $0.date) })
|
let articlesPerYear = Dictionary(grouping: context.items, by: { dateFormatter.string(from: $0.date) })
|
||||||
let sortedByYearDescending = articlesPerYear.sorted { $0.key > $1.key }
|
let sortedByYearDescending = articlesPerYear.sorted { $0.key > $1.key }
|
||||||
|
|
||||||
return baseLayout(canocicalURL: "/articles/", section: .articles, title: "Articles", rssLink: "", extraHeader: "") {
|
return ArticleGrid(
|
||||||
sortedByYearDescending.map { year, articles in
|
articles: sortedByYearDescending,
|
||||||
div(class: "mt-8 mb-10 bg-slate-800 border border-slate-200 rounded-lg") {
|
canocicalURL: "/articles/",
|
||||||
TagGrid(items: context.items)
|
title: "Articles",
|
||||||
div(class: "border-b border-light pt-6 w-full") {
|
rssLink: "",
|
||||||
div(class: "px-6 flex flex-row gap-4 ") {
|
extraHeader: "",
|
||||||
img(src: "/static/img/calendar.svg", width: "40")
|
header: yearHeader(_:)
|
||||||
h1(class: "text-4xl font-extrabold pt-3") { year }
|
)
|
||||||
}
|
|
||||||
}
|
|
||||||
div(class: "grid gap-10 mx-6 mb-16") {
|
|
||||||
articles.map { renderArticleForGrid(article: $0, border: false) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func renderTag<T>(context: PartitionedRenderingContext<T, ArticleMetadata>) -> Node {
|
func renderTag<T>(context: PartitionedRenderingContext<T, ArticleMetadata>) -> NodeConvertible {
|
||||||
let extraHeader = link(
|
let extraHeader = link(
|
||||||
href: "/articles/tag/\(context.key.slugified)/feed.xml",
|
href: "/articles/tag/\(context.key.slugified)/feed.xml",
|
||||||
rel: "alternate",
|
rel: "alternate",
|
||||||
@@ -36,54 +28,67 @@ func renderTag<T>(context: PartitionedRenderingContext<T, ArticleMetadata>) -> N
|
|||||||
)
|
)
|
||||||
|
|
||||||
return baseRenderArticles(
|
return baseRenderArticles(
|
||||||
context.items,
|
(context.key.slugified, context.items),
|
||||||
canocicalURL: "/articles/tag/\(context.key.slugified)/",
|
canocicalURL: "/articles/tag/\(context.key.slugified)/",
|
||||||
title: "Articles in \(context.key)",
|
title: "Articles in \(context.key)",
|
||||||
rssLink: "tag/\(context.key.slugified)/",
|
rssLink: "tag/\(context.key.slugified)/",
|
||||||
extraHeader: extraHeader
|
extraHeader: extraHeader
|
||||||
) {
|
) { tag in
|
||||||
div(class: "border-b border-light grid lg:grid-cols-2 mb-12") {
|
div(class: "border-b border-light mb-12 px-6 pt-6") {
|
||||||
a(href: "/articles") {
|
a(href: "/articles") {
|
||||||
div(class: "flex flex-row gap-2") {
|
div(class: "flex flex-row gap-4") {
|
||||||
img(src: "/static/img/tag.svg", width: "40")
|
img(src: "/static/img/tag.svg", width: "40")
|
||||||
h1(class: "text-4xl font-extrabold") { "\(context.key)" }
|
h1(class: "text-4xl font-extrabold") { tag }
|
||||||
h1(class: "[&:hover]:border-b border-green text-2xl font-extrabold text-orange px-4") { "«" }
|
h1(class: "text-2xl font-extrabold") { "«" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func renderYear<T>(context: PartitionedRenderingContext<T, ArticleMetadata>) -> Node {
|
func renderYear<T>(context: PartitionedRenderingContext<T, ArticleMetadata>) -> NodeConvertible {
|
||||||
baseRenderArticles(context.items, canocicalURL: "/articles/\(context.key)/", title: "Articles in \(context.key)")
|
baseRenderArticles(
|
||||||
|
(context.key.slugified, context.items),
|
||||||
|
canocicalURL: "/articles/\(context.key)/",
|
||||||
|
title: "Articles in \(context.key)",
|
||||||
|
label: { year in
|
||||||
|
div(class: "border-b border-light pt-6 w-full") {
|
||||||
|
a(href: "/articles/") {
|
||||||
|
div(class: "px-6 flex flex-row gap-4 ") {
|
||||||
|
img(src: "/static/img/calendar.svg", width: "40")
|
||||||
|
h1(class: "text-4xl font-extrabold pt-3") { year }
|
||||||
|
h1(class: "text-2xl font-extrabold") { "«" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func yearHeader(_ year: String) -> Node {
|
||||||
|
div(class: "border-b border-light pt-6 w-full") {
|
||||||
|
div(class: "px-6 flex flex-row gap-4 ") {
|
||||||
|
img(src: "/static/img/calendar.svg", width: "40")
|
||||||
|
h1(class: "text-4xl font-extrabold pt-3") { year }
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func baseRenderArticles(
|
private func baseRenderArticles(
|
||||||
_ articles: [Item<ArticleMetadata>],
|
_ articles: (key: String, value: [Item<ArticleMetadata>]),
|
||||||
canocicalURL: String,
|
canocicalURL: String,
|
||||||
title pageTitle: String,
|
title pageTitle: String,
|
||||||
rssLink: String = "",
|
rssLink: String = "",
|
||||||
extraHeader: NodeConvertible = Node.fragment([]),
|
extraHeader: NodeConvertible = Node.fragment([]),
|
||||||
@NodeBuilder label: () -> Node = { Node.fragment([]) }
|
@NodeBuilder label: @escaping (String) -> Node = { _ in Node.fragment([]) }
|
||||||
) -> Node {
|
) -> NodeConvertible {
|
||||||
return baseLayout(
|
ArticleGrid(
|
||||||
|
articles: [articles],
|
||||||
canocicalURL: canocicalURL,
|
canocicalURL: canocicalURL,
|
||||||
section: .articles,
|
|
||||||
title: pageTitle,
|
title: pageTitle,
|
||||||
rssLink: rssLink,
|
rssLink: rssLink,
|
||||||
extraHeader: extraHeader
|
extraHeader: extraHeader
|
||||||
) {
|
) { key in
|
||||||
label()
|
label(key)
|
||||||
articles.map { article in
|
|
||||||
section {
|
|
||||||
h1(class: "text-2xl font-bold") {
|
|
||||||
a(class: "[&:hover]:border-b border-orange", href: article.url) { article.title }
|
|
||||||
}
|
|
||||||
renderArticleInfo(article)
|
|
||||||
p {
|
|
||||||
a(href: article.url) { article.summary }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user