// Spike C (#54): SwiftUI block editor (live preview, mode C) — THROWAWAY, answers a question, not product code. // Question: can a block editor reach live-preview mode C without becoming the project? // Run: spike/blocks/run.sh (from repo root, branch spike/editor-presentation) import Foundation struct Block: Identifiable, Equatable { var id = UUID() var raw: String // markdown source incl. marker; kind is derived, never stored } nonisolated enum BlockModel { enum Kind { case heading1, heading2, paragraph, quote, hr } /// Blank-line split — identical strategy to Spike B's readingBlocks, so the /// spikes differ by substrate, not by parse. ponytail: no AST, no lists/setext (corpus has none, #51). static func parse(_ md: String) -> [Block] { md.components(separatedBy: "\n\n") .map { $0.trimmingCharacters(in: .whitespacesAndNewlines) } .filter { !$0.isEmpty } .map { Block(raw: $0) } } static func kind(of raw: String) -> Kind { if raw.hasPrefix("## ") { return .heading2 } if raw.hasPrefix("# ") { return .heading1 } if raw.hasPrefix("> ") { return .quote } if raw.count <= 6, !raw.isEmpty, raw.allSatisfy({ $0 == "*" || $0 == "-" || $0 == "_" || $0.isWhitespace }) { return .hr } return .paragraph } /// Rendered text: marker stripped, inline markdown parsed (inline-only — block /// structure is ours, exactly like Spike B's reading mode). static func rendered(_ raw: String) -> AttributedString { var p = raw let k = kind(of: raw) switch k { case .heading1: p = String(p.dropFirst(2)) case .heading2: p = String(p.dropFirst(3)) case .quote: p = String(p.dropFirst(2)) case .hr: return AttributedString("⁂") case .paragraph: break } var opts = AttributedString.MarkdownParsingOptions() opts.interpretedSyntax = .inlineOnlyPreservingWhitespace return (try? AttributedString(markdown: p, options: opts)) ?? AttributedString(p) } /// Marker-stripped plain text (anchor search runs over this). static func plainText(_ raw: String) -> String { switch kind(of: raw) { case .heading1: String(raw.dropFirst(2)) case .heading2: String(raw.dropFirst(3)) case .quote: String(raw.dropFirst(2)) case .hr: "" case .paragraph: raw } } /// Within-block UTF-16 range of `quote` (word-tokenized, whitespace-tolerant — same /// strategy as Spike B's anchorRange, scoped to one block). nil = stale. static func anchorRange(of quote: String, in raw: String) -> NSRange? { let text = plainText(raw) let textWords = words(text), quoteWords = words(quote) guard !quoteWords.isEmpty, textWords.count >= quoteWords.count else { return nil } for start in 0 ... textWords.count - quoteWords.count { if (start ..< start + quoteWords.count).allSatisfy({ textWords[$0].norm == quoteWords[$0 - start].norm }) { let from = textWords[start].range.lowerBound let to = textWords[start + quoteWords.count - 1].range.upperBound return NSRange(location: text.utf16.distance(from: text.startIndex, to: from), length: text.utf16.distance(from: from, to: to)) } } return nil } private struct Word { let norm: String; let range: Range } private static func words(_ s: String) -> [Word] { s.split(whereSeparator: \.isWhitespace).map { sub in Word(norm: sub.lowercased().trimmingCharacters(in: .punctuationCharacters), range: sub.startIndex ..< sub.endIndex) } } }