// 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 }