« get me outta code hell

prefer passing around pagePath - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-01-10 20:20:18 -0400
committer(quasar) nebula <qznebula@protonmail.com>2023-01-10 20:20:18 -0400
commit54ad9946e3fa32ae8388d54ec8b3baad78a29417 (patch)
treebdf130e83a41ccf2f71a2de1c02e6189f355f0b3 /src/util
parentfd59ddd05bdfebf2f9a55a0fa5915485929e7de6 (diff)
prefer passing around pagePath
Diffstat (limited to 'src/util')
-rw-r--r--src/util/urls.js37
1 files changed, 22 insertions, 15 deletions
diff --git a/src/util/urls.js b/src/util/urls.js
index 0733ce04..1c7d1c58 100644
--- a/src/util/urls.js
+++ b/src/util/urls.js
@@ -143,9 +143,11 @@ export function getURLsFrom({
   urls,
 
   baseDirectory,
-  pageSubKey,
+  pagePath,
   subdirectoryPrefix,
 }) {
+  const pageSubKey = pagePath[0];
+
   return (targetFullKey, ...args) => {
     const [groupKey, subKey] = targetFullKey.split('.');
     let path = subdirectoryPrefix;
@@ -193,19 +195,19 @@ export function getURLsFromRoot({
   baseDirectory,
   urls,
 }) {
-  const from = urls.from('shared.root');
+  const {to} = urls.from('shared.root');
 
   return (targetFullKey, ...args) => {
     const [groupKey, subKey] = targetFullKey.split('.');
     return (
       '/' +
       (groupKey === 'localized' && baseDirectory
-        ? from.to(
+        ? to(
             'localizedWithBaseDirectory.' + subKey,
             baseDirectory,
             ...args
           )
-        : from.to(targetFullKey, ...args))
+        : to(targetFullKey, ...args))
     );
   };
 }
@@ -213,21 +215,20 @@ export function getURLsFromRoot({
 export function getPagePathname({
   baseDirectory,
   device = false,
-  pageSubKey,
-  urlArgs,
+  pagePath,
   urls,
 }) {
-  const to = urls.from('shared.root')[device ? 'toDevice' : 'to'];
+  const {[device ? 'toDevice' : 'to']: to} = urls.from('shared.root');
+
   return (baseDirectory
-    ? to('localizedWithBaseDirectory.' + pageSubKey, baseDirectory, ...urlArgs)
-    : to('localized.' + pageSubKey, ...urlArgs));
+    ? to('localizedWithBaseDirectory.' + pagePath[0], baseDirectory, ...pagePath.slice(1))
+    : to('localized.' + pagePath[0], ...pagePath.slice(1)));
 }
 
 export function getPagePathnameAcrossLanguages({
   defaultLanguage,
   languages,
-  pageSubKey,
-  urlArgs,
+  pagePath,
   urls,
 }) {
   return withEntries(languages, entries => entries
@@ -239,8 +240,7 @@ export function getPagePathnameAcrossLanguages({
           (language === defaultLanguage
             ? ''
             : language.code),
-        pageSubKey,
-        urlArgs,
+        pagePath,
         urls,
       }),
     ]));
@@ -248,6 +248,13 @@ export function getPagePathnameAcrossLanguages({
 
 // Needed for the rare path arguments which themselves contains one or more
 // slashes, e.g. for listings, with arguments like 'albums/by-name'.
-export function getPageSubdirectoryPrefix({urlArgs}) {
-  return '../'.repeat(urlArgs.join('/').split('/').length - 1);
+export function getPageSubdirectoryPrefix({
+  pagePath,
+}) {
+  const timesNestedDeeply = (pagePath
+    .slice(1) // skip URL key, only check arguments
+    .join('/')
+    .split('/')
+    .length - 1);
+  return '../'.repeat(timesNestedDeeply);
 }