feat: Working on rendering a search index / json file.

This commit is contained in:
2025-04-06 00:15:04 -04:00
parent 7a6e4d17ac
commit 6457674de7
2 changed files with 68 additions and 0 deletions

View File

@@ -63,6 +63,60 @@ func renderYear<T>(context: PartitionedRenderingContext<T, ArticleMetadata>) ->
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(
_ articles: [Item<ArticleMetadata>],
canocicalURL: String,