Shared HTML template for the Website service (#22)
This PR contains the work done to define a `Page` protocol that extracts the HTML scaffolding that `IndexPage` and `ErrorPage` pages duplicated, so every page of the website declares only what makes it unique — its content, title, and assets — while the document structure lives in one place. Also cleans up imports across the workspace. Reviewed-on: rock-n-code/loud-amsterdam#22 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
This commit is contained in:
@@ -3,12 +3,12 @@ import Foundation
|
||||
import Localization
|
||||
|
||||
/// The website's landing page, with its text localized to a given locale.
|
||||
struct IndexPage: HTMLDocument, Sendable {
|
||||
struct IndexPage {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
/// The locale the page content is localized to.
|
||||
private let locale: Locale
|
||||
let locale: Locale
|
||||
|
||||
/// Resolves the page's text from the bundled String Catalog for the page's ``locale``.
|
||||
private let localize: Localize
|
||||
@@ -24,77 +24,28 @@ struct IndexPage: HTMLDocument, Sendable {
|
||||
self.localize = .init(bundle: .module)
|
||||
}
|
||||
|
||||
// MARK: Document
|
||||
}
|
||||
|
||||
/// The page's content: a localized greeting followed by the shared and index scripts.
|
||||
var body: some HTML {
|
||||
// MARK: - Page
|
||||
|
||||
extension IndexPage: Page {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
var content: some HTML {
|
||||
p {
|
||||
localize("index.greeting", locale: locale)
|
||||
}
|
||||
script(.src(StaticFile.index.urlPath(for: .js))) {}
|
||||
script(.src(StaticFile.shared.urlPath(for: .js))) {}
|
||||
}
|
||||
|
||||
/// The metadata, stylesheet, icon, and manifest links placed in the document head.
|
||||
var head: some HTML {
|
||||
meta(.charset(.utf8))
|
||||
meta(
|
||||
.name(.viewport),
|
||||
.content("width=device-width, initial-scale=1")
|
||||
)
|
||||
link(
|
||||
.rel(.stylesheet),
|
||||
.href(StaticFile.shared.urlPath(for: .css))
|
||||
)
|
||||
link(
|
||||
.rel(.stylesheet),
|
||||
.href(StaticFile.index.urlPath(for: .css))
|
||||
)
|
||||
link(
|
||||
.rel(.icon),
|
||||
.href(StaticFile.favicon.urlPath(for: .ico)),
|
||||
.custom(
|
||||
name: "sizes",
|
||||
value: "any"
|
||||
)
|
||||
)
|
||||
link(
|
||||
.rel(.icon),
|
||||
.href(StaticFile.icon.urlPath(for: .svg)),
|
||||
.custom(
|
||||
name: "type",
|
||||
value: "image/svg+xml"
|
||||
)
|
||||
)
|
||||
link(
|
||||
.rel("apple-touch-icon"),
|
||||
.href(StaticFile.appleTouchIcon.urlPath(for: .png))
|
||||
)
|
||||
link(
|
||||
.rel("manifest"),
|
||||
.href(StaticFile.site.urlPath(for: .webmanifest))
|
||||
)
|
||||
meta(
|
||||
.name("theme-color"),
|
||||
.content("#0c0710"),
|
||||
.custom(
|
||||
name: "media",
|
||||
value: "(prefers-color-scheme: dark)"
|
||||
)
|
||||
)
|
||||
meta(
|
||||
.name("theme-color"),
|
||||
.content("#fafafa")
|
||||
)
|
||||
var scripts: [StaticFile] {
|
||||
[.index, .shared]
|
||||
}
|
||||
|
||||
/// The document language, derived from the page's locale and falling back to the default language.
|
||||
var lang: String {
|
||||
locale.language.languageCode?.identifier
|
||||
?? LanguageList(bundle: .module).default
|
||||
var stylesheets: [StaticFile] {
|
||||
[.shared, .index]
|
||||
}
|
||||
|
||||
/// The localized document title.
|
||||
var title: String {
|
||||
localize("index.title", locale: locale)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user