2021-06-24 12:53:45 +02:00
|
|
|
import { queries } from './searchQueries.json';
|
|
|
|
import { Query } from '../interfaces';
|
|
|
|
|
|
|
|
import { searchConfig } from '.';
|
|
|
|
|
2021-06-25 10:42:44 +02:00
|
|
|
export const searchParser = (searchQuery: string): boolean => {
|
2021-07-05 23:04:03 -05:00
|
|
|
const splitQuery = searchQuery.match(/^\/([a-z]+)[ ](.+)$/i);
|
|
|
|
const prefix = splitQuery ? splitQuery[1] : searchConfig('defaultSearchProvider', 'd');
|
|
|
|
const search = splitQuery ? encodeURIComponent(splitQuery[2]) : encodeURIComponent(searchQuery);
|
2021-06-24 12:53:45 +02:00
|
|
|
|
|
|
|
const query = queries.find((q: Query) => q.prefix === prefix);
|
|
|
|
|
|
|
|
if (query) {
|
2021-06-25 11:24:29 +02:00
|
|
|
const sameTab = searchConfig('searchSameTab', false);
|
2021-06-24 12:53:45 +02:00
|
|
|
|
|
|
|
if (sameTab) {
|
|
|
|
document.location.replace(`${query.template}${search}`);
|
|
|
|
} else {
|
|
|
|
window.open(`${query.template}${search}`);
|
|
|
|
}
|
2021-06-25 10:42:44 +02:00
|
|
|
|
|
|
|
return true;
|
2021-06-24 12:53:45 +02:00
|
|
|
}
|
2021-06-25 10:42:44 +02:00
|
|
|
|
|
|
|
return false;
|
2021-07-16 11:55:26 +02:00
|
|
|
}
|