diff --git a/app/javascript/gabsocial/components/status_card.js b/app/javascript/gabsocial/components/status_card.js index b535cd81..e079acbf 100644 --- a/app/javascript/gabsocial/components/status_card.js +++ b/app/javascript/gabsocial/components/status_card.js @@ -8,36 +8,16 @@ import { CX, DEFAULT_REL, } from '../constants' +import { + decodeIDNA, + getHostname, + trim, +} from '../utils/urls' import ResponsiveClassesComponent from '../features/ui/util/responsive_classes_component' import Icon from './icon' import Button from './button' import Text from './text' -const IDNA_PREFIX = 'xn--' - -const decodeIDNA = domain => { - return domain - .split('.') - .map(part => part.indexOf(IDNA_PREFIX) === 0 ? punycode.decode(part.slice(IDNA_PREFIX.length)) : part) - .join('.') -} - -const getHostname = url => { - const parser = document.createElement('a') - parser.href = url - return parser.hostname -} - -const trim = (text, len) => { - const cut = text.indexOf(' ', len) - - if (cut === -1) { - return text - } - - return text.substring(0, cut) + (text.length > len ? '…' : '') -} - const domParser = new DOMParser() const addAutoPlay = html => { diff --git a/app/javascript/gabsocial/utils/urls.js b/app/javascript/gabsocial/utils/urls.js new file mode 100644 index 00000000..a7c470f1 --- /dev/null +++ b/app/javascript/gabsocial/utils/urls.js @@ -0,0 +1,24 @@ +const IDNA_PREFIX = 'xn--' + +export const decodeIDNA = (domain) => { + return domain + .split('.') + .map(part => part.indexOf(IDNA_PREFIX) === 0 ? punycode.decode(part.slice(IDNA_PREFIX.length)) : part) + .join('.') +} + +export const getHostname = (url) => { + const parser = document.createElement('a') + parser.href = url + return parser.hostname +} + +export const trim = (text, len) => { + const cut = text.indexOf(' ', len) + + if (cut === -1) { + return text + } + + return text.substring(0, cut) + (text.length > len ? '…' : '') +} \ No newline at end of file