Skip to content

Commit 2a8ee7d

Browse files
committed
fix: add convenience initialiser for extending metadata from a previous object
1 parent 7641550 commit 2a8ee7d

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

Sources/WebUI/Core/Metadata.swift

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ public struct Metadata {
7373
self.site = site
7474
self.title = title
7575
self.titleSeperator = titleSeperator
76-
self.pageTitle = "\(title ?? "")\(site.map { " \(titleSeperator) \($0)" } ?? "")"
76+
self.pageTitle =
77+
"\(title ?? "")\(site.map { " \(titleSeperator) \($0)" } ?? "")"
7778
self.description = description
7879
self.date = date
7980
self.image = image
@@ -85,3 +86,52 @@ public struct Metadata {
8586
self.themeColor = themeColor
8687
}
8788
}
89+
90+
extension Metadata {
91+
/// Initializes a new `Metadata` instance based on an existing one, with optional modifications to any property.
92+
///
93+
/// - Parameters:
94+
/// - base: The original `Metadata` instance.
95+
/// - site: Optional site value, default is `nil`.
96+
/// - title: Optional title value, default is `nil`.
97+
/// - titleSeperator: Optional title separator, default is the same as `base`.
98+
/// - description: Optional description value, default is the same as `base`.
99+
/// - date: Optional date value, default is the same as `base`.
100+
/// - image: Optional image value, default is the same as `base`.
101+
/// - author: Optional author value, default is the same as `base`.
102+
/// - keywords: Optional keywords value, default is the same as `base`.
103+
/// - twitter: Optional twitter handle, default is the same as `base`.
104+
/// - locale: Optional locale value, default is the same as `base`.
105+
/// - type: Optional content type, default is the same as `base`.
106+
/// - themeColor: Optional theme color, default is the same as `base`.
107+
public init(
108+
base: Metadata,
109+
site: String? = nil,
110+
title: String? = nil,
111+
titleSeperator: String? = nil,
112+
description: String? = nil,
113+
date: Date? = nil,
114+
image: String? = nil,
115+
author: String? = nil,
116+
keywords: [String]? = nil,
117+
twitter: String? = nil,
118+
locale: Locale? = nil,
119+
type: ContentType? = nil,
120+
themeColor: ThemeColor? = nil
121+
) {
122+
self.site = site ?? base.site
123+
self.title = title ?? base.title
124+
self.titleSeperator = titleSeperator ?? base.titleSeperator
125+
self.pageTitle =
126+
"\(title ?? "")\(site.map { " \(titleSeperator ?? "|") \($0)" } ?? "")"
127+
self.description = description ?? base.description
128+
self.date = date ?? base.date
129+
self.image = image ?? base.image
130+
self.author = author ?? base.author
131+
self.keywords = keywords ?? base.keywords
132+
self.twitter = twitter ?? base.twitter
133+
self.locale = locale ?? base.locale
134+
self.type = type ?? base.type
135+
self.themeColor = themeColor ?? base.themeColor
136+
}
137+
}

0 commit comments

Comments
 (0)