quetre/utils/urlModifiers.js
kareem d16ae48dcb
feat: add support for other languages
Co-authored-by: k0xp00f <d.abdelkarim1404+gmail.com>
2023-01-07 17:22:20 +00:00

23 lines
691 B
JavaScript

/* eslint-disable import/prefer-default-export */
import { acceptedLanguages } from './constants.js';
/**
* modifies link to be quetre-friendly.
* @param {string} url the quora url to transform. could be relative or absolute
*/
export const quetrefy = url => {
try {
const link = new URL(url);
const subdomain = link.hostname.split('.')[0];
// normal url
if (subdomain === 'www') return link.pathname;
// lang specific route
if (acceptedLanguages.includes(subdomain))
return `${link.pathname}?lang=${subdomain}`;
// must be spaces link
return `/space/${subdomain}${link.pathname}`;
} catch {
// must be a relative url
return url;
}
};