// // PostLibrary.swift // Museion // // #17 (S1.1): the Posts list mirrors what is on disk under the Museion Home. // import Foundation /// One Post on disk: `/posts//post.md` (ADR-0004 shape). 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 after any frontmatter, 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 } /// Reads every `posts//post.md` into Posts (sorted by title, Finder order). /// Read-only for #17 — create/rename/delete round-trips are #18. Symlinks and /// hidden dirs are skipped; folders without `post.md` (lazy) aren't Posts; /// a malformed folder can't crash the list. static func scan(home: URL) -> [Post] { let postsURL = home.appendingPathComponent("posts", isDirectory: true) guard let entries = try? FileManager.default.contentsOfDirectory( at: postsURL, includingPropertiesForKeys: [.isDirectoryKey, .isSymbolicLinkKey], ) else { return [] } var posts: [Post] = [] for dir in entries { let values = try? dir.resourceValues(forKeys: [.isDirectoryKey, .isSymbolicLinkKey]) guard values?.isDirectory ?? false, !(values?.isSymbolicLink ?? false), !dir.lastPathComponent.hasPrefix(".") else { continue } let fileURL = dir.appendingPathComponent("post.md") guard let text = try? String(contentsOf: fileURL, encoding: .utf8) else { continue } let frontmatter = frontmatter(of: text) posts.append( Post( id: dir.lastPathComponent, title: title(of: text), passState: PassState(rawValue: frontmatter["pass_state"] ?? "") ?? .firstDraft, origin: Origin(rawValue: frontmatter["origin"] ?? "") ?? .writer, ), ) } return posts.sorted { $0.title.localizedStandardCompare($1.title) == .orderedAscending } } /// The `---`-delimited frontmatter block if present; empty otherwise. /// Quoted scalar values (`origin: "ai"`) are unquoted — lenient YAML. static func frontmatter(of text: String) -> [String: String] { let lines = text.split(separator: "\n", omittingEmptySubsequences: false) guard lines.first(where: { !$0.trimmed().isEmpty })?.trimmed() == "---" else { return [:] } var map: [String: String] = [:] for line in lines.drop(while: { $0.trimmed() != "---" }).dropFirst() { let trimmed = line.trimmed() guard trimmed != "---" else { break } guard let colon = trimmed.firstIndex(of: ":") else { continue } let key = trimmed[.. String { var lines = text.split(separator: "\n", omittingEmptySubsequences: false)[...] if let open = lines.firstIndex(where: { !$0.trimmed().isEmpty }), lines[open].trimmed() == "---" { if let close = lines[(open + 1)...].firstIndex(where: { $0.trimmed() == "---" }) { lines = lines[(close + 1)...] } else { return "untitled" // unterminated block: nothing trustworthy follows } } for line in lines { let trimmed = line.trimmed() guard !trimmed.isEmpty else { continue } let stripped = trimmed.trimmingCharacters(in: CharacterSet(charactersIn: "#").union(.whitespaces)) return stripped.isEmpty ? "untitled" : stripped } return "untitled" } } private nonisolated extension Substring { func trimmed() -> String { trimmingCharacters(in: .whitespacesAndNewlines) } }