« get me outta code hell

generateSocialEmbed.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/generateSocialEmbed.js
blob: 115dc262bb592ee77f8b621c06c00371326e41d4 (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
import {getOrigin} from '#urls';

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

  sprawl({wikiInfo}) {
    return {
      canonicalBase: wikiInfo.canonicalBase,
      shortWikiName: wikiInfo.nameShort,
    };
  },

  data(sprawl) {
    return {
      canonicalBase: sprawl.canonicalBase,
      shortWikiName: sprawl.shortWikiName,
    };
  },

  slots: {
    mode: {validate: v => v.is('html', 'json')},

    title: {type: 'string'},
    description: {type: 'string'},

    headingContent: {type: 'string'},
    headingLink: {type: 'string'},
    imagePath: {validate: v => v.strictArrayOf(v.isString)},
  },

  generate(data, slots, {html, language, urls}) {
    switch (slots.mode) {
      case 'html':
        return html.tags([
          slots.title &&
            html.tag('meta', {property: 'og:title', content: slots.title}),

          slots.description &&
            html.tag('meta', {
              property: 'og:description',
              content: slots.description,
            }),

          slots.imagePath &&
            html.tag('meta', {
              property: 'og:image',
              content:
                (() => {
                  const toResult =
                    urls
                      .from('shared.root')
                      .to(...slots.imagePath);

                  if (getOrigin(toResult)) {
                    return toResult;
                  } else {
                    return '/' + toResult;
                  }
                })(),
            }),
        ]);

      case 'json':
        return JSON.stringify({
          author_name:
            (slots.headingContent
              ? html.resolve(
                  language.$('misc.socialEmbed.heading', {
                    wikiName: data.shortWikiName,
                    heading: slots.headingContent,
                  }),
                  {normalize: 'string'})
              : undefined),

          author_url:
            (slots.headingLink && data.canonicalBase
              ? data.canonicalBase.replace(/\/$/, '') +
                '/' +
                slots.headingLink.replace(/^\//, '')
              : undefined),
        });
    }
  },
};