« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util/urls.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/urls.js')
-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,
+  };
+}