From f19fc78a151c761642cf7c9b654e0da9db3c4418 Mon Sep 17 00:00:00 2001 From: Joe Carstairs <65492573+Sycamost@users.noreply.github.com> Date: Sun, 13 Aug 2023 20:33:03 +0100 Subject: More robust relative path handling --- src/components/LallansIssue.astro | 5 ++++- src/components/LallansIssuesGallery.astro | 12 +++++++++++- src/components/Scotsoun.astro | 5 ++++- src/components/ScotsounCdGallery.astro | 12 +++++++++++- src/lib/relativePath.ts | 12 ++++++++++++ src/pages/[locale]/furthsettins/index.astro | 5 +++-- .../lallans/[issueNumber]/payment-success-confirmation.astro | 2 +- src/pages/[locale]/furthsettins/lallans/index.astro | 2 +- .../scotsoun/[scotsounId]/payment-success-confirmation.astro | 2 +- src/pages/[locale]/furthsettins/scotsoun/index.astro | 2 +- 10 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 src/lib/relativePath.ts (limited to 'src') diff --git a/src/components/LallansIssue.astro b/src/components/LallansIssue.astro index c71755b..a07d345 100644 --- a/src/components/LallansIssue.astro +++ b/src/components/LallansIssue.astro @@ -1,6 +1,7 @@ --- import translate from '$i18n/translate'; import tSite from '$i18n/translations/site'; +import relativePath from '$lib/relativePath'; import type LallansIssue from '$types/LallansIssue'; import type Locale from '$types/Locale'; import LallansIssueContributions from './LallansIssueContributions.astro'; @@ -25,7 +26,9 @@ const t = translate(locale);
- +
diff --git a/src/components/LallansIssuesGallery.astro b/src/components/LallansIssuesGallery.astro index bee50c8..c784156 100644 --- a/src/components/LallansIssuesGallery.astro +++ b/src/components/LallansIssuesGallery.astro @@ -1,5 +1,12 @@ --- import { getAllLallansIssues } from '$data/lallans'; +import type Locale from '$types/Locale'; + +interface Props { + locale: Locale; +} + +const { locale } = Astro.props; const issues = await getAllLallansIssues(); --- @@ -8,7 +15,10 @@ const issues = await getAllLallansIssues(); { issues.map((issue) => (
  • - + diff --git a/src/components/Scotsoun.astro b/src/components/Scotsoun.astro index 13c6fa9..1491ebc 100644 --- a/src/components/Scotsoun.astro +++ b/src/components/Scotsoun.astro @@ -5,6 +5,7 @@ import tComponent from '$i18n/translations/components/scotsoun'; import type Locale from '$types/Locale'; import type Scotsoun from '$types/Scotsoun'; import PayPalButtons from './PayPalButtons.astro'; +import relativePath from '$lib/relativePath'; interface Props { locale: Locale; @@ -30,7 +31,9 @@ const t = translate(locale);
    - +
    diff --git a/src/components/ScotsounCdGallery.astro b/src/components/ScotsounCdGallery.astro index 12bafb0..6f6c794 100644 --- a/src/components/ScotsounCdGallery.astro +++ b/src/components/ScotsounCdGallery.astro @@ -1,5 +1,12 @@ --- import { getAllScotsoun } from '$data/scotsoun'; +import type Locale from '$types/Locale'; + +interface Props { + locale: Locale; +} + +const { locale } = Astro.props; const scotsoun = await getAllScotsoun(); --- @@ -8,7 +15,10 @@ const scotsoun = await getAllScotsoun(); { scotsoun.map((scotsounItem) => (
  • - + diff --git a/src/lib/relativePath.ts b/src/lib/relativePath.ts new file mode 100644 index 0000000..ad8582c --- /dev/null +++ b/src/lib/relativePath.ts @@ -0,0 +1,12 @@ +export default function (currentPath: string, relativePath: string) { + if (relativePath.length > 2 && relativePath.slice(0, 3) === '../') { + throw new Error('Cannot calculate relative path because it goes up a directory.'); + } + + if (currentPath.at(-1) !== '/') { + currentPath = currentPath + '/'; + } + relativePath = relativePath.replace(/^\.?\//, ''); + + return currentPath + relativePath; +} diff --git a/src/pages/[locale]/furthsettins/index.astro b/src/pages/[locale]/furthsettins/index.astro index d7a9421..43ff28e 100644 --- a/src/pages/[locale]/furthsettins/index.astro +++ b/src/pages/[locale]/furthsettins/index.astro @@ -5,6 +5,7 @@ import translate from '$i18n/translate'; import tFurthsettins from '$i18n/translations/pages/furthsettins'; import tSite from '$i18n/translations/site'; import type Locale from '$types/Locale'; +import relativePath from '$lib/relativePath'; export async function getStaticPaths() { return localeStaticPaths; @@ -20,7 +21,7 @@ const t = translate(locale);