« get me outta code hell

generateContributionTooltipExternalLinkSection.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/generateContributionTooltipExternalLinkSection.js
blob: 4f9a23ed02a237b60ee88f4317d11bc3b20cf16b (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import {stitchArrays} from '#sugar';

export default {
  contentDependencies: [
    'generateExternalHandle',
    'generateExternalIcon',
    'generateExternalPlatform',
  ],

  extraDependencies: ['html', 'language'],

  relations: (relation, contribution) => ({
    icons:
      contribution.artist.urls
        .map(url => relation('generateExternalIcon', url)),

    handles:
      contribution.artist.urls
        .map(url => relation('generateExternalHandle', url)),

    platforms:
      contribution.artist.urls
        .map(url => relation('generateExternalPlatform', url)),
  }),

  data: (contribution) => ({
    urls: contribution.artist.urls,
  }),

  generate: (data, relations, {html, language}) =>
    language.encapsulate('misc.artistLink', capsule =>
      html.tags(
        stitchArrays({
          icon: relations.icons,
          handle: relations.handles,
          platform: relations.platforms,
          url: data.urls,
        }).map(({icon, handle, platform, url}) => {
            for (const template of [icon, handle, platform]) {
              template.setSlot('context', 'artist');
            }

            return [
              html.tag('a', {class: 'external-link'},
                {href: url},

                [
                  icon,

                  html.tag('span', {class: 'external-handle'},
                    (html.isBlank(handle)
                      ? platform
                      : handle)),
                ]),

              html.tag('span', {class: 'external-platform'},
                // This is a pretty ridiculous hack, but we currently
                // don't have a way of telling formatExternalLink to *not*
                // use the fallback string, which just formats the URL as
                // its host/domain... so is technically detectable.
                (((new URL(url))
                    .host
                    .endsWith(
                      html.resolve(platform, {normalize: 'string'})))

                  ? language.$(capsule, 'noExternalLinkPlatformName')
                  : platform)),
            ];
          }))),
};