2022-08-16 10:39:05 +02:00
|
|
|
import Foundation
|
|
|
|
|
|
|
|
struct PageHeadGenerator {
|
|
|
|
|
2022-12-04 19:15:22 +01:00
|
|
|
// TODO: Add to configuration
|
2022-08-26 17:40:51 +02:00
|
|
|
static let linkPreviewDesiredImageWidth = 1600
|
2022-08-16 10:39:05 +02:00
|
|
|
|
2022-08-28 14:01:53 +02:00
|
|
|
let factory: TemplateFactory
|
2022-08-16 10:39:05 +02:00
|
|
|
|
2022-12-04 19:15:22 +01:00
|
|
|
private let results: GenerationResultsHandler
|
|
|
|
|
|
|
|
init(factory: TemplateFactory, results: GenerationResultsHandler) {
|
2022-08-28 14:01:53 +02:00
|
|
|
self.factory = factory
|
2022-12-04 19:15:22 +01:00
|
|
|
self.results = results
|
2022-08-16 10:39:05 +02:00
|
|
|
}
|
|
|
|
|
2023-12-16 22:10:27 +01:00
|
|
|
func generate(page: Element, language: String, headers: RequiredHeaders = .init()) -> String {
|
2022-08-26 17:40:51 +02:00
|
|
|
let metadata = page.localized(for: language)
|
|
|
|
|
2022-08-16 10:39:05 +02:00
|
|
|
var content = [PageHeadTemplate.Key : String]()
|
|
|
|
content[.author] = page.author
|
2022-08-26 17:40:51 +02:00
|
|
|
content[.title] = metadata.linkPreviewTitle
|
|
|
|
content[.description] = metadata.linkPreviewDescription
|
|
|
|
if let image = page.linkPreviewImage(for: language) {
|
2022-08-16 10:39:05 +02:00
|
|
|
// 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
|
2022-09-29 21:23:41 +02:00
|
|
|
let linkPreviewImageName = "thumbnail-link.\(image.lastComponentAfter("."))"
|
2022-08-26 17:40:51 +02:00
|
|
|
let sourceImagePath = page.pathRelativeToRootForContainedInputFile(image)
|
|
|
|
let destinationImagePath = page.pathRelativeToRootForContainedInputFile(linkPreviewImageName)
|
2022-12-04 19:15:22 +01:00
|
|
|
results.requireSingleImage(
|
2022-08-26 17:40:51 +02:00
|
|
|
source: sourceImagePath,
|
|
|
|
destination: destinationImagePath,
|
2022-09-18 16:47:13 +02:00
|
|
|
requiredBy: page.path,
|
2022-08-26 17:40:51 +02:00
|
|
|
width: PageHeadGenerator.linkPreviewDesiredImageWidth)
|
2022-08-28 14:01:53 +02:00
|
|
|
content[.image] = factory.html.linkPreviewImage(file: linkPreviewImageName)
|
2022-08-16 10:39:05 +02:00
|
|
|
}
|
2022-12-04 19:15:22 +01:00
|
|
|
content[.customPageContent] = results.getContentOfOptionalFile(at: page.additionalHeadContentPath, source: page.path)
|
2023-12-16 22:10:27 +01:00
|
|
|
|
|
|
|
let head = (content[.customPageContent].unwrapped { [$0] } ?? []) + headers
|
2023-12-18 21:30:39 +01:00
|
|
|
.sorted { $0.rawValue < $1.rawValue }
|
2023-12-16 22:10:27 +01:00
|
|
|
.map { option in
|
2023-12-18 21:30:39 +01:00
|
|
|
let scriptPath = "assets/js/\(option.rawValue)"
|
2023-12-16 22:10:27 +01:00
|
|
|
let relative = page.relativePathToOtherSiteElement(file: scriptPath)
|
2023-12-18 21:30:39 +01:00
|
|
|
return factory.html.scriptInclude(path: relative, asModule: option.asModule)
|
2022-08-29 13:35:25 +02:00
|
|
|
}
|
2022-08-16 10:39:05 +02:00
|
|
|
|
2023-12-16 22:10:27 +01:00
|
|
|
content[.customPageContent] = head.joined(separator: "\n")
|
2022-08-28 14:01:53 +02:00
|
|
|
return factory.pageHead.generate(content)
|
2022-08-16 10:39:05 +02:00
|
|
|
}
|
|
|
|
}
|