Fix page paths

Update PostListPageGenerator.swift
This commit is contained in:
Christoph Hagen 2025-01-07 10:31:46 +01:00
parent f8150e0809
commit dd2b7d6cd2
7 changed files with 31 additions and 8 deletions

View File

@ -16,6 +16,13 @@ extension String {
return decoded
}
var withLeadingSlashRemoved: String {
if !hasPrefix("/") {
return self
}
return String(dropFirst("/".count))
}
var removingSurroundingQuotes: String {
if hasPrefix("\"") && hasSuffix("\"") {
return dropBeforeFirst("\"").dropAfterLast("\"")

View File

@ -23,7 +23,14 @@ struct FeedGeneratorSource: PostListPageGeneratorSource {
content.settings.localized(in: language).description
}
/**
The url to the page, including a leading slash
*/
func pageUrlPrefix(for language: ContentLanguage) -> String {
content.settings.localized(in: language).feedUrlPrefix
let prefix = content.settings.localized(in: language).feedUrlPrefix
if prefix.hasPrefix( "/" ) {
return prefix
}
return "/" + prefix
}
}

View File

@ -74,7 +74,8 @@ final class PostListPageGenerator {
let feedPageGenerator = FeedPageGenerator(content: source.content, results: source.results)
let languageButtonUrl = "/" + pageUrl(in: language.next, pageNumber: pageIndex)
// Includes leading slash
let languageButtonUrl = pageUrl(in: language.next, pageNumber: pageIndex)
let fileContent = feedPageGenerator.generatePage(
language: language,

View File

@ -13,6 +13,9 @@ protocol PostListPageGeneratorSource {
var pageDescription: String { get }
/**
The url to the page, including a leading slash
*/
func pageUrlPrefix(for language: ContentLanguage) -> String
var postsPerPage: Int { get }

View File

@ -250,7 +250,11 @@ final class FileResource: Item {
*/
var absoluteUrl: String {
if let customOutputPath {
return customOutputPath
if customOutputPath.hasPrefix("/") {
return customOutputPath
} else {
return "/" + customOutputPath
}
}
let path = pathPrefix + "/" + id
return makeCleanAbsolutePath(path)

View File

@ -2,6 +2,7 @@ import Foundation
struct PostFeedPageNavigation {
/// Includes a leading slash
let linkPrefix: String
let currentPage: Int
@ -15,7 +16,7 @@ struct PostFeedPageNavigation {
}
private func pageLink(_ page: Int) -> String {
"href='/\(linkPrefix)/\(page)'"
"href='\(linkPrefix)/\(page)'"
}
private func addPreviousButton(to result: inout String) {

View File

@ -38,7 +38,7 @@ struct SecurityBookmark {
}
func fullPath(to relativePath: String) -> URL {
url.appending(path: relativePath, directoryHint: .notDirectory)
return url.appending(path: relativePath.withLeadingSlashRemoved, directoryHint: .notDirectory)
}
/**
@ -167,7 +167,7 @@ struct SecurityBookmark {
return false
}
let destination = url.appending(path: relativeDestination)
let destination = url.appending(path: relativeDestination.withLeadingSlashRemoved)
if exists(destination) {
switch overwrite {
case .fail:
@ -328,11 +328,11 @@ struct SecurityBookmark {
// MARK: Generic operations
func with(relativePath: String, perform operation: (URL) -> Bool) -> Bool {
perform { operation($0.appending(path: relativePath)) }
perform { operation($0.appending(path: relativePath.withLeadingSlashRemoved)) }
}
func with<T>(relativePath: String, perform operation: (URL) -> T?) -> T? {
perform { operation($0.appending(path: relativePath)) }
perform { operation($0.appending(path: relativePath.withLeadingSlashRemoved)) }
}
/**