« get me outta code hell

generatePageTitleText.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/generatePageTitleText.js
blob: 5482ca91198f8cf9b6ba9d527367a146ebb8662e (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
import striptags from 'striptags';

export default {
  sprawl: ({wikiInfo}) => ({wikiInfo}),

  data: (sprawl) => ({
    wikiName:
      sprawl.wikiInfo.nameShort,
  }),

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

    showWikiNameInTitle: {
      validate: v => v.is(true, false, 'auto'),
      default: 'auto',
    },

    subtitle: {
      type: 'html',
      mutable: false,
    },
  },

  generate: (data, slots, {html, language}) =>
    language.encapsulate('misc.pageTitle', workingCapsule => {
      const workingOptions = {};

      // Slightly jank: The output of striptags is, of course, a string,
      // and as far as language.formatString() is concerned, that means
      // it needs to be sanitized - including turning ampersands into
      // &'s. But the title is already HTML that has implicitly been
      // sanitized, however it got here, and includes HTML entities that
      // are properly escaped. Those need to get included as they are,
      // so we wrap the title in a tag and pass it off as good to go.
      workingOptions.title =
        html.tags([
striptags(slots.title.toString()),
        ]);

      if (!html.isBlank(slots.subtitle)) {
        // Same shenanigans here, as far as wrapping striptags goes.
        workingCapsule += '.withSubtitle';
        workingOptions.subtitle =
          html.tags([
            striptags(slots.subtitle.toString()),
          ]);
      }

      const showWikiName =
        (slots.showWikiNameInTitle === true
? true
       : slots.showWikiNameInTitle === 'auto'
? html.isBlank(slots.subtitle)
: false);

      if (showWikiName) {
        workingCapsule += '.withWikiName';
        workingOptions.wikiName = data.wikiName;
      }

      return language.$(workingCapsule, workingOptions);
    }),
};