From 6f2f61c1330dbd48a1fdbc564a0fec50a59f2d88 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 3 Jun 2021 12:41:49 -0300 Subject: module-ify news pages & index; new page module fns --- src/page/index.js | 33 ++++++++++---- src/page/news.js | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+), 9 deletions(-) create mode 100644 src/page/news.js (limited to 'src/page') diff --git a/src/page/index.js b/src/page/index.js index d74dad5..384b419 100644 --- a/src/page/index.js +++ b/src/page/index.js @@ -3,7 +3,14 @@ // homepage.js for that. // // Each module published in this list should follow a particular format, -// including the following exports: +// including any of the following exports: +// +// condition({wikiData}) +// Returns a boolean indicating whether to process targets/writes (true) or +// skip this page spec altogether (false). This is usually used for +// selectively toggling pages according to site feature flags, though it may +// also be used to e.g. skip out if no targets would be found (preventing +// writeTargetless from generating an empty index page). // // targets({wikiData}) // Gets the objects which this page's write() function should be called on. @@ -11,14 +18,21 @@ // but it may also apply filter/map/etc if useful. // // write(thing, {wikiData}) -// Gets descriptors for any page and data writes associated with the given -// thing (which will be a value from the targets() array). This includes -// page (HTML) writes, data (JSON) writes, etc. Notably, this function does -// not perform any file operations itself; it only describes the operations -// which will be processed elsewhere, once for each translation language. -// The write function also immediately transforms any data which will be -// reused across writes of the same page, so that this data is effectively -// cached (rather than recalculated for each language/write). +// Provides descriptors for any page and data writes associated with the +// given thing (which will be a value from the targets() array). This +// includes page (HTML) writes, data (JSON) writes, etc. Notably, this +// function does not perform any file operations itself; it only describes +// the operations which will be processed elsewhere, once for each +// translation language. The write function also immediately transforms +// any data which will be reused across writes of the same page, so that +// this data is effectively cached (rather than recalculated for each +// language/write). +// +// writeTargetless({wikiData}) +// Provides descriptors for page/data/etc writes which will be used +// without concern for targets. This is usually used for writing index pages +// which should be generated just once (rather than corresponding to +// targets). // // As these modules are effectively the HTML templates for all site layout, // common patterns may also be exported alongside the special exports above. @@ -29,4 +43,5 @@ export * as album from './album.js'; export * as artist from './artist.js'; export * as artistAlias from './artist-alias.js'; export * as group from './group.js'; +export * as news from './news.js'; export * as track from './track.js'; diff --git a/src/page/news.js b/src/page/news.js new file mode 100644 index 0000000..99cbe8d --- /dev/null +++ b/src/page/news.js @@ -0,0 +1,129 @@ +// News entry & index page specifications. + +// Imports + +import fixWS from 'fix-whitespace'; + +// Page exports + +export function condition({wikiData}) { + return wikiData.wikiInfo.features.news; +} + +export function targets({wikiData}) { + return wikiData.newsData; +} + +export function write(entry, {wikiData}) { + const page = { + type: 'page', + path: ['newsEntry', entry.directory], + page: ({ + generatePreviousNextLinks, + link, + strings, + transformMultiline, + }) => ({ + title: strings('newsEntryPage.title', {entry: entry.name}), + + main: { + content: fixWS` +
+

${strings('newsEntryPage.title', {entry: entry.name})}

+

${strings('newsEntryPage.published', {date: strings.count.date(entry.date)})}

+ ${transformMultiline(entry.body)} +
+ ` + }, + + nav: generateNewsEntryNav(entry, { + generatePreviousNextLinks, + link, + strings, + wikiData + }) + }) + }; + + return [page]; +} + +export function writeTargetless({wikiData}) { + const { newsData } = wikiData; + + const page = { + type: 'page', + path: ['newsIndex'], + page: ({ + link, + strings, + transformMultiline + }) => ({ + title: strings('newsIndex.title'), + + main: { + content: fixWS` +
+

${strings('newsIndex.title')}

+ ${newsData.map(entry => fixWS` +
+

${link.newsEntry(entry)}

+ ${transformMultiline(entry.bodyShort)} + ${entry.bodyShort !== entry.body && `

${link.newsEntry(entry, { + text: strings('newsIndex.entry.viewRest') + })}

`} +
+ `).join('\n')} +
+ ` + }, + + nav: {simple: true} + }) + }; + + return [page]; +} + +// Utility functions + +function generateNewsEntryNav(entry, { + generatePreviousNextLinks, + link, + strings, + wikiData +}) { + const { wikiInfo, 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, strings, + data: newsData.slice().reverse(), + linkKey: 'newsEntry' + }); + + return { + links: [ + { + path: ['localized.home'], + title: wikiInfo.shortName + }, + { + path: ['localized.newsIndex'], + title: strings('newsEntryPage.nav.news') + }, + { + html: strings('newsEntryPage.nav.entry', { + date: strings.count.date(entry.date), + entry: link.newsEntry(entry, {class: 'current'}) + }) + }, + previousNextLinks && + { + divider: false, + html: `(${previousNextLinks})` + } + ] + }; +} -- cgit 1.3.0-6-gf8a5