-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (89 loc) · 2.36 KB
/
index.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
import "@logseq/libs";
import PromiseWorker from "promise-worker";
import InlineWorker from "./worker.js?worker&inline";
import wheels from "./wheels";
const showError = (err) => {
logseq.App.showMsg(err.toString(), "error");
};
const settingsSchema = [
{
key: "language",
type: "string",
title: "Language to use for summaries",
description:
"What language do you want to default to when summarizing text?",
default: "english",
},
{
key: "numberOfSentences",
type: "string",
title: "Number of sentences",
description:
"How many sentences do you want to generate when you summarize content?",
default: 3,
},
{
key: "replaceSourceContent",
type: "boolean",
title: "Replace source content",
description: "Should summaries replace source content or be nested?",
default: true,
},
];
let worker;
async function main() {
worker = new PromiseWorker(new InlineWorker());
await worker.postMessage({
type: "init",
urls: {
pyodide: logseq.resolveResourceFullUrl("pyodide/pyodide.js"),
punkt: logseq.resolveResourceFullUrl("punkt.zip"),
},
wheels: wheels.map((wheel) =>
logseq.resolveResourceFullUrl(`wheels/${wheel}`)
),
});
logseq.Editor.registerSlashCommand("Summary", async () => {
try {
const { language, numberOfSentences, replaceSourceContent } =
logseq.settings;
const { content, uuid } = await logseq.Editor.getCurrentBlock();
const summary = await worker.postMessage({
language,
count: numberOfSentences,
text: content,
});
if (replaceSourceContent) {
await logseq.Editor.updateBlock(uuid, summary[0]);
await logseq.Editor.insertBatchBlock(
uuid,
summary.slice(1).map((c) => {
return { content: c };
}),
{ sibling: true }
);
} else {
await logseq.Editor.insertBatchBlock(
uuid,
{
content: "summary",
children: summary.map((c) => {
return { content: c };
}),
},
{ sibling: false }
);
}
} catch (err) {
showError(err);
}
});
}
logseq.beforeunload(() => {
if (worker) {
worker._worker.terminate();
worker = null;
}
});
logseq.useSettingsSchema(settingsSchema);
logseq.ready(main).catch(showError);