« get me outta code hell

extract getPagePaths, getURLsFrom functions - 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-07 20:31:16 -0400
committer(quasar) nebula <qznebula@protonmail.com>2023-01-07 20:35:07 -0400
commit1ced8788ca64ed430fac003dc9281f7194193956 (patch)
treecdbecc33b8b08c41dabd9ab3dd7b42b006e693d7 /src/util
parentd59ed9687eb8e38aac3ca7dd5b190ba78b3b5556 (diff)
extract getPagePaths, getURLsFrom functions
towards basic dynamics pt. 1 (#124)
Diffstat (limited to 'src/util')
-rw-r--r--src/util/urls.js91
1 files changed, 91 insertions, 0 deletions
diff --git a/src/util/urls.js b/src/util/urls.js
index 1f9cd9c0..f05f134b 100644
--- a/src/util/urls.js
+++ b/src/util/urls.js
@@ -136,3 +136,94 @@ export const thumb = {
   medium: thumbnailHelper('.medium'),
   small: thumbnailHelper('.small'),
 };
+
+export function getURLsFrom({
+  urls,
+
+  baseDirectory,
+  pageSubKey,
+  paths,
+}) {
+  return (targetFullKey, ...args) => {
+    const [groupKey, subKey] = targetFullKey.split('.');
+    let path = paths.subdirectoryPrefix;
+
+    let from;
+    let 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.
+    if (
+      groupKey !== 'localized' &&
+      groupKey !== 'localizedDefaultLanguage' &&
+      baseDirectory
+    ) {
+      from = 'localizedWithBaseDirectory.' + pageSubKey;
+      to = targetFullKey;
+    } else if (groupKey === 'localizedDefaultLanguage' && baseDirectory) {
+      // Special case for specifically linking *from* a page with base
+      // directory *to* a page without! Used for the language switcher and
+      // hopefully nothing else oh god.
+      from = 'localizedWithBaseDirectory.' + pageSubKey;
+      to = 'localized.' + subKey;
+    } else if (groupKey === 'localizedDefaultLanguage') {
+      // Linking to the default, except surprise, we're already IN the default
+      // (no baseDirectory set).
+      from = 'localized.' + pageSubKey;
+      to = 'localized.' + subKey;
+    } else {
+      // If we're linking inside the localized area (or there just is no
+      // 8ase directory), the 8ase directory doesn't matter.
+      from = 'localized.' + pageSubKey;
+      to = targetFullKey;
+    }
+
+    path += urls.from(from).to(to, ...args);
+
+    return path;
+  };
+}
+
+export function getPagePaths({
+  outputPath,
+  urls,
+
+  baseDirectory,
+  fullKey,
+  urlArgs,
+}) {
+  const [groupKey, subKey] = fullKey.split('.');
+
+  const pathname =
+    groupKey === 'localized' && baseDirectory
+      ? urls
+          .from('shared.root')
+          .toDevice(
+            'localizedWithBaseDirectory.' + subKey,
+            baseDirectory,
+            ...urlArgs)
+      : urls
+          .from('shared.root')
+          .toDevice(fullKey, ...urlArgs);
+
+  // Needed for the rare path arguments which themselves contains one or more
+  // slashes, e.g. for listings, with arguments like 'albums/by-name'.
+  const subdirectoryPrefix =
+    '../'.repeat(urlArgs.join('/').split('/').length - 1);
+
+  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],
+
+    output,
+    pathname,
+    subdirectoryPrefix,
+  };
+}