Full page content, fixes, cleaner settings

This commit is contained in:
Christoph Hagen
2024-12-13 11:26:34 +01:00
parent efc9234917
commit b3b8c9a610
50 changed files with 1351 additions and 607 deletions

View File

@@ -1,56 +1,47 @@
extension Content {
#warning("Get tag url prefix from settings")
func tagLink(_ tag: Tag, language: ContentLanguage) -> String {
"/tags/\(tag.localized(in: language).urlComponent).html"
private func makeCleanAbsolutePath(_ path: String) -> String {
("/" + path).replacingOccurrences(of: "//", with: "/")
}
func pageLink(_ page: Page, language: ContentLanguage) -> String {
private func pathPrefix(for file: FileResource) -> String {
switch file.type {
case .image: return settings.paths.imagesOutputFolderPath
case .video: return settings.paths.videosOutputFolderPath
default: return settings.paths.filesOutputFolderPath
}
}
// MARK: Paths to items
func absoluteUrlPrefixForTag(_ tag: Tag, language: ContentLanguage) -> String {
makeCleanAbsolutePath(settings.paths.tagsOutputFolderPath + "/" + tag.localized(in: language).urlComponent)
}
func absoluteUrlToTag(_ tag: Tag, language: ContentLanguage) -> String {
absoluteUrlPrefixForTag(tag, language: language) + ".html"
}
func absoluteUrlToPage(_ page: Page, language: ContentLanguage) -> String {
// TODO: Record link to trace connections between pages
var prefix = settings.pages.pageUrlPrefix
if !prefix.hasPrefix("/") {
prefix = "/" + prefix
}
if !prefix.hasSuffix("/") {
prefix.append("/")
}
return prefix + page.localized(in: language).urlString
makeCleanAbsolutePath(settings.pages.pageUrlPrefix + "/" + page.localized(in: language).urlString)
}
/**
Get the url path to a file in the output folder.
The result is an absolute path from the output folder for use in HTML.
*/
func absoluteUrlToFile(_ file: FileResource) -> String {
let path = pathPrefix(for: file) + "/" + file.id
return makeCleanAbsolutePath(path)
}
// MARK: Find items by id
func page(_ pageId: String) -> Page? {
pages.first { $0.id == pageId }
}
func pageLink(pageId: String, language: ContentLanguage) -> String? {
guard let page = pages.first(where: { $0.id == pageId }) else {
// TODO: Note missing link
print("Missing page \(pageId) linked")
return nil
}
return pageLink(page, language: language)
}
func pathToFile(_ fileId: String) -> String? {
guard let file = file(id: fileId) else {
return nil
}
switch file.type {
case .image: return pathToImage(file)
case .video: return pathToVideo(file)
default: return pathToFile(file)
}
}
func pathToFile(_ file: FileResource) -> String {
#warning("Add files path to settings")
return "/files/\(file.id)"
}
func pathToImage(_ image: FileResource) -> String {
return "/images/\(image.id)"
}
func image(_ imageId: String) -> FileResource? {
files.first { $0.id == imageId && $0.type.isImage }
}
@@ -59,19 +50,11 @@ extension Content {
files.first { $0.id == videoId && $0.type.isVideo }
}
func pathToVideo(_ videoId: String) -> String? {
guard let video = video(videoId) else {
return nil
}
return pathToVideo(video)
}
func pathToVideo(_ video: FileResource) -> String {
"/videos/\(video.id)"
}
func file(id: String) -> FileResource? {
files.first { $0.id == id }
}
func tag(_ tagId: String) -> Tag? {
tags.first { $0.id == tagId }
}
}