Skip to content

Commit ff50bdc

Browse files
authored
RSS: Add option to specify title suffixes for items (#52)
This change adds a new `titleSuffix` property to `ItemRSSProperties` which, just like `titlePrefix`, enables a string to be attached to an item’s title when it’s being rendered as part of an RSS/podcast feed.
1 parent 6872300 commit ff50bdc

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

Sources/Publish/API/Item.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ public struct Item<Site: Website>: AnyItem, Hashable {
5050

5151
internal extension Item {
5252
var rssTitle: String {
53-
(rssProperties.titlePrefix ?? "") + title
53+
let prefix = rssProperties.titlePrefix ?? ""
54+
let suffix = rssProperties.titleSuffix ?? ""
55+
return prefix + title + suffix
5456
}
5557
}
5658

Sources/Publish/API/ItemRSSProperties.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ public struct ItemRSSProperties: Codable, Hashable {
1414
public var guid: String?
1515
/// Any prefix that should be added to the item's title within an RSS feed.
1616
public var titlePrefix: String?
17+
/// Any suffix that should be added to the item's title within an RSS feed.
18+
public var titleSuffix: String?
1719

1820
/// Initialize an instance of this type
19-
/// - Parameter guid: Any specific GUID that should be added for the item.
20-
/// - Parameter titlePrefix: Any prefix that should be added to the item's title.
21+
/// - parameter guid: Any specific GUID that should be added for the item.
22+
/// - parameter titlePrefix: Any prefix that should be added to the item's title.
23+
/// - parameter titleSuffix: Any suffix that should be added to the item's title.
2124
public init(guid: String? = nil,
22-
titlePrefix: String? = nil) {
25+
titlePrefix: String? = nil,
26+
titleSuffix: String? = nil) {
2327
self.guid = guid
2428
self.titlePrefix = titlePrefix
2529
}

Tests/PublishTests/Tests/PodcastFeedGenerationTests.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ final class PodcastFeedGenerationTests: PublishTestCase {
4545
""")
4646
}
4747

48+
func testItemPrefixAndSuffix() throws {
49+
let folder = try Folder.createTemporary()
50+
51+
let prefixSuffix = """
52+
rss.titlePrefix: Prefix
53+
rss.titleSuffix: Suffix
54+
"""
55+
56+
try generateFeed(in: folder, content: [
57+
"one/item.md": """
58+
\(makeStubbedAudioMetadata(including: prefixSuffix))
59+
# Title
60+
"""
61+
])
62+
63+
let feed = try folder.file(at: "Output/feed.rss").readAsString()
64+
XCTAssertTrue(feed.contains("<title>PrefixTitleSuffix</title>"))
65+
}
66+
4867
func testReusingPreviousFeedIfNoItemsWereModified() throws {
4968
let folder = try Folder.createTemporary()
5069
let contentFile = try folder.createFile(at: "Content/one/item.md")
@@ -130,6 +149,7 @@ extension PodcastFeedGenerationTests {
130149
[
131150
("testOnlyIncludingSpecifiedSection", testOnlyIncludingSpecifiedSection),
132151
("testConvertingRelativeLinksToAbsolute", testConvertingRelativeLinksToAbsolute),
152+
("testItemPrefixAndSuffix", testItemPrefixAndSuffix),
133153
("testReusingPreviousFeedIfNoItemsWereModified", testReusingPreviousFeedIfNoItemsWereModified),
134154
("testNotReusingPreviousFeedIfConfigChanged", testNotReusingPreviousFeedIfConfigChanged),
135155
("testNotReusingPreviousFeedIfItemWasAdded", testNotReusingPreviousFeedIfItemWasAdded)
@@ -156,12 +176,13 @@ private extension PodcastFeedGenerationTests {
156176
)
157177
}
158178

159-
func makeStubbedAudioMetadata() -> String {
179+
func makeStubbedAudioMetadata(including additionalString: String = "") -> String {
160180
"""
161181
---
162182
audio.url: https://audio.mp3
163183
audio.duration: 05:02
164184
audio.size: 12345
185+
\(additionalString)
165186
---
166187
"""
167188
}

Tests/PublishTests/Tests/RSSFeedGenerationTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ final class RSSFeedGenerationTests: PublishTestCase {
4242
""")
4343
}
4444

45+
func testItemPrefixAndSuffix() throws {
46+
let folder = try Folder.createTemporary()
47+
48+
try generateFeed(in: folder, content: [
49+
"one/item.md": """
50+
---
51+
rss.titlePrefix: Prefix
52+
rss.titleSuffix: Suffix
53+
---
54+
# Title
55+
"""
56+
])
57+
58+
let feed = try folder.file(at: "Output/feed.rss").readAsString()
59+
XCTAssertTrue(feed.contains("<title>PrefixTitleSuffix</title>"))
60+
}
61+
4562
func testReusingPreviousFeedIfNoItemsWereModified() throws {
4663
let folder = try Folder.createTemporary()
4764
let contentFile = try folder.createFile(at: "Content/one/item.md")
@@ -103,6 +120,7 @@ extension RSSFeedGenerationTests {
103120
[
104121
("testOnlyIncludingSpecifiedSections", testOnlyIncludingSpecifiedSections),
105122
("testConvertingRelativeLinksToAbsolute", testConvertingRelativeLinksToAbsolute),
123+
("testItemPrefixAndSuffix", testItemPrefixAndSuffix),
106124
("testReusingPreviousFeedIfNoItemsWereModified", testReusingPreviousFeedIfNoItemsWereModified),
107125
("testNotReusingPreviousFeedIfConfigChanged", testNotReusingPreviousFeedIfConfigChanged),
108126
("testNotReusingPreviousFeedIfItemWasAdded", testNotReusingPreviousFeedIfItemWasAdded)

0 commit comments

Comments
 (0)