« 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: f0947cb74d8736908149a522d1cd4aadb514d81c (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 {isExternalLinkContext, isExternalLinkStyle} from '#external-links';

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

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

  slots: {
    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',
    },

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

  generate(data, slots, {html, language}) {
    let formattedText =
      language.formatExternalLink(data.url, {
        style: slots.style,
        context: slots.context,
      });

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

    const link =
      html.tag('a', formattedText);

    link.attributes.set('href', data.url);
    link.attributes.set('class', 'nowrap');

    if (slots.tab === 'separate') {
      link.attributes.set('target', '_blank');
    }

    return link;
  },
};