restructure configuration variables

This commit is contained in:
Cade Scroggins
2022-08-05 11:36:53 -05:00
parent c66d9ed05a
commit 1c46b69a55
+51 -43
View File
@@ -7,14 +7,26 @@
<script> <script>
const CONFIG = { const CONFIG = {
commands: { commandPathDelimiter: '/',
commandSearchDelimiter: ' ',
defaultSearchTemplate: 'https://duckduckgo.com/?q={}',
openLinksInNewTab: true,
suggestionLimit: 4,
};
const COMMANDS = {
0: { 0: {
name: 'Local', name: 'Local',
searchTemplate: ':{}', searchTemplate: ':{}',
suggestions: ['0 8000', '0 8080'],
url: 'http://localhost:3000', url: 'http://localhost:3000',
}, },
a: { a: {
name: 'AWS', name: 'AWS',
suggestions: [
'a/cognito/v2/idp/user-pools',
'a/dynamodbv2#item-explorer',
],
url: 'https://us-west-2.console.aws.amazon.com/console/home', url: 'https://us-west-2.console.aws.amazon.com/console/home',
}, },
b: { b: {
@@ -25,11 +37,13 @@
c: { c: {
name: 'Calendar', name: 'Calendar',
searchTemplate: '/calendar/u/0/r/search?q={}', searchTemplate: '/calendar/u/0/r/search?q={}',
suggestions: ['c/calendar/u/1/r', 'c/calendar/u/2/r'],
url: 'https://calendar.google.com/calendar/u/0/r', url: 'https://calendar.google.com/calendar/u/0/r',
}, },
d: { d: {
name: 'Drive', name: 'Drive',
searchTemplate: '/drive/u/0/search?q={}', searchTemplate: '/drive/u/0/search?q={}',
suggestions: ['d/drive/u/1/my-drive', 'd/drive/u/2/my-drive'],
url: 'https://drive.google.com/drive/u/0/my-drive', url: 'https://drive.google.com/drive/u/0/my-drive',
}, },
f: { f: {
@@ -39,20 +53,29 @@
g: { g: {
name: 'GitHub', name: 'GitHub',
searchTemplate: '/search?q={}', searchTemplate: '/search?q={}',
suggestions: [
'g/trending',
'g/ossu',
'g/xvvvyz/tilde',
'gist.github.com',
],
url: 'https://github.com', url: 'https://github.com',
}, },
h: { h: {
name: 'Hypem', name: 'Hypem',
searchTemplate: '/search/{}', searchTemplate: '/search/{}',
suggestions: ['h/popular', 'h/popular/lastweek', 'h/tags'],
url: 'https://hypem.com/latest', url: 'https://hypem.com/latest',
}, },
i: { i: {
name: 'Inbox', name: 'Inbox',
suggestions: ['i/u/1/inbox', 'i/u/2/inbox'],
url: 'https://mail.proton.me/u/0/inbox', url: 'https://mail.proton.me/u/0/inbox',
}, },
k: { k: {
name: 'Keep', name: 'Keep',
searchTemplate: '/u/0/#search/text={}', searchTemplate: '/u/0/#search/text={}',
suggestions: ['k/u/1', 'k/u/2'],
url: 'https://keep.google.com/u/0', url: 'https://keep.google.com/u/0',
}, },
l: { l: {
@@ -63,6 +86,7 @@
m: { m: {
name: 'Mail', name: 'Mail',
searchTemplate: '/mail/u/0/#search/{}', searchTemplate: '/mail/u/0/#search/{}',
suggestions: ['m/mail/u/1/#inbox', 'm/mail/u/2/#inbox'],
url: 'https://mail.google.com/mail/u/0/#inbox', url: 'https://mail.google.com/mail/u/0/#inbox',
}, },
n: { n: {
@@ -71,16 +95,19 @@
}, },
o: { o: {
name: 'Discord', name: 'Discord',
suggestions: ['o/channels/830183651022471199'],
url: 'https://discord.com/channels/@me', url: 'https://discord.com/channels/@me',
}, },
r: { r: {
name: 'Reddit', name: 'Reddit',
searchTemplate: '/search?q={}', searchTemplate: '/search?q={}',
suggestions: ['r/r/startpages', 'r/r/webdev', 'r/r/onebag', 'r/r/fujix'],
url: 'https://www.reddit.com', url: 'https://www.reddit.com',
}, },
s: { s: {
name: 'Spotify', name: 'Spotify',
searchTemplate: '/search/{}', searchTemplate: '/search/{}',
suggestions: ['s/collection/tracks', 's/playlist/37i9dQZEVXcXr3r4FYT3J7'],
url: 'https://open.spotify.com', url: 'https://open.spotify.com',
}, },
t: { t: {
@@ -90,43 +117,20 @@
}, },
u: { u: {
name: 'Supabase', name: 'Supabase',
suggestions: ['u/project/xrncrutshmuifkzkexwb/editor'],
url: 'https://app.supabase.com', url: 'https://app.supabase.com',
}, },
v: { v: {
name: 'Vercel', name: 'Vercel',
suggestions: ['v/xvvvyz/lliist/deployments'],
url: 'https://vercel.com/dashboard', url: 'https://vercel.com/dashboard',
}, },
y: { y: {
name: 'YouTube', name: 'YouTube',
searchTemplate: '/results?search_query={}', searchTemplate: '/results?search_query={}',
suggestions: ['y/feed/trending'],
url: 'https://youtube.com/feed/subscriptions', url: 'https://youtube.com/feed/subscriptions',
}, },
},
defaultCommand: {
searchTemplate: '/?q={}',
url: 'https://duckduckgo.com',
},
openLinksInNewTab: true,
pathDelimiter: '/',
searchDelimiter: "'",
suggestionLimit: 4,
suggestions: {
0: ["0'8000", "0'8080"],
a: ['a/cognito/v2/idp/user-pools', 'a/dynamodbv2#item-explorer'],
c: ['c/calendar/u/1/r', 'c/calendar/u/2/r'],
d: ['d/drive/u/1/my-drive', 'd/drive/u/2/my-drive'],
g: ['g/trending', 'g/ossu', 'g/xvvvyz/tilde', 'gist.github.com'],
h: ['h/popular', 'h/popular/lastweek', 'h/tags'],
i: ['i/u/1/inbox', 'i/u/2/inbox'],
k: ['k/u/1', 'k/u/2'],
m: ['m/mail/u/1/#inbox', 'm/mail/u/2/#inbox'],
o: ['o/channels/830183651022471199'],
r: ['r/r/startpages', 'r/r/webdev', 'r/r/onebag', 'r/r/fujix'],
s: ['s/collection/tracks', 's/playlist/37i9dQZEVXcXr3r4FYT3J7'],
u: ['u/project/xrncrutshmuifkzkexwb/editor'],
v: ['v/xvvvyz/lliist/deployments'],
y: ['y/feed/trending'],
},
}; };
</script> </script>
@@ -259,7 +263,8 @@
#renderCommands() { #renderCommands() {
const template = document.getElementById('command-template'); const template = document.getElementById('command-template');
for (const [key, { name, url }] of Object.entries(CONFIG.commands)) { for (const [key, { name, url }] of Object.entries(COMMANDS)) {
if (!name || !url) continue;
const clone = template.content.cloneNode(true); const clone = template.content.cloneNode(true);
const command = clone.querySelector('.command'); const command = clone.querySelector('.command');
command.href = url; command.href = url;
@@ -453,7 +458,7 @@
static #formatSearchUrl(url, searchPath, search) { static #formatSearchUrl(url, searchPath, search) {
if (!searchPath) return url; if (!searchPath) return url;
const baseUrl = Search.#stripUrlPath(url); const [baseUrl] = Search.#splitUrl(url);
const urlQuery = encodeURIComponent(search); const urlQuery = encodeURIComponent(search);
searchPath = searchPath.replace(/{}/g, urlQuery); searchPath = searchPath.replace(/{}/g, urlQuery);
return baseUrl + searchPath; return baseUrl + searchPath;
@@ -475,39 +480,42 @@
return { query, url }; return { query, url };
} }
if (CONFIG.commands[query]) { if (COMMANDS[query]) {
const { key, url } = CONFIG.commands[query]; const { key, url } = COMMANDS[query];
return { key, query, url }; return { key, query, url };
} }
let splitBy = CONFIG.searchDelimiter; let splitBy = CONFIG.commandSearchDelimiter;
const [searchKey, rawSearch] = query.split(new RegExp(`${splitBy}(.*)`)); const [searchKey, rawSearch] = query.split(new RegExp(`${splitBy}(.*)`));
if (CONFIG.commands[searchKey]) { if (COMMANDS[searchKey]) {
const { searchTemplate, url: base } = CONFIG.commands[searchKey]; const { searchTemplate, url: base } = COMMANDS[searchKey];
const search = rawSearch.trim(); const search = rawSearch.trim();
const url = Search.#formatSearchUrl(base, searchTemplate, search); const url = Search.#formatSearchUrl(base, searchTemplate, search);
return { key: searchKey, query, search, splitBy, url }; return { key: searchKey, query, search, splitBy, url };
} }
splitBy = CONFIG.pathDelimiter; splitBy = CONFIG.commandPathDelimiter;
const [pathKey, path] = query.split(new RegExp(`${splitBy}(.*)`)); const [pathKey, path] = query.split(new RegExp(`${splitBy}(.*)`));
if (CONFIG.commands[pathKey]) { if (COMMANDS[pathKey]) {
const { url: base } = CONFIG.commands[pathKey]; const { url: base } = COMMANDS[pathKey];
const url = `${Search.#stripUrlPath(base)}/${path}`; const [baseUrl] = Search.#splitUrl(base);
const url = `${baseUrl}/${path}`;
return { key: pathKey, path, query, splitBy, url }; return { key: pathKey, path, query, splitBy, url };
} }
const { searchTemplate, url: base } = CONFIG.defaultCommand; const [baseUrl, rest] = Search.#splitUrl(CONFIG.defaultSearchTemplate);
const url = Search.#formatSearchUrl(base, searchTemplate, query); const url = Search.#formatSearchUrl(baseUrl, rest, query);
return { query, search: query, url }; return { query, search: query, url };
}; };
static #stripUrlPath(url) { static #splitUrl(url) {
const parser = document.createElement('a'); const parser = document.createElement('a');
parser.href = url; parser.href = url;
return `${parser.protocol}//${parser.hostname}`; const baseUrl = `${parser.protocol}//${parser.hostname}`;
const rest = `${parser.pathname}${parser.search}`;
return [baseUrl, rest];
} }
#close() { #close() {
@@ -548,7 +556,7 @@
return; return;
} }
let suggestions = CONFIG.suggestions[q.query] ?? []; let suggestions = COMMANDS[q.query]?.suggestions ?? [];
if (q.search && suggestions.length < CONFIG.suggestionLimit) { if (q.search && suggestions.length < CONFIG.suggestionLimit) {
const res = await Search.#fetchDuckDuckGoSuggestions(q.search); const res = await Search.#fetchDuckDuckGoSuggestions(q.search);