« get me outta code hell

linkExternal.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/linkExternal.js
blob: 39a593b2666874932c1b156282594ded5d010450 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import {isExternalLinkContext, isExternalLinkStyle} from '#external-links';

export default {
  extraDependencies: ['html', 'language', 'wikiData'],

  data: (url) => ({url}),

  slots: {
    content: {
      type: 'html',
      mutable: false,
    },

    style: {
      // This awkward syntax is because the slot descriptor validator can't
      // differentiate between a function that returns a validator (the usual
      // syntax) and a function that is itself a validator.
      validate: () => isExternalLinkStyle,
      default: 'platform',
    },

    context: {
      validate: () => isExternalLinkContext,
      default: 'generic',
    },

    indicateExternal: {
      type: 'boolean',
      default: false,
    },

    tab: {
      validate: v => v.is('default', 'separate'),
      default: 'default',
    },
  },

  generate(data, slots, {html, language}) {
    let urlIsValid;
    try {
      new URL(data.url);
      urlIsValid = true;
    } catch (error) {
      urlIsValid = false;
    }

    let formattedLink;
    if (urlIsValid) {
      formattedLink =
        language.formatExternalLink(data.url, {
          style: slots.style,
          context: slots.context,
        });

      // Fall back to platform if nothing matched the desired style.
      if (html.isBlank(formattedLink) && slots.style !== 'platform') {
        formattedLink =
          language.formatExternalLink(data.url, {
            style: 'platform',
            context: slots.context,
          });
      }
    } else {
      formattedLink = null;
    }

    const linkAttributes = html.attributes({
      class: 'external-link',
    });

    let linkContent;
    if (urlIsValid) {
      linkAttributes.set('href', data.url);

      if (html.isBlank(slots.content)) {
        linkContent = formattedLink;
      } else {
        linkContent = slots.content;
      }
    } else {
      if (html.isBlank(slots.content)) {
        linkContent =
          html.tag('i',
            language.$('misc.external.invalidURL.annotation'));
      } else {
        linkContent =
          language.$('misc.external.invalidURL', {
            link: slots.content,
            annotation:
              html.tag('i',
                language.$('misc.external.invalidURL.annotation')),
          });
      }
    }

    if (urlIsValid && slots.indicateExternal) {
      linkAttributes.add('class', 'indicate-external');

      let titleText;
      if (slots.tab === 'separate') {
        if (html.isBlank(slots.content)) {
          titleText =
            language.$('misc.external.opensInNewTab.annotation');
        } else {
          titleText =
            language.$('misc.external.opensInNewTab', {
              link: formattedLink,
              annotation:
                language.$('misc.external.opensInNewTab.annotation'),
            });
        }
      } else if (!html.isBlank(slots.content)) {
        titleText = formattedLink;
      }

      if (titleText) {
        linkAttributes.set('title', titleText.toString());
      }
    }

    if (urlIsValid && slots.tab === 'separate') {
      linkAttributes.set('target', '_blank');
    }

    return html.tag('a', linkAttributes, linkContent);
  },
};