-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtana-export.js
88 lines (80 loc) · 2.14 KB
/
tana-export.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
const defaultNode = draft.processTemplate("[[tanaDefaultNode]]").trim();
const saveMode = draft.processTemplate("[[tanaSaveMode]]").trim();
const credential = Credential.create(
"tana-workspace",
"Tana.inc\nPress cmd+k and select 'Get API Token' on your inbox node"
);
credential.addPasswordField(
"token",
"Token",
);
if (!credential.authorize()) {
alert("No API token provided or found.");
context.fail(
"Tana API token not entered, or not found. Clear API token and try again if no prompt was provided."
);
}
var content = buildTanaContent();
sendTana(content)
function buildTanaContent() {
// builds a json payload
// Just send the whole draft as a Tana node unless the mode is "child"
const [, ...body] = draft.content.split('\n');
switch (saveMode) {
case "single":
break;
case "child":
// Use the first line as a parent node,
// and the rest as a child node
return [{
name: draft.displayTitle,
children: [
{name: body.join('\n').replace(/\n/g,"")}
]
}];
//{name: body.join('\n').replace(/\n/g,"")}
case "children":
// Use the first line as a parent node,
// and the rest of the draft - split by newlines -
// as child nodes
let c = [];
for (let line of body) {
if (line.trim().length > 0) {
c.push({name: line})
}
}
return [{
name: draft.displayTitle,
children: c
}];
default:
console.log("Unexpected save mode: '" + saveMode + "'; assuming 'single'")
break;
}
return [{name: draft.content.replace(/\n/g,"")}];
}
function sendTana(contentObject, node=defaultNode) {
const url = "https://europe-west1-tagr-prod.cloudfunctions.net/addToNodeV2";
if (defaultNode.trim().length == 0) {
node="INBOX";
}
let payload = {
targetNodeId: node,
nodes: contentObject
};
let http = HTTP.create();
let ttok = credential.getValue("token");
let req = {
url: url,
"method": "POST",
"headers": {
"Authorization": "Bearer " + ttok,
"Content-Type": "application/json"
},
"data": payload
}
let response = http.request(req);
if (!response.success){
alert("Failed sending: " + response.responseText)
}
}