« get me outta code hell

Merge pull request #134 from hsmusic/url-path-cleanup - 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:56:54 -0400
committerGitHub <noreply@github.com>2023-01-10 20:56:54 -0400
commit13c81e08805b1de24119c589872dc594af588dcf (patch)
treef23afc29f3e5da987bb9757d042f827e5fe50eb5 /src/util
parent41d35c9b3c22b20cf3291563d299b9d1f347927d (diff)
parent7f6d21d480e22765f438d5a80227f89c416b770b (diff)
Merge pull request #134 from hsmusic/url-path-cleanup
URL & path utilities cleanup
Diffstat (limited to 'src/util')
-rw-r--r--src/util/urls.js109
1 files changed, 46 insertions, 63 deletions
diff --git a/src/util/urls.js b/src/util/urls.js
index 4672c6a2..c2119b8d 100644
--- a/src/util/urls.js
+++ b/src/util/urls.js
@@ -140,18 +140,16 @@ export const thumb = {
 // Makes the generally-used and wiki-specialized "to" page utility.
 // "to" returns a relative path from the current page to the target.
 export function getURLsFrom({
-  urls,
-
   baseDirectory,
-  pageSubKey,
-  subdirectoryPrefix,
+  pagePath,
+  urls,
 }) {
+  const pageSubKey = pagePath[0];
+  const subdirectoryPrefix = getPageSubdirectoryPrefix({pagePath});
+
   return (targetFullKey, ...args) => {
     const [groupKey, subKey] = targetFullKey.split('.');
-    let path = subdirectoryPrefix;
-
-    let from;
-    let to;
+    let from, to;
 
     // When linking to *outside* the localized area of the site, we need to
     // make sure the result is correctly relative to the 8ase directory.
@@ -180,9 +178,9 @@ export function getURLsFrom({
       to = targetFullKey;
     }
 
-    path += urls.from(from).to(to, ...args);
-
-    return path;
+    return (
+      subdirectoryPrefix +
+      urls.from(from).to(to, ...args));
   };
 }
 
@@ -193,19 +191,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,61 +211,46 @@ export function getURLsFromRoot({
 export function getPagePathname({
   baseDirectory,
   device = false,
-  fullKey,
-  urlArgs,
+  pagePath,
   urls,
 }) {
-  const [groupKey, subKey] = fullKey.split('.');
-
-  const toKey = device ? 'toDevice' : 'to';
-
-  return (groupKey === 'localized' && baseDirectory
-    ? urls
-        .from('shared.root')[toKey](
-          'localizedWithBaseDirectory.' + subKey,
-          baseDirectory,
-          ...urlArgs)
-    : urls
-        .from('shared.root')[toKey](
-          fullKey,
-          ...urlArgs));
-}
+  const {[device ? 'toDevice' : 'to']: to} = urls.from('shared.root');
 
-// 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);
+  return (baseDirectory
+    ? to('localizedWithBaseDirectory.' + pagePath[0], baseDirectory, ...pagePath.slice(1))
+    : to('localized.' + pagePath[0], ...pagePath.slice(1)));
 }
 
-export function getPagePaths({
-  baseDirectory,
-  fullKey,
-  outputPath,
-  urlArgs,
+export function getPagePathnameAcrossLanguages({
+  defaultLanguage,
+  languages,
+  pagePath,
   urls,
 }) {
-  const [groupKey, subKey] = fullKey.split('.');
-
-  const pathname = getPagePathname({
-    baseDirectory,
-    device: true,
-    fullKey,
-    urlArgs,
-    urls,
-  });
-
-  const outputDirectory = path.join(outputPath, pathname);
-
-  const output = {
-    directory: outputDirectory,
-    documentHTML: path.join(outputDirectory, 'index.html'),
-    oEmbedJSON: path.join(outputDirectory, 'oembed.json'),
-  };
-
-  return {
-    urlPath: [fullKey, ...urlArgs],
+  return withEntries(languages, entries => entries
+    .filter(([key, language]) => key !== 'default' && !language.hidden)
+    .map(([_key, language]) => [
+      language.code,
+      getPagePathname({
+        baseDirectory:
+          (language === defaultLanguage
+            ? ''
+            : language.code),
+        pagePath,
+        urls,
+      }),
+    ]));
+}
 
-    output,
-    pathname,
-  };
+// 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({
+  pagePath,
+}) {
+  const timesNestedDeeply = (pagePath
+    .slice(1) // skip URL key, only check arguments
+    .join('/')
+    .split('/')
+    .length - 1);
+  return '../'.repeat(timesNestedDeeply);
 }