« get me outta code hell

new --append-index-html to dev w/o server - 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>2021-12-31 21:17:35 -0400
committer(quasar) nebula <qznebula@protonmail.com>2021-12-31 21:17:35 -0400
commit3fa4eaf8f1e400bc420f1365da26f48f8dfe37f5 (patch)
treed65ebdfe60b43805b620b387d2c6489a020b3dd0
parent5b1418b417c2bac641a8671e7da5cf02aefa6245 (diff)
new --append-index-html to dev w/o server
-rwxr-xr-xsrc/upd8.js16
-rw-r--r--src/util/link.js31
2 files changed, 42 insertions, 5 deletions
diff --git a/src/upd8.js b/src/upd8.js
index b31505d..c9fc1d8 100755
--- a/src/upd8.js
+++ b/src/upd8.js
@@ -2233,12 +2233,20 @@ async function main() {
             type: 'flag'
         },
 
-        // Only want 8uild one language during testing? This can chop down
+        // Only want to 8uild one language during testing? This can chop down
         // 8uild times a pretty 8ig chunk! Just pass a single language code.
         'lang': {
             type: 'value'
         },
 
+        // Working without a dev server and just using file:// URLs in your we8
+        // 8rowser? This will automatically append index.html to links across
+        // the site. Not recommended for production, since it isn't guaranteed
+        // 100% error-free (and index.html-style links are less pretty anyway).
+        'append-index-html': {
+            type: 'flag'
+        },
+
         'queue-size': {
             type: 'value',
             validate(size) {
@@ -2275,6 +2283,12 @@ async function main() {
         }
     }
 
+    const appendIndexHTML = miscOptions['append-index-html'] ?? false;
+    if (appendIndexHTML) {
+        logWarn`Appending index.html to link hrefs. (Note: not recommended for production release!)`;
+        unbound_link.globalOptions.appendIndexHTML = true;
+    }
+
     const skipThumbs = miscOptions['skip-thumbs'] ?? false;
     const thumbsOnly = miscOptions['thumbs-only'] ?? false;
 
diff --git a/src/util/link.js b/src/util/link.js
index 7ed5fd8..4e611df 100644
--- a/src/util/link.js
+++ b/src/util/link.js
@@ -19,6 +19,8 @@ export function getLinkThemeString(color) {
     return `--primary-color: ${primary}; --dim-color: ${dim}`;
 }
 
+const appendIndexHTMLRegex = /^(?!https?:\/\/).+\/$/;
+
 const linkHelper = (hrefFn, {color = true, attr = null} = {}) =>
     (thing, {
         to,
@@ -27,18 +29,30 @@ const linkHelper = (hrefFn, {color = true, attr = null} = {}) =>
         class: className = '',
         color: color2 = true,
         hash = ''
-    }) => (
-        html.tag('a', {
+    }) => {
+        let href = hrefFn(thing, {to});
+
+        if (link.globalOptions.appendIndexHTML) {
+            if (appendIndexHTMLRegex.test(href)) {
+                href += 'index.html';
+            }
+        }
+
+        if (hash) {
+            href += (hash.startsWith('#') ? '' : '#') + hash;
+        }
+
+        return html.tag('a', {
             ...attr ? attr(thing) : {},
             ...attributes ? attributes : {},
-            href: hrefFn(thing, {to}) + (hash ? (hash.startsWith('#') ? '' : '#') + hash : ''),
+            href,
             style: (
                 typeof color2 === 'string' ? getLinkThemeString(color2) :
                 color2 && color ? getLinkThemeString(thing.color) :
                 ''),
             class: className
         }, text || thing.name)
-    );
+    };
 
 const linkDirectory = (key, {expose = null, attr = null, ...conf} = {}) =>
     linkHelper((thing, {to}) => to('localized.' + key, thing.directory), {
@@ -53,6 +67,15 @@ const linkPathname = (key, conf) => linkHelper(({directory: pathname}, {to}) =>
 const linkIndex = (key, conf) => linkHelper((_, {to}) => to('localized.' + key), conf);
 
 const link = {
+    globalOptions: {
+        // This should usually only 8e used during development! It'll take any
+        // href that ends with `/` and append `index.html` to the returned
+        // value (for to.thing() functions). This is handy when developing
+        // without a local server (i.e. using file:// protocol URLs in your
+        // 8rowser), 8ut isn't guaranteed to 8e 100% 8ug-free.
+        appendIndexHTML: false
+    },
+
     album: linkDirectory('album'),
     albumCommentary: linkDirectory('albumCommentary'),
     artist: linkDirectory('artist', {color: false}),