2024-12-14 16:31:40 +01:00

143 lines
3.0 KiB
Swift

import Foundation
final class Page: Item {
/**
The unique id of the entry
*/
@Published
var id: String
/**
The external link this page points to.
If this value is not `nil`, then the page has no content
and many other features are disabled.
*/
@Published
var externalLink: String?
@Published
var isDraft: Bool
@Published
var createdDate: Date
@Published
var startDate: Date
@Published
var hasEndDate: Bool
@Published
var endDate: Date
@Published
var german: LocalizedPage
@Published
var english: LocalizedPage
@Published
var tags: [Tag]
/**
Additional images required by the element.
These images are specified as: `source_name destination_name width (height)`.
*/
@Published
var images: Set<String> = []
init(content: Content,
id: String,
externalLink: String?,
isDraft: Bool,
createdDate: Date,
startDate: Date,
endDate: Date?,
german: LocalizedPage,
english: LocalizedPage,
tags: [Tag]) {
self.id = id
self.externalLink = externalLink
self.isDraft = isDraft
self.createdDate = createdDate
self.startDate = startDate
self.hasEndDate = endDate != nil
self.endDate = endDate ?? startDate
self.german = german
self.english = english
self.tags = tags
super.init(content: content)
}
func localized(in language: ContentLanguage) -> LocalizedPage {
switch language {
case .german: return german
case .english: return english
}
}
func update(id newId: String) -> Bool {
guard content.storage.move(page: id, to: newId) else {
print("Failed to move file of page \(id)")
return false
}
id = newId
return true
}
var isExternalUrl: Bool {
externalLink != nil
}
// MARK: Paths
func absoluteUrl(for language: ContentLanguage) -> String {
if let url = externalLink {
return url
}
// TODO: Record link to trace connections between pages
return makeCleanAbsolutePath(internalPath(for: language))
}
func filePathRelativeToOutputFolder(for language: ContentLanguage) -> String {
makeCleanRelativePath(internalPath(for: language))
}
private func internalPath(for language: ContentLanguage) -> String {
content.settings.pages.pageUrlPrefix + "/" + localized(in: language).urlString
}
}
extension Page: Identifiable {
}
extension Page: Equatable {
static func == (lhs: Page, rhs: Page) -> Bool {
lhs.id == rhs.id
}
}
extension Page: Hashable {
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
extension Page: Comparable {
static func < (lhs: Page, rhs: Page) -> Bool {
lhs.id < rhs.id
}
}
extension Page: DateItem {
}