« get me outta code hell

be a bit more careful with to() path resolving - 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>2022-01-02 10:57:22 -0400
committer(quasar) nebula <qznebula@protonmail.com>2022-01-02 10:57:22 -0400
commit22e1c9e1f57a9d2252b6bc614ca8abb805721d4d (patch)
tree73fedbe63e266f3e9c7470befc3ab6868af7877f
parentc2428d2a136d2af4ed7235240308082db32cb3f8 (diff)
be a bit more careful with to() path resolving
-rwxr-xr-xsrc/upd8.js2
-rw-r--r--src/util/urls.js17
2 files changed, 13 insertions, 6 deletions
diff --git a/src/upd8.js b/src/upd8.js
index daef145..12e301c 100755
--- a/src/upd8.js
+++ b/src/upd8.js
@@ -1792,7 +1792,7 @@ writePage.html = (pageFn, {
     footer.content ??= (wikiInfo.footer ? transformMultiline(wikiInfo.footer) : '');
 
     const canonical = (wikiInfo.canonicalBase
-        ? wikiInfo.canonicalBase + paths.pathname
+        ? wikiInfo.canonicalBase + (paths.pathname === '/' ? '' : paths.pathanme)
         : '');
 
     const collapseSidebars = (sidebarLeft.collapse !== false) && (sidebarRight.collapse !== false);
diff --git a/src/util/urls.js b/src/util/urls.js
index a30cc81..e15c018 100644
--- a/src/util/urls.js
+++ b/src/util/urls.js
@@ -34,25 +34,32 @@ export function generateURLs(urlSpec) {
         };
     };
 
+    // This should be called on values which are going to be passed to
+    // path.relative, because relative will resolve a leading slash as the root
+    // directory of the working device, which we aren't looking for here.
+    const trimLeadingSlash = P => P.startsWith('/') ? P.slice(1) : P;
+
     const generateTo = (fromPath, fromGroup) => {
+        const A = trimLeadingSlash(fromPath);
+
         const rebasePrefix = '../'.repeat((fromGroup.prefix || '').split('/').filter(Boolean).length);
 
         const pathHelper = (toPath, toGroup) => {
-            let target = (toPath === '/' ? '' : toPath);
+            let B = trimLeadingSlash(toPath);
 
             let argIndex = 0;
-            target = target.replaceAll('<>', () => `<${argIndex++}>`);
+            B = B.replaceAll('<>', () => `<${argIndex++}>`);
 
             if (toGroup.prefix !== fromGroup.prefix) {
                 // TODO: Handle differing domains in prefixes.
-                target = rebasePrefix + (toGroup.prefix || '') + target;
+                B = rebasePrefix + (toGroup.prefix || '') + B;
             }
 
             const suffix = (toPath.endsWith('/') ? '/' : '');
 
             return {
-                posix: path.posix.relative(fromPath, target) + suffix,
-                device: path.relative(fromPath, target) + suffix
+                posix: path.posix.relative(A, B) + suffix,
+                device: path.relative(A, B) + suffix
             };
         };