-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (54 loc) · 1.33 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
const arvish = require("arvish");
const conf = {
lang: process.env.lang
};
const transform = (resp) => {
const items = [];
resp.map((word) => {
word.meanings.map((meaning) => {
meaning.definitions.map((definition) => {
items.push({
title: `[${meaning.partOfSpeech}]: ${word.word}: ${definition.definition}`,
subtitle: definition.example ? `${definition.example}` : "",
arg: definition.definition,
variables: {
action: "clipboard",
},
quicklook: {
type: 'markdown',
data:
`## ${word.word}
### ${meaning.partOfSpeech}
#### ${word.phonetics[0].text}
> ${definition.definition}
${definition.example ? `### Example\n${definition.example}` : ""}
`}
});
});
});
});
return items;
};
const getPluginItems = async ({ inputStr }) => {
if (inputStr === "") {
return {
items: [],
};
}
const configItems = [];
if (inputStr && inputStr.length > 1) {
try {
const resp = await arvish.fetch(
`https://api.dictionaryapi.dev/api/v2/entries/${conf.lang}/${inputStr}`,
{ maxAge: 86400000 }
);
return {
items: [...transform(resp), ...configItems],
};
} catch (err) {}
}
return {
items: configItems,
};
};
module.exports = getPluginItems;