From e7d894b54cf6adb3999ed03396524a9c874769d6 Mon Sep 17 00:00:00 2001 From: Sam Becker Date: Sun, 15 Oct 2023 11:19:19 -0500 Subject: [PATCH] Improve site-wide url parsing --- __tests__/path.test.ts | 8 ++++---- __tests__/string.test.ts | 24 ++++++++++++++++++++++++ src/site/config.ts | 8 ++++++-- src/utility/url.ts | 10 ++++++++++ 4 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 __tests__/string.test.ts create mode 100644 src/utility/url.ts diff --git a/__tests__/path.test.ts b/__tests__/path.test.ts index d8e25562..c441c6fb 100644 --- a/__tests__/path.test.ts +++ b/__tests__/path.test.ts @@ -15,11 +15,11 @@ import { } from '@/site/paths'; import { getCameraFromKey } from '@/camera'; -const PHOTO_ID = 'UsKSGcbt'; -const TAG = 'tag-name'; -const CAMERA = 'fujifilm-x-t1'; +const PHOTO_ID = 'UsKSGcbt'; +const TAG = 'tag-name'; +const CAMERA = 'fujifilm-x-t1'; const CAMERA_OBJECT = getCameraFromKey(CAMERA); -const SHARE = 'share'; +const SHARE = 'share'; const PATH_ROOT = '/'; const PATH_GRID = '/grid'; diff --git a/__tests__/string.test.ts b/__tests__/string.test.ts new file mode 100644 index 00000000..e71e3112 --- /dev/null +++ b/__tests__/string.test.ts @@ -0,0 +1,24 @@ +import { makeUrlAbsolute, shortenUrl } from '@/utility/url'; +import '@testing-library/jest-dom'; + +const URL_LONG_1 = 'https://www.example.com/'; +const URL_LONG_2 = 'https://www.example.com'; +const URL_LONG_3 = 'https://example.com/'; +const URL_LONG_4 = 'http://example.com/'; +const URL_LONG_5 = 'https://example.com'; +const URL_SHORT_1 = 'example.com'; +const URL_SHORT_2 = 'example.com/'; + +describe('String', () => { + it('url can be shortened', () => { + expect(shortenUrl(URL_LONG_1)).toBe(URL_SHORT_1); + expect(shortenUrl(URL_LONG_2)).toBe(URL_SHORT_1); + expect(shortenUrl(URL_LONG_3)).toBe(URL_SHORT_1); + expect(shortenUrl(URL_LONG_4)).toBe(URL_SHORT_1); + expect(shortenUrl(URL_SHORT_1)).toBe(URL_SHORT_1); + }); + it('url can be made absolute', () => { + expect(makeUrlAbsolute(URL_SHORT_1)).toBe(URL_LONG_5); + expect(makeUrlAbsolute(URL_SHORT_2)).toBe(URL_LONG_5); + }); +}); diff --git a/src/site/config.ts b/src/site/config.ts index 5f66d86d..96453e0e 100644 --- a/src/site/config.ts +++ b/src/site/config.ts @@ -1,3 +1,5 @@ +import { makeUrlAbsolute, shortenUrl } from '@/utility/url'; + export const SITE_TITLE = process.env.NEXT_PUBLIC_SITE_TITLE || 'Photo Blog'; @@ -12,8 +14,10 @@ const SITE_DOMAIN = process.env.NEXT_PUBLIC_SITE_DOMAIN || VERCEL_URL; +const SITE_DOMAIN_SHORT = shortenUrl(SITE_DOMAIN); + export const SITE_DOMAIN_OR_TITLE = - SITE_DOMAIN || + SITE_DOMAIN_SHORT || SITE_TITLE; export const SITE_DESCRIPTION = @@ -21,7 +25,7 @@ export const SITE_DESCRIPTION = SITE_DOMAIN; export const BASE_URL = process.env.NODE_ENV === 'production' - ? `https://${SITE_DOMAIN}` + ? makeUrlAbsolute(SITE_DOMAIN) : 'http://localhost:3000'; export const SHOW_REPO_LINK = process.env.NEXT_PUBLIC_HIDE_REPO_LINK !== '1'; diff --git a/src/utility/url.ts b/src/utility/url.ts new file mode 100644 index 00000000..c766ad90 --- /dev/null +++ b/src/utility/url.ts @@ -0,0 +1,10 @@ +// Remove protocol, www, and trailing slash from url +export const shortenUrl = (url?: string) => url + ? url.replace(/^(?:https?:\/\/)?(?:www\.)?/i, '').split('/')[0] + : undefined; + +// Add protocol to url and remove trailing slash +export const makeUrlAbsolute = (url = '') => + (!url.startsWith('http') + ? `https://${url}` + : url).replace(/\/$/, '');