« 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
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
parentfd59ddd05bdfebf2f9a55a0fa5915485929e7de6 (diff)
prefer passing around pagePath
-rw-r--r--src/misc-templates.js13
-rw-r--r--src/util/urls.js37
-rw-r--r--src/write/build-modes/live-dev-server.js16
-rw-r--r--src/write/build-modes/static-build.js26
-rw-r--r--src/write/page-template.js11
5 files changed, 47 insertions, 56 deletions
diff --git a/src/misc-templates.js b/src/misc-templates.js
index 6659d72..794b692 100644
--- a/src/misc-templates.js
+++ b/src/misc-templates.js
@@ -973,10 +973,8 @@ function unbound_getFooterLocalizationLinks(pathname, {
   defaultLanguage,
   language,
   languages,
+  pagePath,
   to,
-
-  pageSubKey,
-  urlArgs,
 }) {
   const links = Object.entries(languages)
     .filter(([code, language]) => code !== 'default' && !language.hidden)
@@ -989,11 +987,12 @@ function unbound_getFooterLocalizationLinks(pathname, {
             href:
               language === defaultLanguage
                 ? to(
-                    'localizedDefaultLanguage.' + pageSubKey,
-                    ...urlArgs)
+                    'localizedDefaultLanguage.' + pagePath[0],
+                    ...pagePath.slice(1))
                 : to(
-                    'localizedWithBaseDirectory.' + pageSubKey,
-                    language.code, ...urlArgs),
+                    'localizedWithBaseDirectory.' + pagePath[0],
+                    language.code,
+                    ...pagePath.slice(1)),
           },
           language.name)));
 
diff --git a/src/util/urls.js b/src/util/urls.js
index 0733ce0..1c7d1c5 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);
 }
diff --git a/src/write/build-modes/live-dev-server.js b/src/write/build-modes/live-dev-server.js
index bdd4561..ec9689b 100644
--- a/src/write/build-modes/live-dev-server.js
+++ b/src/write/build-modes/live-dev-server.js
@@ -89,8 +89,7 @@ export async function go({
 
         const pathname = getPagePathname({
           baseDirectory,
-          pageSubKey: servePath[0],
-          urlArgs: servePath.slice(1),
+          pagePath: servePath,
           urls,
         });
 
@@ -248,9 +247,9 @@ export async function go({
     const to = getURLsFrom({
       urls,
       baseDirectory,
-      pageSubKey: servePath[0],
+      pagePath: servePath,
       subdirectoryPrefix: getPageSubdirectoryPrefix({
-        urlArgs: servePath.slice(1),
+        pagePath: servePath,
       }),
     });
 
@@ -260,9 +259,6 @@ export async function go({
     });
 
     try {
-      const pageSubKey = servePath[0];
-      const urlArgs = servePath.slice(1);
-
       if (page.type === 'redirect') {
         response.writeHead(301, contentTypeHTML);
 
@@ -280,8 +276,7 @@ export async function go({
       const localizedPathnames = getPagePathnameAcrossLanguages({
         defaultLanguage,
         languages,
-        pageSubKey,
-        urlArgs,
+        pagePath: servePath,
         urls,
       });
 
@@ -305,9 +300,8 @@ export async function go({
         languages,
         localizedPathnames,
         oEmbedJSONHref: null, // No oEmbed support for live dev server
-        pageSubKey,
+        pagePath: servePath,
         pathname,
-        urlArgs,
         to,
         transformMultiline: bound.transformMultiline,
         wikiData,
diff --git a/src/write/build-modes/static-build.js b/src/write/build-modes/static-build.js
index edfb02c..5141891 100644
--- a/src/write/build-modes/static-build.js
+++ b/src/write/build-modes/static-build.js
@@ -265,30 +265,27 @@ export async function go({
 
     await progressPromiseAll(`Writing ${language.code}`, queue([
       ...pageWrites.map(page => () => {
-        const pageSubKey = page.path[0];
-        const urlArgs = page.path.slice(1);
+        const pagePath = page.path;
 
         const localizedPathnames = getPagePathnameAcrossLanguages({
           defaultLanguage,
           languages,
-          pageSubKey,
-          urlArgs,
+          pagePath,
           urls,
         });
 
         const pathname = getPagePathname({
           baseDirectory,
-          pageSubKey,
-          urlArgs,
+          pagePath,
           urls,
         });
 
         const to = getURLsFrom({
           urls,
           baseDirectory,
-          pageSubKey,
+          pagePath,
           subdirectoryPrefix: getPageSubdirectoryPrefix({
-            urlArgs: page.path.slice(1),
+            pagePath,
           }),
         });
 
@@ -330,11 +327,10 @@ export async function go({
           languages,
           localizedPathnames,
           oEmbedJSONHref,
-          pageSubKey,
+          pagePath,
           pathname,
           to,
           transformMultiline: bound.transformMultiline,
-          urlArgs,
           wikiData,
         });
 
@@ -344,8 +340,7 @@ export async function go({
           outputDirectory: path.join(outputPath, getPagePathname({
             baseDirectory,
             device: true,
-            pageSubKey,
-            urlArgs,
+            pagePath,
             urls,
           })),
         });
@@ -358,9 +353,9 @@ export async function go({
         const to = getURLsFrom({
           urls,
           baseDirectory,
-          pageSubKey: fromPath[0],
+          pagePath: fromPath,
           subdirectoryPrefix: getPageSubdirectoryPrefix({
-            urlArgs: fromPath.slice(1),
+            pagePath: fromPath,
           }),
         });
 
@@ -372,8 +367,7 @@ export async function go({
           outputDirectory: path.join(outputPath, getPagePathname({
             baseDirectory,
             device: true,
-            pageSubKey: fromPath[0],
-            urlArgs: fromPath.slice(1),
+            pagePath: fromPath,
             urls,
           })),
         });
diff --git a/src/write/page-template.js b/src/write/page-template.js
index 88d81c2..7c2f0c6 100644
--- a/src/write/page-template.js
+++ b/src/write/page-template.js
@@ -55,11 +55,10 @@ export function generateDocumentHTML(pageInfo, {
   languages,
   localizedPathnames,
   oEmbedJSONHref,
-  pageSubKey,
+  pagePath,
   pathname,
   to,
   transformMultiline,
-  urlArgs,
   wikiData,
 }) {
   const {wikiInfo} = wikiData;
@@ -169,9 +168,8 @@ export function generateDocumentHTML(pageInfo, {
           html,
           language,
           languages,
-          pageSubKey,
+          pagePath,
           to,
-          urlArgs,
         }),
       ]);
 
@@ -450,10 +448,9 @@ export function generateDocumentHTML(pageInfo, {
     {
       lang: language.intlCode,
       'data-language-code': language.code,
-      'data-url-key': 'localized.' + pageSubKey,
+      'data-url-key': 'localized.' + pagePath[0],
       ...Object.fromEntries(
-        urlArgs.map((v, i) => [['data-url-value' + i], v])
-      ),
+        pagePath.slice(1).map((v, i) => [['data-url-value' + i], v])),
       'data-rebase-localized': to('localized.root'),
       'data-rebase-shared': to('shared.root'),
       'data-rebase-media': to('media.root'),