feat: Initial commit

This commit is contained in:
2025-02-19 17:01:08 -05:00
commit e0fb6129ad
2362 changed files with 325107 additions and 0 deletions

View File

@@ -0,0 +1,104 @@
import Foundation
import HTML
enum Section: String {
case home
case articles
case about
case notFound
}
func baseLayout(
canocicalURL: String,
section: Section,
title pageTitle: String,
rssLink: String = "",
extraHeader: NodeConvertible = Node.fragment([]),
@NodeBuilder children: () -> NodeConvertible
) -> Node {
return [
.documentType("html"),
html(lang: "en-US") {
generateHeader(pageTitle, extraHeader)
body(class: "bg-page text-white pb-5 font-avenir \(section.rawValue)") {
header(class: "bg-nav text-gray py-4 text-base/6 lg:fixed w-full lg:h-[62px]") {
nav(class: "container flex gap-x-5 lg:gap-x-y items-center") {
ul(class: "flex flex-wrap gap-x-2 lg:gap-x-5") {
li {
a(class: section == .home ? "active" : "", href: "/") { "Home" }
}
li {
a(class: section == .articles ? "active" : "", href: "/articles/") { "Articles" }
}
li {
a(class: section == .about ? "active" : "", href: "/about/") { "About" }
}
}
}
}
div(class: "container pt-12 lg:pt-28") {
children()
}
footer(rssLink)
}
}
]
}
private func footer(_ rssLink: String) -> Node {
div(class: "site-footer container text-gray gray-links border-t border-light text-center pt-6 mt-8 text-sm") {
p {
"Copyright © Michael Housh 2023-\(Date().description.prefix(4))."
}
p {
"Built in Swift using"
a(href: "https://github.com/loopwerk/Saga", rel: "nofollow", target: "_blank") { "Saga" }
"("
%a(href: "https://github.com/m-housh/mhoush.com", rel: "nofollow", target: "_blank") { "source" }
%")."
}
p {
a(
href: "\(SiteMetadata.url.absoluteString)/articles/\(rssLink)feed.xml",
rel: "nofollow",
target: "_blank"
) { "RSS" }
" | "
a(href: "https://github.com/m-housh", rel: "nofollow", target: "_blank") { "Github" }
" | "
a(
href: "https://www.youtube.com/channel/UCb58SeURd5bObfTiL0KoliA",
rel: "nofollow",
target: "_blank"
) { "Youtube" }
" | "
a(href: "https://www.facebook.com/michael.housh", rel: "nofollow", target: "_blank") { "Facebook" }
" | "
a(href: "mailto:michael@mhoush.com", rel: "nofollow") { "Email" }
}
}
}
private func generateHeader(_ pageTitle: String, _ extraHeader: NodeConvertible) -> Node {
head {
meta(charset: "utf-8")
meta(content: "#0e1112", name: "theme-color", customAttributes: ["media": "(prefers-color-scheme: dark)"])
meta(content: "#566B78", name: "theme-color", customAttributes: ["media": "(prefers-color-scheme: light)"])
meta(content: "Michael Housh", name: "author")
meta(content: "Mhoush", name: "apple-mobile-web-app-title")
meta(content: "initial-scale=1.0, width=device-width", name: "viewport")
meta(content: "telephone=no", name: "format-detection")
meta(content: "True", name: "HandheldFriendly")
meta(content: "320", name: "MobileOptimized")
meta(content: "Mhoush", name: "og:site_name")
meta(content: "hvac, developer, swift, home-performance, design", name: "keywords")
title { SiteMetadata.name + ": \(pageTitle)" }
link(href: "/static/favicon.ico", rel: "shortcut icon")
link(href: "/static/output.css", rel: "stylesheet")
link(href: "/static/style.css", rel: "stylesheet")
link(href: "/articles/feed.xml", rel: "alternate", title: SiteMetadata.name, type: "application/rss+xml")
extraHeader
}
}

View File

@@ -0,0 +1,128 @@
import Foundation
import HTML
import Saga
func tagPrefix(index: Int, totalTags: Int) -> Node {
if index > 0 {
if index == totalTags - 1 {
return " and "
} else {
return ", "
}
}
return ""
}
func renderArticleInfo(_ article: Item<ArticleMetadata>) -> Node {
div(class: "text-gray gray-links text-sm") {
span(class: "border-r border-gray pr-2 mr-2") {
article.date.formatted("MMMM dd, yyyy")
}
%.text("\(article.body.withoutHtmlTags.numberOfWords) words, posted in ")
article.metadata.tags.sorted().enumerated().map { index, tag in
Node.fragment([
%tagPrefix(index: index, totalTags: article.metadata.tags.count),
%a(href: "/articles/tag/\(tag.slugified)/") { tag }
])
}
}
}
func ogURL(_ article: Item<ArticleMetadata>) -> String {
SiteMetadata.url
.appendingPathComponent("/articles/images/\(article.url)")
.absoluteString
}
@NodeBuilder
func getArticleHeader(_ article: Item<ArticleMetadata>) -> NodeConvertible {
link(href: "/static/prism.css", rel: "stylesheet")
meta(content: article.summary, name: "description")
meta(content: "summary_large_image", name: "twitter:card")
meta(content: article.imagePath, name: "twitter:image")
meta(content: article.title, name: "twitter:image:alt")
meta(content: ogURL(article), name: "og:url")
meta(content: article.title, name: "og:title")
meta(content: article.summary, name: "og:description")
meta(content: article.imagePath, name: "og:image")
meta(content: "1014", name: "og:image:width")
meta(content: "530", name: "og:image:height")
script(crossorigin: "anonymous", src: "https://kit.fontawesome.com/f209982030.js")
}
func renderArticle(context: ItemRenderingContext<ArticleMetadata>) -> Node {
let extraHeader = getArticleHeader(context.item)
let allArticles = context.allItems.compactMap { $0 as? Item<ArticleMetadata> }
let otherArticles = allArticles.filter { $0.url != context.item.url }.prefix(2)
return baseLayout(
canocicalURL: context.item.url,
section: .articles,
title: context.item.title,
extraHeader: extraHeader
) {
article(class: "prose") {
h1 { context.item.title }
div(class: "-mt-6") {
renderArticleInfo(context.item)
}
img(alt: "banner", src: context.item.imagePath)
Node.raw(context.item.body)
}
div(class: "border-t border-light mt-8 pt-8") {
h2(class: "text-4xl font-extrabold mb-8") { "Written by" }
div(class: "flex flex-col lg:flex-row gap-8") {
div(class: "flex-[0_0_120px]") {
img(class: "w-[120px] h-[120px] rounded-full", src: "/static/images/avatar.png")
}
div(class: "prose") {
h3(class: "!m-0") { SiteMetadata.author }
p(class: "text-gray") {
"""
HVAC business owner with over 27 years of experience. Writes articles about HVAC,
Programming, Home-Performance, and Building Science
"""
}
}
}
}
div(class: "mt-16") {
h2(class: "text-4xl font-extrabold mb-8") { "More articles" }
div(class: "grid lg:grid-cols-2 gap-10") {
otherArticles.map { renderArticleForGrid(article: $0) }
}
p(class: "prose mt-8") {
a(href: "/articles/") { " See all articles" }
}
}
// div(class: "border-t border-light mt-8 pt-8") {
// Node.raw("""
// <script src="https://giscus.app/client.js"
// data-repo="loopwerk/loopwerk.io"
// data-repo-id="MDEwOlJlcG9zaXRvcnk0Nzg0NTA3MA=="
// data-category="Article discussions"
// data-category-id="DIC_kwDOAtoOzs4Ciykw"
// data-mapping="pathname"
// data-strict="1"
// data-reactions-enabled="1"
// data-emit-metadata="0"
// data-input-position="bottom"
// data-theme="preferred_color_scheme"
// data-lang="en"
// data-loading="lazy"
// crossorigin="anonymous"
// async>
// </script>
// """)
// }
}
}

View File

@@ -0,0 +1,83 @@
import Foundation
import HTML
import Saga
func uniqueTagsWithCount(_ articles: [Item<ArticleMetadata>]) -> [(String, Int)] {
let tags = articles.flatMap { $0.metadata.tags }
let tagsWithCounts = tags.reduce(into: [:]) { $0[$1, default: 0] += 1 }
return tagsWithCounts.sorted { $0.1 > $1.1 }
}
func renderArticleForGrid(article: Item<ArticleMetadata>) -> Node {
section {
h2(class: "text-2xl font-bold mb-2") {
a(class: "[&:hover]:border-b border-orange", href: article.url) { article.title }
}
div(class: "text-gray gray-links text-sm mb-4") {
span(class: "border-r border-gray pr-2 mr-2") {
article.date.formatted("MMMM dd, YYYY")
}
article.metadata.tags.sorted().enumerated().map { index, tag in
Node.fragment([
%tagPrefix(index: index, totalTags: article.metadata.tags.count),
%a(href: "/articles/tag/\(tag.slugified)/") { tag }
])
}
}
p {
a(href: article.url) {
div {
img(alt: "banner", src: article.imagePath)
article.summary
}
}
}
}
}
func renderArticles(context: ItemsRenderingContext<ArticleMetadata>) -> Node {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy"
let articlesPerYear = Dictionary(grouping: context.items, by: { dateFormatter.string(from: $0.date) })
let sortedByYearDescending = articlesPerYear.sorted { $0.key > $1.key }
return baseLayout(canocicalURL: "/articles/", section: .articles, title: "Articles", rssLink: "", extraHeader: "") {
sortedByYearDescending.map { year, articles in
div {
h1(class: "text-4xl font-extrabold mb-12") { year }
div(class: "grid gap-10 mb-16") {
articles.map { renderArticleForGrid(article: $0) }
}
}
}
}
}
func _renderArticles(_ articles: [Item<ArticleMetadata>], canocicalURL: String, title pageTitle: String, rssLink: String = "", extraHeader: NodeConvertible = Node.fragment([])) -> Node {
return baseLayout(canocicalURL: canocicalURL, section: .articles, title: pageTitle, rssLink: rssLink, extraHeader: extraHeader) {
articles.map { article in
section(class: "mb-10") {
h1(class: "text-2xl font-bold mb-2") {
a(class: "[&:hover]:border-b border-orange", href: article.url) { article.title }
}
renderArticleInfo(article)
p(class: "mt-4") {
a(href: article.url) { article.summary }
}
}
}
}
}
func renderTag<T>(context: PartitionedRenderingContext<T, ArticleMetadata>) -> Node {
let extraHeader = link(href: "/articles/tag/\(context.key.slugified)/feed.xml", rel: "alternate", title: "\(SiteMetadata.name): articles with tag \(context.key)", type: "application/rss+xml")
return _renderArticles(context.items, canocicalURL: "/articles/tag/\(context.key.slugified)/", title: "Articles in \(context.key)", rssLink: "tag/\(context.key.slugified)/", extraHeader: extraHeader)
}
func renderYear<T>(context: PartitionedRenderingContext<T, ArticleMetadata>) -> Node {
_renderArticles(context.items, canocicalURL: "/articles/\(context.key)/", title: "Articles in \(context.key)")
}

View File

@@ -0,0 +1,57 @@
import HTML
import Saga
func renderPage(context: ItemRenderingContext<PageMetadata>) -> Node {
let section = Section(rawValue: context.item.metadata.section ?? "")
assert(section != nil)
return baseLayout(canocicalURL: context.item.url, section: section!, title: context.item.title) {
switch section {
case .home:
renderHome(body: context.item.body)
case .notFound:
let articles = context.allItems
.compactMap { $0 as? Item<ArticleMetadata> }
.prefix(10)
render404(body: context.item.body, articles: Array(articles))
default:
renderNonHome(body: context.item.body)
}
}
}
func renderHome(body: String) -> Node {
div {
img(alt: "Avatar", class: "my-24 w-[315px] h-200px mx-auto", src: "/static/images/avatar.png")
div(class: "my-24 uppercase font-avenir text-[40px] leading-[1.25] font-thin text-center [&>h1>strong]:font-bold") {
Node.raw(body)
}
}
}
func renderNonHome(body: String) -> Node {
article {
div(class: "prose") {
Node.raw(body)
}
}
}
func render404(body: String, articles: [Item<ArticleMetadata>]) -> Node {
article(class: "prose") {
Node.raw(body)
ul {
articles.map { article in
li {
a(href: article.url) { article.title }
}
}
}
div {
a(href: "/articles/") { " See all articles" }
}
}
}