Files
Docwell/Server/internal/convert/readwise.go
T
tajniak81andClaude Opus 5.5 3d1cf362d7 Add calendars, a web clipper, more importers and workspace snapshots
Phase F of the AFFiNE plan (ADR-16):

- Calendars: people subscribe to iCal feeds (webcal, Google's secret
  address, Outlook) or CalDAV accounts in account settings; their
  events show at the top of journal pages in the reader's time zone.
  Repeats, moved and cancelled occurrences, IANA and Windows zones
  are handled. Events are read on demand and kept in memory for 15
  minutes; passwords are sealed with DOCWELL_SECRET_KEY; sources
  follow the webhook address rules. New calendar_accounts collection.
- Importers: zips of Markdown (Obsidian vaults, Bear TextBundles and
  .bear2bk, Readwise's Markdown, Docwell's own space export) keep
  folders as pages, resolve [[wikilinks]] and embeds by name, and turn
  front matter and #tags into properties and tags. OneNote .mht
  exports, Readwise CSV, and any other CSV as a database page.
- Exports: PNG of a page drawn in the browser, print styles that print
  only the page in the light theme, and workspace snapshots (records,
  Yjs states, JSON and files in one zip) that restore as a new
  workspace. Old ids are swapped byte for byte inside the Yjs updates,
  so links point at the restored pages and comments stay anchored.
- Web-Clipper/: a Manifest V3 extension for Chrome, Edge and Firefox
  that saves the article or the selection as Markdown through the
  import API with a personal API token.
- OpenAPI, generated clients, translations and user docs.

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

132 lines
3.9 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 ""
}
pages := []NotionPage{{Title: "Readwise", Parent: -1}}
byBook := map[string]int{}
for _, row := range rows[1:] {
text := get(row, "highlight")
if text == "" {
continue
}
book := get(row, "book title")
if book == "" {
book = "Untitled"
}
i, ok := byBook[book]
if !ok {
if len(pages) > maxNotionPages {
break
}
i = len(pages)
byBook[book] = i
p := NotionPage{Title: book, Parent: 0, Doc: Node{Type: "doc"}}
if a := get(row, "book author"); a != "" {
p.Props = map[string]any{"Author": a}
p.Doc.Content = append(p.Doc.Content, Node{Type: "paragraph", Content: []Node{{Type: "text", Text: a, Marks: []Mark{{Type: "italic"}}}}})
}
for _, t := range strings.Split(get(row, "document tags"), ",") {
if t = strings.TrimSpace(t); t != "" {
p.Tags = append(p.Tags, t)
}
}
pages = append(pages, p)
}
p := &pages[i]
p.Doc.Content = append(p.Doc.Content, Node{Type: "blockquote", Content: []Node{textParagraph(text, nil)}})
if note := get(row, "note"); note != "" {
p.Doc.Content = append(p.Doc.Content, textParagraph(note, nil))
}
var meta []string
if loc := get(row, "location"); loc != "" {
kind := get(row, "location type")
if kind == "" || strings.EqualFold(kind, "order") {
kind = "location"
}
meta = append(meta, kind+" "+loc)
}
if at := get(row, "highlighted at"); len(at) >= 10 {
meta = append(meta, at[:10])
}
for _, t := range strings.Split(get(row, "tags"), ",") {
if t = strings.TrimSpace(t); t != "" {
meta = append(meta, "#"+t)
}
}
if len(meta) > 0 {
p.Doc.Content = append(p.Doc.Content, textParagraph(strings.Join(meta, " · "), []Mark{{Type: "textColor", Attrs: map[string]any{"color": "gray"}}}))
}
}
if len(pages) == 1 {
return nil, ErrNotNotion
}
pages[0].Doc = Node{Type: "doc", Content: []Node{textParagraph("Highlights imported from Readwise.", nil)}}
return pages, nil
}
// 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
}