Files
tajniak81andClaude Opus 5.5 6984c7d162 Connect Google Calendar, import OneNote notebooks, sync Readwise, and package the clipper
Phase F follow-ups (ADR-16):

- Calendars: events are now stored in calendar_events by a
  calendars.sync job (every 15 minutes per account, 90 days back and
  400 ahead), so journals open at once and keep showing events while a
  source is down. Google accounts connect with OAuth (calendar.readonly)
  through DOCWELL_GOOGLE_CLIENT_ID/SECRET; reconnecting renews access.
- OneNote: whole notebooks import through Microsoft Graph (Notes.Read,
  DOCWELL_MICROSOFT_CLIENT_ID/SECRET/TENANT) in the background, with
  section groups, sections, subpages, pictures and attached files. The
  Microsoft sign-in lives in memory for an hour and is never stored.
- Readwise: an hourly sync with the person's sealed access token; new
  books get a page under "Readwise", new highlights are appended once.
  New readwise_syncs collection.
- internal/oauth: signed states and one callback route per provider,
  routed by purpose. directory gains ImportConverted and CallerOf.
- Phone app: "Today's journal" on the home screen and calendar events
  on journal pages.
- Web clipper: `npm run package` builds Chrome/Edge, Firefox and source
  zips; store/ holds listing texts, permission notes and a privacy
  policy.
- OpenAPI, generated clients, translations, compose and .env examples,
  and user docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 14:59:34 +01:00

189 lines
5.7 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package convert
import (
"bytes"
"encoding/csv"
"strings"
)
// ---- CSV: Readwise highlights, or a table ----------------------------------
// FromCSV reads a CSV file as a page holding one database (ADR-13): the
// header names the columns, the first column is the title.
func FromCSV(data []byte) (Node, error) { return notionTable(data) }
// IsReadwiseCSV tells Readwise's "Export to CSV" from any other CSV.
func IsReadwiseCSV(data []byte) bool {
r := csv.NewReader(bytes.NewReader(bytes.TrimPrefix(data, []byte(utf8BOM))))
r.LazyQuotes = true
head, err := r.Read()
if err != nil {
return false
}
has := map[string]bool{}
for _, h := range head {
has[strings.ToLower(strings.TrimSpace(h))] = true
}
return has["highlight"] && has["book title"]
}
// ReadReadwise turns a Readwise CSV export into a "Readwise" page with one
// page per book or article under it: its highlights as quotes, each with its
// note, location and tags. The book's document tags become the page's tags
// and its author the "Author" property.
func ReadReadwise(data []byte) ([]NotionPage, error) {
r := csv.NewReader(bytes.NewReader(bytes.TrimPrefix(data, []byte(utf8BOM))))
r.FieldsPerRecord = -1
r.LazyQuotes = true
rows, err := r.ReadAll()
if err != nil {
return nil, err
}
if len(rows) == 0 {
return nil, ErrNotNotion
}
col := map[string]int{}
for i, h := range rows[0] {
col[strings.ToLower(strings.TrimSpace(h))] = i
}
get := func(row []string, name string) string {
if i, ok := col[name]; ok && i < len(row) {
return strings.TrimSpace(row[i])
}
return ""
}
var books []*ReadwiseBook
byBook := map[string]*ReadwiseBook{}
for _, row := range rows[1:] {
text := get(row, "highlight")
if text == "" {
continue
}
title := get(row, "book title")
if title == "" {
title = "Untitled"
}
b := byBook[title]
if b == nil {
if len(books) >= maxNotionPages {
break
}
b = &ReadwiseBook{Title: title, Author: get(row, "book author"), Tags: splitTags(get(row, "document tags"))}
byBook[title] = b
books = append(books, b)
}
b.Highlights = append(b.Highlights, ReadwiseHighlight{
Text: text, Note: get(row, "note"), Location: get(row, "location"), LocationType: get(row, "location type"),
HighlightedAt: get(row, "highlighted at"), Tags: splitTags(get(row, "tags")),
})
}
if len(books) == 0 {
return nil, ErrNotNotion
}
pages := []NotionPage{{Title: "Readwise", Parent: -1, Doc: ReadwiseIndex()}}
for _, b := range books {
p := b.Page()
p.Parent = 0
pages = append(pages, p)
}
return pages, nil
}
func splitTags(v string) []string {
var out []string
for _, t := range strings.Split(v, ",") {
if t = strings.TrimSpace(t); t != "" {
out = append(out, t)
}
}
return out
}
// ReadwiseIndex is the document of the page that holds the books.
func ReadwiseIndex() Node {
return Node{Type: "doc", Content: []Node{textParagraph("Highlights imported from Readwise.", nil)}}
}
// ReadwiseBook is a book, article or other document with its highlights,
// from a Readwise CSV export or its API.
type ReadwiseBook struct {
Title, Author string
// URL is where the document is (articles, tweets); "" for books.
URL string
Tags []string
Highlights []ReadwiseHighlight
}
// ReadwiseHighlight is one highlight. LocationType "order" (or "") reads as
// a plain location.
type ReadwiseHighlight struct {
Text, Note, Location, LocationType, HighlightedAt string
Tags []string
}
// Page is the book's page: its author, then its highlights. Tags are the
// document's tags, and the author is the "Author" property.
func (b ReadwiseBook) Page() NotionPage {
p := NotionPage{Title: b.Title, Parent: -1, Doc: Node{Type: "doc"}, Tags: b.Tags}
if b.Author != "" {
p.Props = map[string]any{"Author": b.Author}
p.Doc.Content = append(p.Doc.Content, Node{Type: "paragraph", Content: []Node{{Type: "text", Text: b.Author, Marks: []Mark{{Type: "italic"}}}}})
}
if strings.HasPrefix(b.URL, "https://") || strings.HasPrefix(b.URL, "http://") {
p.Doc.Content = append(p.Doc.Content, Node{Type: "paragraph", Content: []Node{{Type: "text", Text: b.URL, Marks: []Mark{{Type: "link", Attrs: map[string]any{
"href": b.URL, "target": "_blank", "rel": "noopener noreferrer nofollow", "class": nil,
}}}}}})
}
p.Doc.Content = append(p.Doc.Content, ReadwiseNodes(b.Highlights)...)
if len(p.Doc.Content) == 0 {
p.Doc.Content = []Node{{Type: "paragraph"}}
}
return p
}
// ReadwiseNodes are highlights as blocks: each a quote, its note, and a grey
// line with its location, date and tags.
func ReadwiseNodes(hs []ReadwiseHighlight) []Node {
var out []Node
for _, h := range hs {
out = append(out, Node{Type: "blockquote", Content: []Node{textParagraph(h.Text, nil)}})
if h.Note != "" {
out = append(out, textParagraph(h.Note, nil))
}
var meta []string
if h.Location != "" {
kind := h.LocationType
if kind == "" || strings.EqualFold(kind, "order") {
kind = "location"
}
meta = append(meta, kind+" "+h.Location)
}
if len(h.HighlightedAt) >= 10 {
meta = append(meta, h.HighlightedAt[:10])
}
for _, t := range h.Tags {
meta = append(meta, "#"+t)
}
if len(meta) > 0 {
out = append(out, textParagraph(strings.Join(meta, " · "), []Mark{{Type: "textColor", Attrs: map[string]any{"color": "gray"}}}))
}
}
return out
}
// textParagraph is a paragraph of plain text; line breaks stay.
func textParagraph(s string, marks []Mark) Node {
p := Node{Type: "paragraph"}
for i, line := range strings.Split(strings.ReplaceAll(s, "\r\n", "\n"), "\n") {
if i > 0 {
p.Content = append(p.Content, Node{Type: "hardBreak"})
}
if line != "" {
p.Content = append(p.Content, Node{Type: "text", Text: line, Marks: marks})
}
}
return p
}