-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.js
117 lines (105 loc) · 3.63 KB
/
util.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
import RemNoteAPI from './index';
export async function getContext() {
const context = await RemNoteAPI.v0.get_context();
// TODO: transparent preloading
context.rem = await getRem({ id: context.remId });
context.document = await getRem({ id: context.documentId });
return context;
}
export async function getDocument() {
const context = await RemNoteAPI.v0.get_context();
const documentRem = await RemNoteAPI.v0.get(context.documentId);
return documentRem;
}
export async function getRem(options = {}) {
if (options.id) {
return RemNoteAPI.v0.get(options.id);
} else if (options.name) {
return RemNoteAPI.v0.get_by_name(options.name);
}
}
export async function getChildren(rem, visibleOnly = false) {
const children = visibleOnly ? rem.visibleRemOnDocument : rem.children;
// TODO: Children have the correct order, visibleRemOnDocument don't
return Promise.all(children.map((remId) => RemNoteAPI.v0.get(remId)));
}
/**
* Insert an image to a rem.
* @param {*} rem Rem or RemID
*/
export async function insertImage(rem, imageURL) {
if (typeof rem === 'string') {
rem = await getRem({ id: rem });
}
// append
console.log('remnote append', rem);
if (rem.content) {
await RemNoteAPI.v0.update(rem._id, {
content: rem.contentAsMarkdown + `![](${imageURL})`,
});
} else {
await RemNoteAPI.v0.update(rem._id, { name: rem.nameAsMarkdown + `![](${imageURL})` });
}
}
export async function getVisibleChildren(remId) {
return getChildren(remId, true);
}
/**
* Take a Rem, and extract its text. The rem.name and rem.content fields are
* both of type "RichTextInterface", which is an array of text strings, or js
* objects representing rich text element. Text is extracted recursively from
* Rem Reference elements.
*/
export async function getRemText(rem, exploredRem = []) {
if (!rem.found) return '';
const richTextElementsText = await Promise.all(
// Go through each element in the rich text
rem.name.concat(rem.content || []).map(async (richTextElement) => {
// If the element is a string, juts return it
if (typeof richTextElement == 'string') {
return richTextElement;
// If the element is a Rem Reference (i == "q"), then recursively get that Rem Reference's text.
} else if (richTextElement.i == 'q' && !exploredRem.includes(richTextElement._id)) {
return await getRemText(
await RemNoteAPI.v0.get(richTextElement._id),
// Track explored Rem to avoid infinite loops
exploredRem.concat([richTextElement._id])
);
} else {
// If the Rem is some other rich text element, just take its .text property.
return richTextElement.text;
}
})
);
return richTextElementsText.join('');
}
export async function loadText(remList) {
await Promise.all(
remList.map(async (rem) => {
rem.text = await getRemText(rem);
return rem;
})
);
}
export async function loadTags(rem) {
rem.tags = await Promise.all(
rem.tagParents.map(async (tagId) => {
let tagRem = await RemNoteAPI.v0.get(tagId);
return tagRem.nameAsMarkdown;
// let text = await getRemText(tagRem);
// console.log(text);
// return text;
})
);
}
/** ----------- Plugin related --------------- */
export function getPluginSettings(urlParamsStr, defaultSettings = {}) {
const params = new URLSearchParams(urlParamsStr);
function camelCase(str) {
return str.replace(/-([a-z])/g, function (g) {
return g[1].toUpperCase();
});
}
const settings = Object.fromEntries([...params.entries()].map(([k, v]) => [camelCase(k), v]));
return Object.assign(defaultSettings, settings);
}