Skip to content

Commit

Permalink
Base URL changes (#53)
Browse files Browse the repository at this point in the history
- base URL support for image metadata
- removing baseUrl trailing slash
  • Loading branch information
viaszkadi authored Nov 29, 2024
1 parent 998fad6 commit 4907bdf
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
16 changes: 5 additions & 11 deletions Sources/ToucanSDK/Extensions/String+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -262,21 +262,15 @@ extension String {
return baseUrl
}
if components.last?.split(separator: ".").count ?? 0 > 1 {
return baseUrl + components.joined(separator: "/")
return ([baseUrl] + components).joined(separator: "/")
}
return baseUrl + components.joined(separator: "/") + "/"
return ([baseUrl] + components).joined(separator: "/") + "/"
}

// public func transform(
// _ block: (Self) -> Self
// ) -> Self {
// block(self)
// }

func ensureTrailingSlash() -> String {
func dropTrailingSlash() -> String {
if hasSuffix("/") {
return self
return String(dropLast())
}
return self + "/"
return self
}
}
20 changes: 19 additions & 1 deletion Sources/ToucanSDK/PageBundle/PageBundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,31 @@ struct PageBundle {
slug.isEmpty ? "home" : slug
}

/// Resolves the full asset URL by replacing the base URL placeholder or constructing the URL based on the asset folder configuration.
///
/// - Parameters:
/// - path: The relative or placeholder-based asset path.
/// - Returns: The resolved asset URL as a string.
func resolveAsset(path: String) -> String {
if path.contains("{{baseUrl}}") {
return path.replacingOccurrences(of: "{{baseUrl}}", with: baseUrl)
}

let prefix = "./\(config.assets.folder)/"

guard path.hasPrefix(prefix) else {
return path
}

let src = String(path.dropFirst(prefix.count))
return baseUrl + config.assets.folder + "/" + assetsLocation + "/" + src

return [
baseUrl,
config.assets.folder,
assetsLocation,
src,
]
.joined(separator: "/")
}

// MARK: -
Expand Down
22 changes: 7 additions & 15 deletions Sources/ToucanSDK/Site/Site.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,20 @@ struct Site {
}

init(_ dict: [String: Any]) {
self.baseUrl =
(dict.string(Keys.baseUrl)
?? Self.defaults.baseUrl)
.ensureTrailingSlash()
self.baseUrl = (dict.string(Keys.baseUrl) ?? Self.defaults.baseUrl)
.dropTrailingSlash()

self.title =
dict.string(Keys.title)
?? Self.defaults.title
self.title = dict.string(Keys.title) ?? Self.defaults.title

self.description =
dict.string(Keys.description)
?? Self.defaults.description
dict.string(Keys.description) ?? Self.defaults.description

self.language = dict.string(Keys.language)

self.dateFormat =
dict.string(Keys.dateFormat)
?? Self.defaults.dateFormat
dict.string(Keys.dateFormat) ?? Self.defaults.dateFormat

self.noindex =
dict.bool(Keys.noindex)
?? Self.defaults.noindex
self.noindex = dict.bool(Keys.noindex) ?? Self.defaults.noindex

self.hreflang = dict.array(Keys.hreflang, as: Hreflang.self)
self.userDefined = dict.filter { !Keys.allKeys.contains($0.key) }
Expand All @@ -95,7 +87,7 @@ struct Site {
extension Site {

static let `defaults` = Site(
baseUrl: "http://localhost:3000/",
baseUrl: "http://localhost:3000",
title: "",
description: "",
language: nil,
Expand Down
6 changes: 3 additions & 3 deletions Tests/ToucanSDKTests/Mocks/PageBundle+Mocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extension PageBundle {
static let post1 = PageBundle(
id: "post-1",
url: URL(fileURLWithPath: "/"),
baseUrl: "http://localhost:3000/",
baseUrl: "http://localhost:3000",
slug: "post-1",
permalink: "post-1",
title: "Post 1",
Expand Down Expand Up @@ -60,7 +60,7 @@ extension PageBundle {
static let author1 = PageBundle(
id: "author-1",
url: URL(fileURLWithPath: "/"),
baseUrl: "http://localhost:3000/",
baseUrl: "http://localhost:3000",
slug: "author-1",
permalink: "author-1",
title: "Author 1",
Expand Down Expand Up @@ -103,7 +103,7 @@ extension PageBundle {
static let page1 = PageBundle(
id: "page-1",
url: URL(fileURLWithPath: "/"),
baseUrl: "http://localhost:3000/",
baseUrl: "http://localhost:3000",
slug: "page-1",
permalink: "page-1",
title: "Page 1",
Expand Down

0 comments on commit 4907bdf

Please sign in to comment.