Skip to content

Commit

Permalink
Add config support to change RSS feed output file (#56)
Browse files Browse the repository at this point in the history
Now it's possible to change `rss.xml` output to a custom name via the
config:

```yml
contents:
  rss:
    output: feed.xml
```
  • Loading branch information
tib authored Dec 12, 2024
1 parent e0625d4 commit d50b7d3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
22 changes: 21 additions & 1 deletion Sources/ToucanSDK/Config/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,31 +107,42 @@ struct Config {
let template: String
}

struct RSS {
enum Keys {
static let output = "output"
}
let output: String
}

enum Keys {
static let dateFormat = "dateFormat"
static let assets = "assets"
static let home = "home"
static let notFound = "notFound"
static let rss = "rss"
}

let folder: String
let dateFormat: String
let assets: Location
let home: Page
let notFound: Page
let rss: RSS

init(
folder: String,
dateFormat: String,
assets: Config.Location,
home: Page,
notFound: Page
notFound: Page,
rss: RSS
) {
self.folder = folder
self.dateFormat = dateFormat
self.assets = assets
self.home = home
self.notFound = notFound
self.rss = rss
}

init(_ dict: [String: Any]) {
Expand Down Expand Up @@ -163,6 +174,12 @@ struct Config {
template: notFound.string(Page.Keys.template)
?? Config.defaults.contents.notFound.template
)

let rss = dict.dict(Keys.rss)
self.rss = .init(
output: rss.string(RSS.Keys.output)
?? Config.defaults.contents.rss.output
)
}
}

Expand Down Expand Up @@ -288,6 +305,9 @@ extension Config {
notFound: .init(
id: "404",
template: "pages.404"
),
rss: .init(
output: "rss.xml"
)
),
transformers: .init(
Expand Down
18 changes: 8 additions & 10 deletions Sources/ToucanSDK/Renderers/RSSRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import Foundation

struct RSSRenderer {

public enum Files {
static let rss = "rss.xml"
}

let site: Site
let sourceConfig: SourceConfig
let destinationUrl: URL
let fileManager: FileManager
let templateRenderer: MustacheToHTMLRenderer
Expand Down Expand Up @@ -42,18 +38,20 @@ struct RSSRenderer {
?? rssDateFormatter.string(from: .init())

let context = RSSContext(
title: site.title,
description: site.description,
baseUrl: site.baseUrl,
language: site.language,
title: sourceConfig.site.title,
description: sourceConfig.site.description,
baseUrl: sourceConfig.site.baseUrl,
language: sourceConfig.site.language,
lastBuildDate: rssDateFormatter.string(from: .init()),
publicationDate: publicationDate,
items: items
)
try templateRenderer.render(
template: "rss",
with: context,
to: destinationUrl.appendingPathComponent(Files.rss)
to:
destinationUrl
.appendingPathComponent(sourceConfig.config.contents.rss.output)
)
}
}
2 changes: 1 addition & 1 deletion Sources/ToucanSDK/Toucan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public struct Toucan {
try sitemapRenderer.render()

let rssRenderer = RSSRenderer(
site: source.sourceConfig.site,
sourceConfig: source.sourceConfig,
destinationUrl: workDirUrl,
fileManager: .default,
templateRenderer: templateRenderer,
Expand Down

0 comments on commit d50b7d3

Please sign in to comment.