feat(microblog): set and create categories
A piece's `category:` frontmatter line now travels to Micro.blog: a first send carries the list beside title and content, and a re-send replaces the post's categories with exactly the piece's list — only when the piece has the line (opt-in by key; `[]` clears; no line leaves the blog's). Naming a category Micro.blog doesn't know creates it there, so there is no separate create step. The publish sheet gains a Categories field: a comma list with the system completion menu over the blog's own categories. The system replaces the whole field text with a completion, so the completion is the merge — a pick adds, a half-typed last word is replaced. The edit lands on the piece at the send (a failed write refuses with nothing on the wire); Publish with a pending edit applies it first and flips the post that send touched. The one-line codec lives in PostFrontmatter (line-bounded; backslashes and quotes escaped; a Set de-duplicates). Import keeps an imported `category:` line — the writer's classification, unlike the micropub bookkeeping keys. Reviewed twice (two-axis during the build, finish-audit after): the stale pending baseline after a failed step, the backslash round-trip, and the publish targeting the caller's possibly-stale URL were found and fixed before this commit. 39 of the 40 new or changed pins were watched fail on their own guard. Rail 791/791; SwiftFormat and SwiftLint clean. Docs: AGENTS.md bullets and learnings, the GLOSSARY term, CHANGELOG [Unreleased], PRD S6.1, the devlog, and the swift wiki (text completion is a whole-text contract). Closes #207
This commit is contained in:
1 parent
6aa31a419a
commit
6db4cf8cf1
18 files changed
+1629
-67
No files matched your search
@@ -15,10 +15,99 @@
|
||||
// piece says so (and links its live post) and offers Update Live
|
||||
// Post; a draft offers Publish / Update Draft. Both re-sends update
|
||||
// the remembered post IN PLACE — never a second post.
|
||||
// #207: a Categories field — the piece's `category:` line as a
|
||||
// comma list, edited with the system completion menu over the
|
||||
// blog's own categories (`Micropub.blogCategories`, fetched on
|
||||
// open; a failed fetch is silent). The system REPLACES the whole
|
||||
// field text with a completion, so `MicropubCategoryField` (the
|
||||
// enum above the view) carries the merge: a pick ADDS, a
|
||||
// half-typed last word is replaced. The edit lands on disk AT THE
|
||||
// SEND (the model's `categories:`), never before — Cancel/Done
|
||||
// discard it and a refused send puts nothing on the wire; the field
|
||||
// syncs from disk on open and after a forget, settles to the
|
||||
// canonical text after a successful send, and after a failed step
|
||||
// only its baseline re-syncs (the send writes the line before the
|
||||
// wire — a later clear must still count as an edit).
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
/// #207: the publish sheet's Categories field — its pure rules over
|
||||
/// the one-line category codec (`PostFrontmatter.categoryList` /
|
||||
/// `categoryListValue`: the ONE quote-aware comma-list parser — the
|
||||
/// field holds exactly the bracket-inner text). The system completion
|
||||
/// menu REPLACES the whole field text with a completion, so a
|
||||
/// completion IS the merge: a pick ADDS, a half-typed last word is
|
||||
/// replaced, a present name is never offered.
|
||||
nonisolated enum MicropubCategoryField {
|
||||
/// The word being typed — the LAST comma segment (a comma inside
|
||||
/// double quotes never splits), trimmed, one pair of quotes off
|
||||
/// (the codec's own step: the field reads `Tech, "Tech, general"`
|
||||
/// after that pick, and the word is the name). SwiftUI exposes no
|
||||
/// caret, so the end of the text is the only place a writer can be
|
||||
/// typing. "" when the text is empty or ends with a comma.
|
||||
static func partial(_ text: String) -> String {
|
||||
// The codec's own segment scanner splits (ONE quote-aware walk);
|
||||
// its `categoryList` trims, unquotes and unescapes the segment.
|
||||
PostFrontmatter.categoryList(PostFrontmatter.categorySegments(text).last ?? "").first ?? ""
|
||||
}
|
||||
|
||||
/// The last thing typed is itself a known category — nothing is
|
||||
/// half-typed.
|
||||
static func finished(_ text: String, blog: [String]) -> Bool {
|
||||
let word = partial(text)
|
||||
return !word.isEmpty && blog.contains { same($0, word) }
|
||||
}
|
||||
|
||||
/// The menu over the blog's categories, blog order kept: ALL of
|
||||
/// them when nothing or a finished name is typed, else the names
|
||||
/// starting with the half-typed word (case-insensitive); a name
|
||||
/// already in the field is never offered.
|
||||
static func suggestions(text: String, blog: [String]) -> [String] {
|
||||
let word = partial(text)
|
||||
let present = Set(PostFrontmatter.categoryList(text).map { $0.lowercased() })
|
||||
let all = word.isEmpty || finished(text, blog: blog)
|
||||
return blog.filter { name in
|
||||
!present.contains(name.lowercased()) && (all || name.lowercased().hasPrefix(word.lowercased()))
|
||||
}
|
||||
}
|
||||
|
||||
/// What the WHOLE field becomes when `name` is picked: a
|
||||
/// half-typed last word makes room (the pick extends it, and the
|
||||
/// word is not itself a category), then `name` is appended unless
|
||||
/// present — never a duplicate.
|
||||
static func completion(text: String, picking name: String, blog: [String]) -> String {
|
||||
var names = PostFrontmatter.categoryList(text)
|
||||
let word = partial(text)
|
||||
let halfTyped = !word.isEmpty && !finished(text, blog: blog) && same(names.last ?? "", word)
|
||||
if halfTyped, !same(word, name), name.lowercased().hasPrefix(word.lowercased()) {
|
||||
names.removeLast()
|
||||
}
|
||||
if !names.contains(where: { same($0, name) }) {
|
||||
names.append(name)
|
||||
}
|
||||
return PostFrontmatter.categoryListValue(names)
|
||||
}
|
||||
|
||||
/// The sheet's edit for the model: nil when the field's list IS the
|
||||
/// settled one — LISTS, so a trailing comma is not an edit, and
|
||||
/// equal empties stay nil (a piece without a line never gains an
|
||||
/// empty key); else the field's list (`[]` = cleared — the model
|
||||
/// writes `category: []`).
|
||||
static func pendingCategories(field: String, settled: String) -> [String]? {
|
||||
let list = PostFrontmatter.categoryList(field)
|
||||
return list == PostFrontmatter.categoryList(settled) ? nil : list
|
||||
}
|
||||
|
||||
/// ponytail: case-insensitive on purpose — the writer's spelling stays
|
||||
/// in the field, the menu just never re-offers a name that's there in
|
||||
/// any case; the codec's own dedup stays exact (it records what was
|
||||
/// written, it never rewrites a spelling).
|
||||
private static func same(_ a: String, _ b: String) -> Bool {
|
||||
a.caseInsensitiveCompare(b) == .orderedSame
|
||||
}
|
||||
}
|
||||
|
||||
/// #33 (S6.1): the two-step Micro.blog ask over the open piece.
|
||||
struct MicropubPublishSheet: View {
|
||||
let posts: PostsModel
|
||||
@@ -49,6 +138,15 @@ struct MicropubPublishSheet: View {
|
||||
@State private var refusal: String?
|
||||
@State private var tokenField = ""
|
||||
@State private var reentry = false
|
||||
/// #207: the Categories field as typed, the text it rests on (the
|
||||
/// disk read on open, or a successful send's canonical form — the
|
||||
/// pending-edit baseline), and the blog's categories the menu offers.
|
||||
@State private var categoryField = ""
|
||||
@State private var settledCategoryField = ""
|
||||
@State private var blogCategories: [String] = []
|
||||
/// Whether the blog's categories were ever fetched in this sheet
|
||||
/// (a blog with none is not re-asked after every send).
|
||||
@State private var suggestionsLoaded = false
|
||||
|
||||
/// Token entry shows when none is saved — or again after an auth
|
||||
/// failure (the entered-once token can be replaced, never a dead end).
|
||||
@@ -60,6 +158,11 @@ struct MicropubPublishSheet: View {
|
||||
phase == .drafting || phase == .updating || phase == .publishing
|
||||
}
|
||||
|
||||
/// #207: the field's edit for the send — nil when nothing changed.
|
||||
private var pendingCategories: [String]? {
|
||||
MicropubCategoryField.pendingCategories(field: categoryField, settled: settledCategoryField)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 12) {
|
||||
Text(publishedAt == nil ? "Publish to Micro.blog?" : "On Micro.blog")
|
||||
@@ -73,6 +176,26 @@ struct MicropubPublishSheet: View {
|
||||
.foregroundStyle(DesignTokens.muted)
|
||||
}
|
||||
|
||||
// #207: the Categories field, in every phase — a send is possible in all of them.
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
Text("Categories")
|
||||
.font(.system(size: 11, design: DesignTokens.mono))
|
||||
.foregroundStyle(DesignTokens.muted)
|
||||
TextField("Tech, Notes", text: $categoryField)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
.font(.system(size: 12, design: DesignTokens.mono))
|
||||
.disabled(busy)
|
||||
.accessibilityLabel("Categories")
|
||||
.textInputSuggestions {
|
||||
ForEach(MicropubCategoryField.suggestions(text: categoryField, blog: blogCategories), id: \.self) { name in
|
||||
Text(name).textInputCompletion(MicropubCategoryField.completion(text: categoryField, picking: name, blog: blogCategories))
|
||||
}
|
||||
}
|
||||
Text("Comma-separated. A name Micro.blog doesn't know is created when the post is sent.")
|
||||
.font(.system(size: 11, design: DesignTokens.mono))
|
||||
.foregroundStyle(DesignTokens.muted)
|
||||
}
|
||||
|
||||
if showsTokenField {
|
||||
SecureField("App token", text: $tokenField)
|
||||
.textFieldStyle(.roundedBorder)
|
||||
@@ -152,6 +275,8 @@ struct MicropubPublishSheet: View {
|
||||
postURL = post.micropubURL
|
||||
publishedAt = post.micropubPublished
|
||||
phase = settledPhase
|
||||
syncFieldFromDisk()
|
||||
loadSuggestions()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,13 +347,15 @@ struct MicropubPublishSheet: View {
|
||||
|
||||
/// A failed step: the message shows inline with the buttons kept, and
|
||||
/// the sheet rests on the piece's record (a rejected token re-opens
|
||||
/// entry — the entered-once token may be wrong).
|
||||
/// entry — the entered-once token may be wrong). #207: the category
|
||||
/// baseline follows the disk (the field keeps the text as typed).
|
||||
private func fail(_ message: String, tokenRejected: Bool = false) {
|
||||
if tokenRejected {
|
||||
reentry = true
|
||||
}
|
||||
refusal = message
|
||||
phase = settledPhase
|
||||
syncSettledFromDisk()
|
||||
}
|
||||
|
||||
/// Step 1 — the model decides create-vs-update by the piece's
|
||||
@@ -242,14 +369,16 @@ struct MicropubPublishSheet: View {
|
||||
refusal = nil
|
||||
phase = postURL == nil ? .drafting : .updating
|
||||
Task {
|
||||
switch await posts.micropubSend(token: token) {
|
||||
switch await posts.micropubSend(token: token, categories: pendingCategories) {
|
||||
case let .drafted(url, preview):
|
||||
postURL = url
|
||||
previewURL = preview
|
||||
phase = .drafted
|
||||
sendLanded()
|
||||
case let .updated(url):
|
||||
postURL = url
|
||||
phase = .updated
|
||||
sendLanded()
|
||||
case let .authFailed(detail):
|
||||
fail("Micro.blog rejected the token: \(detail)", tokenRejected: true)
|
||||
case let .refused(reason), let .unreachable(reason):
|
||||
@@ -272,10 +401,11 @@ struct MicropubPublishSheet: View {
|
||||
refusal = nil
|
||||
phase = .publishing
|
||||
Task {
|
||||
switch await posts.micropubPublish(url: url, token: token) {
|
||||
switch await posts.micropubPublish(url: url, token: token, categories: pendingCategories) {
|
||||
case .published:
|
||||
publishedAt = Date()
|
||||
phase = .published
|
||||
settle()
|
||||
case let .authFailed(detail):
|
||||
fail("Micro.blog rejected the token: \(detail)", tokenRejected: true)
|
||||
case let .refused(reason), let .unreachable(reason):
|
||||
@@ -299,5 +429,63 @@ struct MicropubPublishSheet: View {
|
||||
publishedAt = nil
|
||||
previewURL = nil
|
||||
phase = .idle
|
||||
// #207: the forget clears only the micropub keys — a category
|
||||
// line the failed send already wrote stays on the piece, so the
|
||||
// field re-syncs to it (no false pending state).
|
||||
syncFieldFromDisk()
|
||||
}
|
||||
|
||||
// MARK: Categories (#207)
|
||||
|
||||
/// The piece's `category:` line as it is on DISK (the model's read —
|
||||
/// never the scan snapshot), in the codec's unbracketed form. No
|
||||
/// open saved piece, or an unreadable file → "".
|
||||
private var diskCategoryText: String {
|
||||
PostFrontmatter.categoryListValue(posts.openPieceCategories())
|
||||
}
|
||||
|
||||
/// The field AND its baseline take the disk's line (on open, after a
|
||||
/// forget).
|
||||
private func syncFieldFromDisk() {
|
||||
categoryField = diskCategoryText
|
||||
settledCategoryField = categoryField
|
||||
}
|
||||
|
||||
/// Only the BASELINE follows the disk (a failed step). The send
|
||||
/// writes the category line BEFORE the wire, so a refusal or a
|
||||
/// failed publish may leave the edit on the piece; the pending test
|
||||
/// must compare against what the piece really holds — a later clear
|
||||
/// of the field has to count as an edit. The field keeps the text as
|
||||
/// typed.
|
||||
private func syncSettledFromDisk() {
|
||||
settledCategoryField = diskCategoryText
|
||||
}
|
||||
|
||||
/// The blog's categories for the menu, over the Keychain's token.
|
||||
/// A failure is silent — no suggestions is a normal state (the
|
||||
/// field still works: a typed new name creates the category).
|
||||
private func loadSuggestions() {
|
||||
guard let token = MicropubTokenStore.read() else { return }
|
||||
Task {
|
||||
blogCategories = await Micropub.blogCategories(token: token)
|
||||
suggestionsLoaded = true
|
||||
}
|
||||
}
|
||||
|
||||
/// A send landed: the writer sees the canonical text, and the
|
||||
/// edit is no longer pending. (A REFUSED send keeps the text as typed.)
|
||||
private func settle() {
|
||||
categoryField = PostFrontmatter.categoryListValue(PostFrontmatter.categoryList(categoryField))
|
||||
settledCategoryField = categoryField
|
||||
}
|
||||
|
||||
/// A successful send: the field settles, and a token first entered
|
||||
/// in this sheet gets its suggestions now — once. A blog WITH no
|
||||
/// categories is not asked again after every send.
|
||||
private func sendLanded() {
|
||||
settle()
|
||||
if !suggestionsLoaded {
|
||||
loadSuggestions()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user