From ce524225a4c74aed82813b9bc067fd54c8160516 Mon Sep 17 00:00:00 2001 From: Christopher Warner Date: Sat, 10 Oct 2026 18:56:14 -0400 Subject: feat: format notes with Apple Notes-style structure Rewrite Markdown bodies into title, headings, spaced paragraphs, and Notes-friendly lists so iPhone notes no longer look like raw dumps. Co-authored-by: Cursor --- src/browser.rs | 77 +++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 58 insertions(+), 19 deletions(-) (limited to 'src/browser.rs') diff --git a/src/browser.rs b/src/browser.rs index b3b72d8..7763ef9 100644 --- a/src/browser.rs +++ b/src/browser.rs @@ -9,6 +9,7 @@ use serde::Deserialize; use tokio::time::sleep; use crate::config::{notes_url, Paths}; +use crate::format::{build_paragraphs, ParagraphStyle}; use crate::helpers::{format_note_text, looks_logged_out, LoggedOutSignals}; #[derive(Debug, Deserialize)] @@ -221,14 +222,62 @@ fn note_list_helpers_js() -> &'static str { "# } +fn paragraphs_json(title: &str, body: &str) -> Result { + let paras = build_paragraphs(title, body); + let wire: Vec = paras + .iter() + .map(|p| { + serde_json::json!({ + "style": match p.style { + ParagraphStyle::Title => "title", + ParagraphStyle::Heading => "heading", + ParagraphStyle::Subheading => "subheading", + ParagraphStyle::Body => "body", + ParagraphStyle::Bullet => "bullet", + ParagraphStyle::Dashed => "dashed", + ParagraphStyle::Numbered => "numbered", + ParagraphStyle::Checklist => "checklist", + ParagraphStyle::Monospace => "mono", + }, + "text": p.text, + "checked": p.checked, + }) + }) + .collect(); + Ok(serde_json::to_string(&wire)?) +} + +fn style_helpers_js() -> &'static str { + r#" + function writeNoteText(Note, dataManager, note, text) { + if (typeof Note.createInitialTopoTextString === "function" + && dataManager.topoTextManager + && typeof dataManager.topoTextManager.load === "function") { + const replacement = Note.createInitialTopoTextString(text); + dataManager.topoTextManager.load(note.id, replacement); + if (typeof note.userDidChangeTopoText === "function") note.userDidChangeTopoText(); + return; + } + if (typeof note.userDidChangeTopoText === "function") { + note.userDidChangeTopoText(text); + } + } + "# +} + async fn create_via_notes_app(page: &Page, title: &str, body: &str) -> Result { + // Prefer Rust-formatted plain text (Apple Notes spacing/lists); pass paras for future rich path let full = format_note_text(title, body); let text_js = serde_json::to_string(&full)?; + let paras_js = paragraphs_json(title, body)?; let helpers = note_list_helpers_js(); + let styles = style_helpers_js(); let expr = format!( r#"async () => {{ const text = {text_js}; + const paras = {paras_js}; {helpers} + {styles} const iframe = document.querySelector("iframe"); const w = iframe?.contentWindow; const app = w?.NotesApp; @@ -253,20 +302,22 @@ async fn create_via_notes_app(page: &Page, title: &str, body: &str) -> Result {{ const text = {text_js}; const matchTitle = {match_js}; {helpers} + {styles} const iframe = document.querySelector("iframe"); const w = iframe?.contentWindow; const app = w?.NotesApp; if (!app?.dataManager) throw new Error("NotesApp unavailable"); const dataManager = app.dataManager; - const mv = app.mainViewModel; const noteList = dataManager.allNotes || dataManager.__CW__allNotes; const note = findByTitle(noteList, matchTitle); if (!note) {{ throw new Error("No note titled " + JSON.stringify(matchTitle)); }} const Note = note.constructor; - // Preferred path: replace TopoText via NotesApp helpers - if (typeof Note.createInitialTopoTextString === "function" - && dataManager.topoTextManager - && typeof dataManager.topoTextManager.load === "function") {{ - const replacement = Note.createInitialTopoTextString(text); - dataManager.topoTextManager.load(note.id, replacement); - if (typeof note.userDidChangeTopoText === "function") {{ - note.userDidChangeTopoText(); - }} - }} else if (typeof note.userDidChangeTopoText === "function") {{ - note.userDidChangeTopoText(text); - }} else {{ - throw new Error("No supported update path on Note model"); - }} + writeNoteText(Note, dataManager, note, text); if (note.Title !== undefined) {{ note.Title = text.split("\n")[0] || matchTitle; }} -- cgit v1.2.3