« get me outta code hell

news.js « page « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/page/news.js
blob: 478ff9d036a07d72e122e87023649e9b01e4e5da (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
/** @format */

// News entry & index page specifications.

// Imports

import fixWS from 'fix-whitespace';

// Page exports

export function condition({wikiData}) {
  return wikiData.wikiInfo.enableNews;
}

export function targets({wikiData}) {
  return wikiData.newsData;
}

export function write(entry, {wikiData}) {
  const page = {
    type: 'page',
    path: ['newsEntry', entry.directory],
    page: ({
      generatePreviousNextLinks,
      link,
      language,
      transformMultiline,
    }) => ({
      title: language.$('newsEntryPage.title', {entry: entry.name}),

      main: {
        content: fixWS`
                    <div class="long-content">
                        <h1>${language.$('newsEntryPage.title', {
                          entry: entry.name,
                        })}</h1>
                        <p>${language.$('newsEntryPage.published', {
                          date: language.formatDate(entry.date),
                        })}</p>
                        ${transformMultiline(entry.content)}
                    </div>
                `,
      },

      nav: generateNewsEntryNav(entry, {
        generatePreviousNextLinks,
        link,
        language,
        wikiData,
      }),
    }),
  };

  return [page];
}

export function writeTargetless({wikiData}) {
  const {newsData} = wikiData;

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

      main: {
        content: fixWS`
                    <div class="long-content news-index">
                        <h1>${language.$('newsIndex.title')}</h1>
                        ${newsData
                          .map(
                            (entry) => fixWS`
                            <article id="${entry.directory}">
                                <h2><time>${language.formatDate(
                                  entry.date
                                )}</time> ${link.newsEntry(entry)}</h2>
                                ${transformMultiline(entry.contentShort)}
                                ${
                                  entry.contentShort !== entry.content &&
                                  `<p>${link.newsEntry(entry, {
                                    text: language.$(
                                      'newsIndex.entry.viewRest'
                                    ),
                                  })}</p>`
                                }
                            </article>
                        `
                          )
                          .join('\n')}
                    </div>
                `,
      },

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

  return [page];
}

// Utility functions

function generateNewsEntryNav(
  entry,
  {generatePreviousNextLinks, link, language, wikiData}
) {
  const {newsData} = wikiData;

  // The newsData list is sorted reverse chronologically (newest ones first),
  // so the way we find next/previous entries is flipped from normal.
  const previousNextLinks = generatePreviousNextLinks(entry, {
    link,
    language,
    data: newsData.slice().reverse(),
    linkKey: 'newsEntry',
  });

  return {
    linkContainerClasses: ['nav-links-hierarchy'],
    links: [
      {toHome: true},
      {
        path: ['localized.newsIndex'],
        title: language.$('newsEntryPage.nav.news'),
      },
      {
        html: language.$('newsEntryPage.nav.entry', {
          date: language.formatDate(entry.date),
          entry: link.newsEntry(entry, {class: 'current'}),
        }),
      },
      previousNextLinks && {
        divider: false,
        html: `(${previousNextLinks})`,
      },
    ],
  };
}