« get me outta code hell

album-commentary.js « page « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/page/album-commentary.js
blob: 8a5ba0691f0d5645f95a2213ef82f6b32a536209 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/** @format */

// Album commentary page and index specifications.

// Imports

import fixWS from 'fix-whitespace';

import {filterAlbumsByCommentary} from '../util/wiki-data.js';

// Page exports

export function condition({wikiData}) {
  return filterAlbumsByCommentary(wikiData.albumData).length;
}

export function targets({wikiData}) {
  return filterAlbumsByCommentary(wikiData.albumData);
}

export function write(album, {wikiData}) {
  const {wikiInfo} = wikiData;

  const entries = [album, ...album.tracks]
    .filter((x) => x.commentary)
    .map((x) => x.commentary);
  const words = entries.join(' ').split(' ').length;

  const page = {
    type: 'page',
    path: ['albumCommentary', album.directory],
    page: ({
      getAlbumStylesheet,
      getLinkThemeString,
      getThemeString,
      link,
      language,
      to,
      transformMultiline,
    }) => ({
      title: language.$('albumCommentaryPage.title', {album: album.name}),
      stylesheet: getAlbumStylesheet(album),
      theme: getThemeString(album.color),

      main: {
        content: fixWS`
                    <div class="long-content">
                        <h1>${language.$('albumCommentaryPage.title', {
                          album: link.album(album),
                        })}</h1>
                        <p>${language.$('albumCommentaryPage.infoLine', {
                          words: `<b>${language.formatWordCount(words, {
                            unit: true,
                          })}</b>`,
                          entries: `<b>${language.countCommentaryEntries(
                            entries.length,
                            {
                              unit: true,
                            }
                          )}</b>`,
                        })}</p>
                        ${
                          album.commentary &&
                          fixWS`
                            <h3>${language.$(
                              'albumCommentaryPage.entry.title.albumCommentary'
                            )}</h3>
                            <blockquote>
                                ${transformMultiline(album.commentary)}
                            </blockquote>
                        `
                        }
                        ${album.tracks
                          .filter((t) => t.commentary)
                          .map(
                            (track) => fixWS`
                            <h3 id="${track.directory}">${language.$(
                              'albumCommentaryPage.entry.title.trackCommentary',
                              {
                                track: link.track(track),
                              }
                            )}</h3>
                            <blockquote style="${getLinkThemeString(
                              track.color
                            )}">
                                ${transformMultiline(track.commentary)}
                            </blockquote>
                        `
                          )
                          .join('\n')}
                    </div>
                `,
      },

      nav: {
        linkContainerClasses: ['nav-links-hierarchy'],
        links: [
          {toHome: true},
          {
            path: ['localized.commentaryIndex'],
            title: language.$('commentaryIndex.title'),
          },
          {
            html: language.$('albumCommentaryPage.nav.album', {
              album: link.albumCommentary(album, {class: 'current'}),
            }),
          },
        ],
      },
    }),
  };

  return [page];
}

export function writeTargetless({wikiData}) {
  const data = filterAlbumsByCommentary(wikiData.albumData)
    .map((album) => ({
      album,
      entries: [album, ...album.tracks]
        .filter((x) => x.commentary)
        .map((x) => x.commentary),
    }))
    .map(({album, entries}) => ({
      album,
      entries,
      words: entries.join(' ').split(' ').length,
    }));

  const totalEntries = data.reduce((acc, {entries}) => acc + entries.length, 0);
  const totalWords = data.reduce((acc, {words}) => acc + words, 0);

  const page = {
    type: 'page',
    path: ['commentaryIndex'],
    page: ({link, language}) => ({
      title: language.$('commentaryIndex.title'),

      main: {
        content: fixWS`
                    <div class="long-content">
                        <h1>${language.$('commentaryIndex.title')}</h1>
                        <p>${language.$('commentaryIndex.infoLine', {
                          words: `<b>${language.formatWordCount(totalWords, {
                            unit: true,
                          })}</b>`,
                          entries: `<b>${language.countCommentaryEntries(
                            totalEntries,
                            {
                              unit: true,
                            }
                          )}</b>`,
                        })}</p>
                        <p>${language.$('commentaryIndex.albumList.title')}</p>
                        <ul>
                            ${data
                              .map(
                                ({album, entries, words}) => fixWS`
                                    <li>${language.$(
                                      'commentaryIndex.albumList.item',
                                      {
                                        album: link.albumCommentary(album),
                                        words: language.formatWordCount(words, {
                                          unit: true,
                                        }),
                                        entries:
                                          language.countCommentaryEntries(
                                            entries.length,
                                            {
                                              unit: true,
                                            }
                                          ),
                                      }
                                    )}</li>
                                `
                              )
                              .join('\n')}
                        </ul>
                    </div>
                `,
      },

      nav: {simple: true},
    }),
  };

  return [page];
}