2022-08-16 10:39:05 +02:00
|
|
|
import Foundation
|
|
|
|
|
|
|
|
protocol PageHeadInfoProvider {
|
|
|
|
|
|
|
|
var author: String { get }
|
|
|
|
|
|
|
|
var linkPreviewTitle: String { get }
|
|
|
|
|
|
|
|
var linkPreviewDescription: String { get }
|
|
|
|
|
|
|
|
var linkPreviewImage: String? { get }
|
|
|
|
|
|
|
|
var customHeadContent: String? { get }
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PageHeadInfo: PageHeadInfoProvider {
|
|
|
|
|
|
|
|
let author: String
|
|
|
|
|
|
|
|
let linkPreviewTitle: String
|
|
|
|
|
|
|
|
let linkPreviewDescription: String
|
|
|
|
|
|
|
|
let linkPreviewImage: String?
|
|
|
|
|
|
|
|
let customHeadContent: String?
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PageHeadGenerator {
|
|
|
|
|
|
|
|
let template: PageHeadTemplate
|
|
|
|
|
2022-08-16 12:26:45 +02:00
|
|
|
let files: FileProcessor
|
2022-08-16 10:39:05 +02:00
|
|
|
|
2022-08-16 12:26:45 +02:00
|
|
|
init(factory: TemplateFactory, files: FileProcessor) {
|
2022-08-16 10:39:05 +02:00
|
|
|
self.template = factory.pageHead
|
2022-08-16 12:26:45 +02:00
|
|
|
self.files = files
|
2022-08-16 10:39:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func generate(page: PageHeadInfoProvider) throws -> String {
|
|
|
|
var content = [PageHeadTemplate.Key : String]()
|
|
|
|
content[.author] = page.author
|
|
|
|
content[.title] = page.linkPreviewTitle
|
|
|
|
content[.description] = page.linkPreviewDescription
|
|
|
|
if let image = page.linkPreviewImage {
|
|
|
|
// Note: Generate separate destination link for the image,
|
|
|
|
// since we don't want a single large image for thumbnails.
|
|
|
|
// Warning: Link preview source path must be relative to root
|
|
|
|
let linkPreviewImagePath = image.insert("-link", beforeLast: ".")
|
2022-08-16 12:26:45 +02:00
|
|
|
try files.requireImage(
|
2022-08-16 10:39:05 +02:00
|
|
|
source: image,
|
|
|
|
destination: linkPreviewImagePath,
|
|
|
|
width: Site.linkPreviewDesiredImageWidth)
|
|
|
|
#warning("Make link preview image path absolute")
|
|
|
|
content[.image] = "<meta property=\"og:image\" content=\"\(linkPreviewImagePath)\" />"
|
|
|
|
}
|
|
|
|
content[.customPageContent] = page.customHeadContent
|
|
|
|
|
|
|
|
return template.generate(content)
|
|
|
|
}
|
|
|
|
}
|