The post-gate main surface is the workspace shell from the design
contract: nav rail (Posts/Sessions/Learnings/Settings), Posts pane
mirroring <home>/posts/ on disk (Pass State groups, origin meta,
empty state; lazy folders skipped), Settings pane with repoint-only
Home picking (NSOpenPanel + UserDefaults, default ~/Documents/Museion
per b3daac9), placeholder panes until #28/#26. Column collapse
mirrors the export's media queries so narrow windows never clip.
Mentor pane becomes the fourth column (#14 surface preserved).
[Generated by AI]
115 lines
4.2 KiB
Swift
115 lines
4.2 KiB
Swift
//
|
|
// PostLibrary.swift
|
|
// Museion
|
|
//
|
|
// #17 (S1.1): the Posts list mirrors what is on disk under the Museion Home.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
/// One Post on disk: `<home>/posts/<id>/post.md` (ADR-0004 shape).
|
|
/// A Post directory with no post.md yet is skipped — folders are created lazily
|
|
/// on first write, so an empty folder is not a Post.
|
|
nonisolated struct Post: Identifiable, Equatable {
|
|
/// The post folder's name — the Post's durable identity (CONTEXT.md).
|
|
let id: String
|
|
/// The Post's Title: first non-empty line, heading markers stripped.
|
|
var title: String
|
|
/// Pass State from frontmatter; First Draft when absent (explicit, never inferred).
|
|
var passState: PassState
|
|
/// Origin from frontmatter; writer when absent. Immutable once set (CONTEXT.md).
|
|
var origin: Origin
|
|
|
|
nonisolated enum PassState: String, Equatable, CaseIterable {
|
|
case firstDraft = "First Draft"
|
|
case pass1Done = "Pass-1 Done"
|
|
case pass2Done = "Pass-2 Done"
|
|
}
|
|
|
|
nonisolated enum Origin: String, Equatable {
|
|
case writer
|
|
/// PRD S5.1 pins the frontmatter vocabulary: `origin: writer|ai`.
|
|
// swiftlint:disable:next identifier_name
|
|
case ai
|
|
}
|
|
}
|
|
|
|
/// Scans `<home>/posts/` and parses each Post's frontmatter leniently.
|
|
/// Read-only for #17 — create/rename/delete round-trips are #18.
|
|
/// Nonisolated: a pure synchronous disk scan, safe from any executor.
|
|
nonisolated struct PostLibrary: Equatable {
|
|
private(set) var posts: [Post] = []
|
|
|
|
/// Reads every `posts/<id>/post.md`. Frontmatter keys (`pass_state:`, `origin:`)
|
|
/// are read leniently — corpus posts ship bare, so absent or unknown values fall
|
|
/// back to First Draft / writer. Keys outside a frontmatter block are ignored;
|
|
/// a malformed folder can't crash the list.
|
|
static func scan(home: URL) -> PostLibrary {
|
|
let postsURL = home.appendingPathComponent("posts", isDirectory: true)
|
|
guard let entries = try? FileManager.default.contentsOfDirectory(
|
|
at: postsURL,
|
|
includingPropertiesForKeys: [.isDirectoryKey],
|
|
) else { return PostLibrary() }
|
|
|
|
var posts: [Post] = []
|
|
for dir in entries where (try? dir.resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true {
|
|
let fileURL = dir.appendingPathComponent("post.md")
|
|
guard let text = try? String(contentsOf: fileURL, encoding: .utf8) else { continue }
|
|
let frontmatter = Self.frontmatter(of: text)
|
|
posts.append(
|
|
Post(
|
|
id: dir.lastPathComponent,
|
|
title: Self.title(of: text),
|
|
passState: Post.PassState(rawValue: frontmatter["pass_state"] ?? "") ?? .firstDraft,
|
|
origin: Post.Origin(rawValue: frontmatter["origin"] ?? "") ?? .writer,
|
|
),
|
|
)
|
|
}
|
|
return PostLibrary(posts: posts.sorted { $0.title.localizedStandardCompare($1.title) == .orderedAscending })
|
|
}
|
|
|
|
/// The `---`-delimited frontmatter block if present; empty otherwise.
|
|
static func frontmatter(of text: String) -> [String: String] {
|
|
let lines = text.split(separator: "\n", omittingEmptySubsequences: false)
|
|
guard lines.first?.trimmed() == "---" else { return [:] }
|
|
var map: [String: String] = [:]
|
|
for line in lines.dropFirst() {
|
|
let trimmed = line.trimmed()
|
|
guard trimmed != "---" else { break }
|
|
guard let colon = trimmed.firstIndex(of: ":") else { continue }
|
|
let key = trimmed[..<colon].trimmingCharacters(in: .whitespaces)
|
|
let value = trimmed[trimmed.index(after: colon)...].trimmingCharacters(in: .whitespaces)
|
|
map[key] = value
|
|
}
|
|
return map
|
|
}
|
|
|
|
/// The Post's Title: first non-empty line after any frontmatter block,
|
|
/// `#` heading markers stripped. A body with no text at all reads as
|
|
/// `untitled` (the blank-draft id).
|
|
static func title(of text: String) -> String {
|
|
var lines = text.split(separator: "\n", omittingEmptySubsequences: false)[...]
|
|
if lines.first?.trimmed() == "---" {
|
|
if let close = lines.dropFirst().firstIndex(where: { $0.trimmed() == "---" }) {
|
|
lines = lines[(close + 1)...]
|
|
} else {
|
|
lines = lines.dropFirst() // unterminated block: no title inside it
|
|
}
|
|
}
|
|
for line in lines {
|
|
let trimmed = line.trimmed()
|
|
if trimmed.isEmpty {
|
|
continue
|
|
}
|
|
return trimmed.trimmingCharacters(in: CharacterSet(charactersIn: "#").union(.whitespaces))
|
|
}
|
|
return "untitled"
|
|
}
|
|
}
|
|
|
|
private nonisolated extension Substring {
|
|
func trimmed() -> String {
|
|
trimmingCharacters(in: .whitespacesAndNewlines)
|
|
}
|
|
}
|