-
Notifications
You must be signed in to change notification settings - Fork 12
/
singlefile2trilium-handler.js
65 lines (54 loc) · 1.85 KB
/
singlefile2trilium-handler.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
// HowTo:
// 1- Add this file to trilium as a `JS Backend` code note.
// 2- Set label `customRequestHandler` to 'singlefile2trilium'.
const preamble = `
<iframe id="__trilium_iframe" style="width:100%; flex-grow: 1; border:none;" srcdoc="`;
const postamble = `
">iframe not loaded properly?</iframe>
<script>
(function () {
let iframe = document.getElementById("__trilium_iframe");
function pageY(elem) {
return elem.offsetParent ? (elem.offsetTop + pageY(elem.offsetParent)) : elem.offsetTop;
}
function resizeIframe() {
let height = document.documentElement.clientHeight;
height -= pageY(iframe) + 20 ;
height = (height < 0) ? 0 : height;
if (!iframe) return;
iframe.style.height = height + 'px';
}
iframe.addEventListener('load', resizeIframe);
window.addEventListener('resize', resizeIframe);
})();
</script>
`;
const {req, res} = api;
const {title, url, content} = req.body;
if (req.method == 'POST') {
const todayNote = api.getTodayNote();
const renderNote = (api.createNewNote({
parentNoteId: todayNote.noteId,
title: title,
content: '',
type: 'render'
})).note;
renderNote.setLabel('clipType', 'singlefile2trilium');
renderNote.setLabel('pageUrl', url);
renderNote.setLabel('pageTitle', title);
const encodedContent = content.replaceAll('"', '"');
const wrapped_content = preamble + encodedContent + postamble;
const htmlNote = (api.createNewNote({
parentNoteId: renderNote.noteId,
title: 'content.html',
content: wrapped_content,
type: 'code',
mime: 'text/html'
})).note;
htmlNote.setLabel('archived');
renderNote.setRelation('renderNote', htmlNote.noteId);
res.send(201); // http 201: created
}
else {
res.send(400); // http 400: bad request
}