mirror of
https://github.com/samsonjs/samhuri.net.git
synced 2026-04-27 14:57:40 +00:00
Render recent posts at /index.html instead of /posts/index.html
This commit is contained in:
parent
085949bd87
commit
38918fe5f9
1 changed files with 14 additions and 12 deletions
|
|
@ -11,13 +11,15 @@ import Ink
|
||||||
final class PostsPlugin: Plugin {
|
final class PostsPlugin: Plugin {
|
||||||
let fileManager: FileManager = .default
|
let fileManager: FileManager = .default
|
||||||
let markdownParser = MarkdownParser()
|
let markdownParser = MarkdownParser()
|
||||||
let path: String
|
let postsPath: String
|
||||||
|
let recentPostsPath: String
|
||||||
|
|
||||||
var posts: PostsByYear!
|
var posts: PostsByYear!
|
||||||
var sourceURL: URL!
|
var sourceURL: URL!
|
||||||
|
|
||||||
init(path: String = "posts") {
|
init(postsPath: String = "posts", recentPostsPath: String = "index.html") {
|
||||||
self.path = path
|
self.postsPath = postsPath
|
||||||
|
self.recentPostsPath = recentPostsPath
|
||||||
}
|
}
|
||||||
|
|
||||||
func setUp(sourceURL: URL) throws {
|
func setUp(sourceURL: URL) throws {
|
||||||
|
|
@ -47,19 +49,19 @@ final class PostsPlugin: Plugin {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let postsDir = targetURL.appendingPathComponent(path)
|
let recentPostsURL = targetURL.appendingPathComponent(recentPostsPath)
|
||||||
try renderRecentPosts(postsDir: postsDir, templateRenderer: templateRenderer)
|
try renderRecentPosts(targetFileURL: recentPostsURL, templateRenderer: templateRenderer)
|
||||||
|
|
||||||
|
let postsDir = targetURL.appendingPathComponent(postsPath)
|
||||||
try renderYearsAndMonths(postsDir: postsDir, templateRenderer: templateRenderer)
|
try renderYearsAndMonths(postsDir: postsDir, templateRenderer: templateRenderer)
|
||||||
try renderPostsByDate(postsDir: postsDir, templateRenderer: templateRenderer)
|
try renderPostsByDate(postsDir: postsDir, templateRenderer: templateRenderer)
|
||||||
}
|
}
|
||||||
|
|
||||||
func renderRecentPosts(postsDir: URL, templateRenderer: TemplateRenderer) throws {
|
func renderRecentPosts(targetFileURL: URL, templateRenderer: TemplateRenderer) throws {
|
||||||
print("renderRecentPosts(postsDir: \(postsDir), templateRenderer: \(templateRenderer)")
|
print("renderRecentPosts(targetFileURL: \(targetFileURL), templateRenderer: \(templateRenderer)")
|
||||||
let recentPosts = posts.flattened().prefix(10)
|
let recentPosts = posts.flattened().prefix(10)
|
||||||
try fileManager.createDirectory(at: postsDir, withIntermediateDirectories: true, attributes: nil)
|
|
||||||
let recentPostsURL = postsDir.appendingPathComponent("index.html")
|
|
||||||
let recentPostsHTML = try templateRenderer.renderTemplate(name: "recent-posts", context: ["recentPosts": recentPosts])
|
let recentPostsHTML = try templateRenderer.renderTemplate(name: "recent-posts", context: ["recentPosts": recentPosts])
|
||||||
try recentPostsHTML.write(to: recentPostsURL, atomically: true, encoding: .utf8)
|
try recentPostsHTML.write(to: targetFileURL, atomically: true, encoding: .utf8)
|
||||||
}
|
}
|
||||||
|
|
||||||
func renderPostsByDate(postsDir: URL, templateRenderer: TemplateRenderer) throws {
|
func renderPostsByDate(postsDir: URL, templateRenderer: TemplateRenderer) throws {
|
||||||
|
|
@ -92,7 +94,7 @@ final class PostsPlugin: Plugin {
|
||||||
|
|
||||||
let monthDir = yearDir.appendingPathComponent(month.padded)
|
let monthDir = yearDir.appendingPathComponent(month.padded)
|
||||||
try fileManager.createDirectory(at: monthDir, withIntermediateDirectories: true, attributes: nil)
|
try fileManager.createDirectory(at: monthDir, withIntermediateDirectories: true, attributes: nil)
|
||||||
let context: [String: Any] = ["path": path, "month": month, "posts": renderedPosts]
|
let context: [String: Any] = ["path": postsPath, "month": month, "posts": renderedPosts]
|
||||||
let monthHTML = try templateRenderer.renderTemplate(name: "posts-month", context: context)
|
let monthHTML = try templateRenderer.renderTemplate(name: "posts-month", context: context)
|
||||||
let monthURL = monthDir.appendingPathComponent("index.html")
|
let monthURL = monthDir.appendingPathComponent("index.html")
|
||||||
try monthHTML.write(to: monthURL, atomically: true, encoding: .utf8)
|
try monthHTML.write(to: monthURL, atomically: true, encoding: .utf8)
|
||||||
|
|
@ -106,7 +108,7 @@ final class PostsPlugin: Plugin {
|
||||||
}
|
}
|
||||||
let monthsPadded = months.map { $0.padded }
|
let monthsPadded = months.map { $0.padded }
|
||||||
let context: [String: Any] = [
|
let context: [String: Any] = [
|
||||||
"path": path,
|
"path": postsPath,
|
||||||
"year": year,
|
"year": year,
|
||||||
"months": monthsPadded,
|
"months": monthsPadded,
|
||||||
"monthNames": months.reduce(into: [String: String](), { dict, month in
|
"monthNames": months.reduce(into: [String: String](), { dict, month in
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue