« get me outta code hell

linkTemplate.js « dependencies « content « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/content/dependencies/linkTemplate.js
blob: acac99be8b91da05da7abfc109cc1c2dbc933642 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import {empty} from '../../util/sugar.js';

export default {
  extraDependencies: [
    'appendIndexHTML',
    'getColors',
    'html',
    'to',
  ],

  generate({
    appendIndexHTML,
    getColors,
    html,
    to,
  }) {
    return html.template(slot =>
      slot('color', ([color]) =>
      slot('hash', ([hash]) =>
      slot('href', ([href]) =>
      slot('path', ([...path]) => {
        let style;

        if (!href && !empty(path)) {
          href = to(...path);
        }

        if (appendIndexHTML) {
          if (
            /^(?!https?:\/\/).+\/$/.test(href) &&
            href.endsWith('/')
          ) {
            href += 'index.html';
          }
        }

        if (hash) {
          href += (hash.startsWith('#') ? '' : '#') + hash;
        }

        if (color) {
          const {primary, dim} = getColors(color);
          style = `--primary-color: ${primary}; --dim-color: ${dim}`;
        }

        return slot('attributes', ([attributes]) =>
          html.tag('a',
            {
              ...attributes ?? {},
              href,
              style,
            },
            slot('content')));
      })))));
  },
}