« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/content/dependencies/linkTemplate.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/content/dependencies/linkTemplate.js')
-rw-r--r--src/content/dependencies/linkTemplate.js73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/content/dependencies/linkTemplate.js b/src/content/dependencies/linkTemplate.js
new file mode 100644
index 0000000..9109ab5
--- /dev/null
+++ b/src/content/dependencies/linkTemplate.js
@@ -0,0 +1,73 @@
+import {empty} from '../../util/sugar.js';
+
+export default {
+  extraDependencies: [
+    'appendIndexHTML',
+    'getColors',
+    'html',
+    'to',
+  ],
+
+  generate({
+    appendIndexHTML,
+    getColors,
+    html,
+    to,
+  }) {
+    return html.template({
+      annotation: 'linkTemplate',
+
+      slots: {
+        href: {type: 'string'},
+        path: {validate: v => v.validateArrayItems(v.isString)},
+        hash: {type: 'string'},
+
+        tooltip: {validate: v => v.isString},
+        attributes: {validate: v => v.isAttributes},
+        color: {validate: v => v.isColor},
+        content: {type: 'html'},
+      },
+
+      content(slots) {
+        let href = slots.href;
+        let style;
+        let title;
+
+        if (!href && !empty(slots.path)) {
+          href = to(...slots.path);
+        }
+
+        if (appendIndexHTML) {
+          if (
+            /^(?!https?:\/\/).+\/$/.test(href) &&
+            href.endsWith('/')
+          ) {
+            href += 'index.html';
+          }
+        }
+
+        if (slots.hash) {
+          href += (slots.hash.startsWith('#') ? '' : '#') + slots.hash;
+        }
+
+        if (slots.color) {
+          const {primary, dim} = getColors(slots.color);
+          style = `--primary-color: ${primary}; --dim-color: ${dim}`;
+        }
+
+        if (slots.tooltip) {
+          title = slots.tooltip;
+        }
+
+        return html.tag('a',
+          {
+            ...slots.attributes ?? {},
+            href,
+            style,
+            title,
+          },
+          slots.content);
+      },
+    });
+  },
+}