-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
63 lines (48 loc) · 1.57 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
import process from 'node:process';
export const separator = Symbol('separator');
export const isDarkMode = process.env.XBARDarkMode === 'true';
export const isXbar = process.env.__CFBundleIdentifier === 'com.xbarapp.app';
const encodeHref = url => {
url = encodeURI(url);
url = url.replaceAll('\'', '%27');
url = url.replaceAll('&', '%26');
return url;
};
export const _create = (input, options = {}, menuLevel = 0) => {
if (options.text !== undefined) {
throw new TypeError('The `text` option is not supported as a top-level option. Use it on an item instead.');
}
return input.map(line => {
if (typeof line === 'string') {
line = {text: line};
}
if (line === separator) {
return '--'.repeat(menuLevel) + '---';
}
line = {
...options,
...line,
};
const {text} = line;
if (typeof text !== 'string') {
throw new TypeError('The `text` property is required and should be a string');
}
delete line.text;
let submenuText = '';
if (typeof line.submenu === 'object' && line.submenu.length > 0) {
submenuText = `\n${_create(line.submenu, options, menuLevel + 1)}`;
delete line.submenu;
}
const prefix = '--'.repeat(menuLevel);
return text.split('\n').map(textLine => {
const options = Object.entries(line).map(([key, value]) => {
const finalValue = key === 'href' ? encodeHref(value) : value;
return `${key}="${finalValue}"`;
}).join(' ');
return `${prefix}${textLine}|${options}`;
}).join('\n') + submenuText;
}).join('\n');
};
export default function xbar(input, options) {
console.log(_create(input, options));
}