-
Notifications
You must be signed in to change notification settings - Fork 0
/
readwisetotanapaste_ddv.js
98 lines (77 loc) · 2.1 KB
/
readwisetotanapaste_ddv.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Name: Grab Readwise Highlights
import "@johnlindquist/kit"
let API_TOKEN = await env("TANA_API_TOKEN")
let HIGHLIGHTS_URL = "https://readwise.io/api/v2/export/"
// Credit to: https://github.com/tanainc/tana-paste-examples
// Copy/pasted and refactored to Script Kit: scriptkit.com
let getItemsFromReadwise = async (daysToFetch = 1) => {
let dateOffset = 24 * 60 * 60 * 1000 * daysToFetch
let updatedAfterDate = new Date()
updatedAfterDate.setTime(
updatedAfterDate.getTime() - dateOffset
)
let response = await fetch(
`${HIGHLIGHTS_URL}?updatedAfter=${updatedAfterDate.toISOString()}`,
{
headers: {
Authorization: `Token ${API_TOKEN}`,
},
}
)
return await response.json()
}
let daysToFetch = await arg("Days to fetch")
let { results } = await getItemsFromReadwise(
parseInt(daysToFetch, 10)
)
let result = results.map(book => {
let isValidSourceURL =
book.source_url?.startsWith("https://")
let hasHighlights = book.highlights?.length > 0
let title = `${book.title}`
let url = ``
if (isValidSourceURL) {
url = `Source URL:: ${book.source_url}`
}
let type = `Readwise category:: ${book.category?.replace(/s$/, "")}`
let author = `Author:: ${book.author}`
let highlights = book.highlights.map(highlight => {
let lines = highlight.text.split("\n")
let cleanLines = lines.map(line => {
let cleanedLine = line.replace(/•\s+/, "").trim()
return `${cleanedLine}`
})
let note = ``
if (highlight.note) {
note = `${highlight.note}`
}
return `
${cleanLines}
${note}
`.trim()
})
let maybeUrl = url
? `
- ${url}
`
: ``
let maybeHighlights = highlights?.length
? `
- Highlights
- ${highlights.join("\n")}`
: ``
return `
- Rw ${title} #referenceNote
${maybeUrl}
- ${type}
- ${author}
${maybeHighlights} #highlight
`.trim()
})
// because I'm on a trial account (I assume)
let [skip, ...rest] = result
// copies the readwise content to the clipboard
// I'm using slice to only take 2 results for demo purposes
await copy(`%%tana%%
${rest.join("\n")}
`)