Skip to content

Commit

Permalink
RSS: Add option to specify title suffixes for items (#52)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JohnSundell committed Jan 16, 2020
1 parent 6872300 commit ff50bdc
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
4 changes: 3 additions & 1 deletion Sources/Publish/API/Item.swift
Expand Up @@ -50,7 +50,9 @@ public struct Item<Site: Website>: AnyItem, Hashable {

internal extension Item {
var rssTitle: String {
(rssProperties.titlePrefix ?? "") + title
let prefix = rssProperties.titlePrefix ?? ""
let suffix = rssProperties.titleSuffix ?? ""
return prefix + title + suffix
}
}

Expand Down
10 changes: 7 additions & 3 deletions Sources/Publish/API/ItemRSSProperties.swift
Expand Up @@ -14,12 +14,16 @@ public struct ItemRSSProperties: Codable, Hashable {
public var guid: String?
/// Any prefix that should be added to the item's title within an RSS feed.
public var titlePrefix: String?
/// Any suffix that should be added to the item's title within an RSS feed.
public var titleSuffix: String?

/// Initialize an instance of this type
/// - Parameter guid: Any specific GUID that should be added for the item.
/// - Parameter titlePrefix: Any prefix that should be added to the item's title.
/// - parameter guid: Any specific GUID that should be added for the item.
/// - parameter titlePrefix: Any prefix that should be added to the item's title.
/// - parameter titleSuffix: Any suffix that should be added to the item's title.
public init(guid: String? = nil,
titlePrefix: String? = nil) {
titlePrefix: String? = nil,
titleSuffix: String? = nil) {
self.guid = guid
self.titlePrefix = titlePrefix
}
Expand Down
23 changes: 22 additions & 1 deletion Tests/PublishTests/Tests/PodcastFeedGenerationTests.swift
Expand Up @@ -45,6 +45,25 @@ final class PodcastFeedGenerationTests: PublishTestCase {
""")
}

func testItemPrefixAndSuffix() throws {
let folder = try Folder.createTemporary()

let prefixSuffix = """
rss.titlePrefix: Prefix
rss.titleSuffix: Suffix
"""

try generateFeed(in: folder, content: [
"one/item.md": """
\(makeStubbedAudioMetadata(including: prefixSuffix))
# Title
"""
])

let feed = try folder.file(at: "Output/feed.rss").readAsString()
XCTAssertTrue(feed.contains("<title>PrefixTitleSuffix</title>"))
}

func testReusingPreviousFeedIfNoItemsWereModified() throws {
let folder = try Folder.createTemporary()
let contentFile = try folder.createFile(at: "Content/one/item.md")
Expand Down Expand Up @@ -130,6 +149,7 @@ extension PodcastFeedGenerationTests {
[
("testOnlyIncludingSpecifiedSection", testOnlyIncludingSpecifiedSection),
("testConvertingRelativeLinksToAbsolute", testConvertingRelativeLinksToAbsolute),
("testItemPrefixAndSuffix", testItemPrefixAndSuffix),
("testReusingPreviousFeedIfNoItemsWereModified", testReusingPreviousFeedIfNoItemsWereModified),
("testNotReusingPreviousFeedIfConfigChanged", testNotReusingPreviousFeedIfConfigChanged),
("testNotReusingPreviousFeedIfItemWasAdded", testNotReusingPreviousFeedIfItemWasAdded)
Expand All @@ -156,12 +176,13 @@ private extension PodcastFeedGenerationTests {
)
}

func makeStubbedAudioMetadata() -> String {
func makeStubbedAudioMetadata(including additionalString: String = "") -> String {
"""
---
audio.url: https://audio.mp3
audio.duration: 05:02
audio.size: 12345
\(additionalString)
---
"""
}
Expand Down
18 changes: 18 additions & 0 deletions Tests/PublishTests/Tests/RSSFeedGenerationTests.swift
Expand Up @@ -42,6 +42,23 @@ final class RSSFeedGenerationTests: PublishTestCase {
""")
}

func testItemPrefixAndSuffix() throws {
let folder = try Folder.createTemporary()

try generateFeed(in: folder, content: [
"one/item.md": """
---
rss.titlePrefix: Prefix
rss.titleSuffix: Suffix
---
# Title
"""
])

let feed = try folder.file(at: "Output/feed.rss").readAsString()
XCTAssertTrue(feed.contains("<title>PrefixTitleSuffix</title>"))
}

func testReusingPreviousFeedIfNoItemsWereModified() throws {
let folder = try Folder.createTemporary()
let contentFile = try folder.createFile(at: "Content/one/item.md")
Expand Down Expand Up @@ -103,6 +120,7 @@ extension RSSFeedGenerationTests {
[
("testOnlyIncludingSpecifiedSections", testOnlyIncludingSpecifiedSections),
("testConvertingRelativeLinksToAbsolute", testConvertingRelativeLinksToAbsolute),
("testItemPrefixAndSuffix", testItemPrefixAndSuffix),
("testReusingPreviousFeedIfNoItemsWereModified", testReusingPreviousFeedIfNoItemsWereModified),
("testNotReusingPreviousFeedIfConfigChanged", testNotReusingPreviousFeedIfConfigChanged),
("testNotReusingPreviousFeedIfItemWasAdded", testNotReusingPreviousFeedIfItemWasAdded)
Expand Down

0 comments on commit ff50bdc

Please sign in to comment.