-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgistMirror.js
288 lines (266 loc) · 9.38 KB
/
gistMirror.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
* Mirror GitHub Gists to Trilium Note tree
*
* To install:
* 1. Create note of type `JS backend` with the following attributes:
* a. #gistRoot anchors the gist mirror (required)
* b. #gistUsername set to the GitHub username
* 2. When the script runs it will create any missing templates and set missing attributes
*/
"use strict";
const fetch = require("node-fetch");
const process = require("process");
const sql = require("./sql");
let utc = require("dayjs/plugin/utc");
api.dayjs.extend(utc);
const gistRootNote = api.getNoteWithLabel("gistRoot");
if (!gistRootNote) {
api.log("Info: gistRoot does not exist");
return;
}
let gistUsername = gistRootNote.getLabelValue("gistUsername");
if (!gistUsername) {
gistUsername =
process.env.SUDO_USER ||
process.env.C9_USER ||
process.env.LOGNAME ||
process.env.USER ||
process.env.LNAME ||
process.env.USERNAME;
}
if (!gistUsername) {
throw new Error("gistUsername not set and unable to determine user name");
}
verifyTemplate(api.currentNote, gistRootNote);
// TODO: Add support for authentication
// TODO: Should private gist be imported as protected?
fetch(`https://api.github.com/users/${gistUsername}/gists`, {
headers: {
Accept: "application/vnd.github+json",
},
})
.then(async (response) => {
if (!response.ok) {
api.log(`Error: Listing gists failed with ${response.status}: ${response.url}`);
return;
}
return await response.json();
})
.then((gists) => {
for (const gist of gists) {
const note = api.searchForNote(`note.title="${gist.description}"`,
{ ancestorNoteId: gistRootNote.noteId }
);
if (note) {
updateNote(gistRootNote, note, gist);
} else {
newNote(gistRootNote, gist);
}
}
})
.then(() => {
gistRootNote.setLabel("lastUpdated", api.dayjs.utc().format());
})
.catch((err) => {
api.log(`Error: Failed fetching gists, ${err}`);
});
const scriptNote = api.currentNote;
scriptNote.setLabel("lastUpdated", api.dayjs.utc().format());
/**
* Add given gist to existing root note including attached files
*
* @param {BNote} parent BNote to add new note to
* @param {Object} gist GitHub Gist object
*/
function newNote(parent, gist) {
const lastUpdated = api.dayjs.utc();
const gistUpdatedAt = api.dayjs.utc(gist.updated_at);
const note = api.transactional(() => {
const note = api.createNewNote({
parentNoteId: parent.noteId,
title: gist.description,
content: "",
type: "text",
isProtected: false,
}).note;
// The labels #gistId and #gistUrl are promoted in the default template
note.setLabel("gistId", gist.id);
note.setLabel("gistUrl", gist.url);
note.setLabel("iconClass", "bx bxl-github");
note.setLabel("lastUpdated", lastUpdated.utc().format());
note.setLabel("gistUpdatedAt", gistUpdatedAt.utc().format());
note.setLabel("readOnly");
note.setRelation("isChildOf", parent.noteId);
return note;
});
for (const file of Object.values(gist.files)) {
fetch(file.raw_url, {
headers: {
Accept: "application/vnd.github.raw",
},
})
.then(async (response) => {
if (!response.ok) {
throw new Error(
`Failed to fetch file with ${response.status}, ${response.url}`
);
}
return await response.text();
})
.then((content) => {
api.transactional(() => {
const n = createFileNote(note.noteId, file, content);
// n.setLabel('readOnly'); TODO why does this break syntax highlighting?
n.setRelation("isChildOf", note.noteId);
});
})
.catch((err) => {
api.log(`Error: fetching ${file.filename}, ${err}`);
});
}
}
/**
* Update existing note with latest gist data, new notes created if needed
*
* @param {BNote} root Trilium gistRoot note
* @param {BNote} note Trilium note to update
* @param {Object} gist GitHub Gist object holding updated data
*/
function updateNote(root, note, gist) {
let lastUpdated = api.dayjs.utc(note.getLabelValue("lastUpdated"));
if (!lastUpdated.isValid()) {
lastUpdated = api.dayjs.utc("1970-01-01T00:00:00Z");
}
const gistUpdatedAt = api.dayjs.utc(gist.updated_at);
if (gistUpdatedAt.isAfter(lastUpdated)) {
for (const file of Object.values(gist.files)) {
const childNote = api.searchForNote(`note.title="${file.filename}"`,
{ ancestorNoteId: root.noteId }
);
fetch(file.raw_url, {
headers: {
Accept: "application/vnd.github.raw",
},
})
.then(async (response) => {
if (!response.ok) {
throw new Error(
`Failed to fetch file with ${response.status}, ${response.url}`
);
}
return await response.text();
})
.then((content) => {
if (childNote) {
childNote.setContent(content);
} else {
api.transactional(() => {
const n = createFileNote(note.noteId, file, content);
n.setRelation("isChildOf", note.noteId);
});
}
})
.catch((err) => {
api.log(`Error: fetching ${file.filename}, ${err}`);
});
}
}
}
// TODO: How to call the mimeTypesService.getMimeTypes()from here?
const langDetails = [
{ name: "Go", mime: "text/x-go", icon: "bx bxl-go-lang" },
{ name: "Markdown", mime: "text/x-markdown", icon: "bx bxs-file-md" },
{ name: "Python", mime: "text/x-python", icon: "bx bxl-python" },
{ name: "Ruby", mime: "text/x-ruby", icon: "bx bx-code-alt" },
{ name: "Shell", mime: "text/x-sh", icon: "bx bx-terminal" },
{ name: "Text", mime: "text/plain", icon: "bx bxs-file-txt" },
{ name: "TypeScript", mime: "text/typescript", icon: "bx bxl-typescript" },
{ name: "JavaScript", mime: "text/javascript", icon: "bx bxl-javascript" },
];
/**
* Transform GitHub Gist language to Trilium mime type
*
* @param {string} lang GitHub Gist language type
* @returns trilium mime type
*/
function transformMimeType(lang) {
const props = langDetails.find((obj) => obj.name === lang);
return props ? props.mime : lang;
}
/**
* Map GitHub Gist language to boxicon icon class
*
* @param {string} lang GitHub Gist language type
* @returns boxicon icon class
*
* @see https://boxicons.com/
*/
function mapIcon(lang) {
const props = langDetails.find((obj) => obj.name === lang);
return props ? props.icon : "bx bx-file-blank";
}
/**
* Create new note with given file as content
*
* @param {string} parentId Id of parent note
* @param {Object} file gist attachment data
* @param {string} content content of file
* @returns BNote representing new file
*/
function createFileNote(parentId, file, content) {
// TODO: Could this be refactored to use importSingleFile()?
const note = api.createNewNote({
parentNoteId: parentId,
title: file.filename,
content: content,
type: "code",
mime: transformMimeType(file.language),
isProtected: false,
}).note;
note.setLabel("iconClass", mapIcon(file.language));
return note;
}
/**
* Verify template note exists and #gistRootNote defines relationship ~child:template
*
* Note:
* 1. If #gistRoot has a child note, it will be used as ~template, otherwise a
* default template note will be created
* a. the new template will be initialized with the labels #readOnly and #iconClass,
* and the promoted labels: #gistId and #gistUrl
* 2.Verify #gistRoot contains the relation ~child:template
*
* @param {BNote} scriptNote note holding this script
* @param {BNote} rootNote note anchoring the gist tree
* @returns BNote template note
*/
function verifyTemplate(scriptNote, rootNote) {
let template;
if (scriptNote.hasChildren()) {
template = scriptNote?.getChildNotes()[0];
} else {
api.transactional(() => {
template = api.createNewNote({
parentNoteId: scriptNote.noteId,
title: "template",
content: "",
type: "text",
isProtected: false,
}).note;
template.setLabel("label:gistId", "promoted,single,text");
template.setLabel("label:gistUrl", "promoted,single,url");
template.setLabel("iconClass", "bx bxl-github");
template.setLabel("readOnly");
});
}
if (!rootNote.hasRelation("child:template")) {
rootNote.setRelation("child:template", template.noteId);
}
if (!rootNote.hasLabel("iconClass")) {
rootNote.setLabel("iconClass", "bx bxl-github");
}
if (!rootNote.hasLabel("sorted")) {
rootNote.setLabel("sorted", "gistUpdatedAt");
}
return template;
}