feat: Working on rendering a search index / json file.
This commit is contained in:
@@ -35,6 +35,7 @@ struct Run {
|
|||||||
writers: [
|
writers: [
|
||||||
.itemWriter(swim(renderArticle)),
|
.itemWriter(swim(renderArticle)),
|
||||||
.listWriter(swim(renderArticles)),
|
.listWriter(swim(renderArticles)),
|
||||||
|
.listWriter(renderJson, output: "../static/search.json"),
|
||||||
.tagWriter(swim(renderTag), tags: \.metadata.tags),
|
.tagWriter(swim(renderTag), tags: \.metadata.tags),
|
||||||
.yearWriter(swim(renderYear)),
|
.yearWriter(swim(renderYear)),
|
||||||
// Atom feed for all articles, and a feed per tag
|
// Atom feed for all articles, and a feed per tag
|
||||||
@@ -72,5 +73,18 @@ struct Run {
|
|||||||
// All the remaining files that were not parsed to markdown, so for example images, raw html files and css,
|
// All the remaining files that were not parsed to markdown, so for example images, raw html files and css,
|
||||||
// are copied as-is to the output folder.
|
// are copied as-is to the output folder.
|
||||||
.staticFiles()
|
.staticFiles()
|
||||||
|
|
||||||
|
// Run saga again on articles, to collect search index.
|
||||||
|
// try await Saga(input: "content", output: "deploy")
|
||||||
|
// .register(
|
||||||
|
// folder: "articles",
|
||||||
|
// metadata: ArticleMetadata.self,
|
||||||
|
// readers: [.plainReader],
|
||||||
|
// filter: \.public,
|
||||||
|
// writers: [
|
||||||
|
// .listWriter(renderJson, output: "../search.json")
|
||||||
|
// ]
|
||||||
|
// )
|
||||||
|
// .run()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,6 +63,60 @@ func renderYear<T>(context: PartitionedRenderingContext<T, ArticleMetadata>) ->
|
|||||||
baseRenderArticles(context.items, canocicalURL: "/articles/\(context.key)/", title: "Articles in \(context.key)")
|
baseRenderArticles(context.items, canocicalURL: "/articles/\(context.key)/", title: "Articles in \(context.key)")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private struct SearchData: Encodable, Identifiable {
|
||||||
|
let id: String
|
||||||
|
let title: String
|
||||||
|
let content: String
|
||||||
|
|
||||||
|
init(article: Item<ArticleMetadata>) throws {
|
||||||
|
self.id = article.url
|
||||||
|
self.title = article.title
|
||||||
|
let rawContent: String = try article.absoluteSource.read()
|
||||||
|
self.content = Self.parse(rawContent)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Grabs the metadata (wrapped within `---`), the first title, and the body of the document.
|
||||||
|
static func parts(from content: String) -> (String?, String?, String) {
|
||||||
|
let scanner = Scanner(string: content)
|
||||||
|
|
||||||
|
var header: String? = nil
|
||||||
|
var title: String? = nil
|
||||||
|
|
||||||
|
if scanner.scanString("---") == "---" {
|
||||||
|
header = scanner.scanUpToString("---")
|
||||||
|
_ = scanner.scanString("---")
|
||||||
|
}
|
||||||
|
|
||||||
|
if scanner.scanString("# ") == "# " {
|
||||||
|
title = scanner.scanUpToString("\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
let body = String(scanner.string[scanner.currentIndex...])
|
||||||
|
|
||||||
|
return (header, title, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
static func parse(_ content: String) -> String {
|
||||||
|
let (_, _, body) = parts(from: content)
|
||||||
|
return body
|
||||||
|
.replacingOccurrences(of: "\n", with: " ")
|
||||||
|
.replacingOccurrences(of: "#", with: "")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func renderJson(_ articles: ItemsRenderingContext<ArticleMetadata>) throws -> String {
|
||||||
|
print(articles.items.count)
|
||||||
|
print(articles.items)
|
||||||
|
let data = try jsonEncoder.encode(articles.items.map(SearchData.init(article:)))
|
||||||
|
return String(data: data, encoding: .utf8)!
|
||||||
|
}
|
||||||
|
|
||||||
|
private let jsonEncoder: JSONEncoder = {
|
||||||
|
let encoder = JSONEncoder()
|
||||||
|
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
|
||||||
|
return encoder
|
||||||
|
}()
|
||||||
|
|
||||||
private func baseRenderArticles(
|
private func baseRenderArticles(
|
||||||
_ articles: [Item<ArticleMetadata>],
|
_ articles: [Item<ArticleMetadata>],
|
||||||
canocicalURL: String,
|
canocicalURL: String,
|
||||||
|
|||||||
Reference in New Issue
Block a user