« get me outta code hell

content: generateNewsEntryPage - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-08-01 11:08:53 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-08-01 11:09:24 -0300
commite03756a5113f9b9b37588dfa7ddfa96137c685fe (patch)
tree1ed79db32e9e4a3a558048618d76bbb3406dc713 /src
parentef873ddd0296b45a3a330c1ba447d0b3ba417ca0 (diff)
content: generateNewsEntryPage
Diffstat (limited to 'src')
-rw-r--r--src/content/dependencies/generateNewsEntryPage.js123
-rw-r--r--src/content/dependencies/generateNewsIndexPage.js3
-rw-r--r--src/page/index.js2
-rw-r--r--src/page/news.js141
4 files changed, 145 insertions, 124 deletions
diff --git a/src/content/dependencies/generateNewsEntryPage.js b/src/content/dependencies/generateNewsEntryPage.js
new file mode 100644
index 00000000..e3071a57
--- /dev/null
+++ b/src/content/dependencies/generateNewsEntryPage.js
@@ -0,0 +1,123 @@
+import {sortChronologically} from '../../util/wiki-data.js';
+
+export default {
+  contentDependencies: [
+    'generatePageLayout',
+    'generatePreviousNextLinks',
+    'linkNewsEntry',
+    'linkNewsIndex',
+    'transformContent',
+  ],
+
+  extraDependencies: ['html', 'language', 'wikiData'],
+
+  sprawl({newsData}) {
+    return {newsData};
+  },
+
+  query({newsData}, newsEntry) {
+    const entries = sortChronologically(newsData.slice());
+
+    const index = entries.indexOf(newsEntry);
+
+    const previousEntry =
+      (index > 0
+        ? entries[index - 1]
+        : null);
+
+    const nextEntry =
+      (index < entries.length - 1
+        ? entries[index + 1]
+        : null);
+
+    return {previousEntry, nextEntry};
+  },
+
+  relations(relation, query, sprawl, newsEntry) {
+    const relations = {};
+
+    relations.layout =
+      relation('generatePageLayout');
+
+    relations.content =
+      relation('transformContent', newsEntry.content);
+
+    relations.newsIndexLink =
+      relation('linkNewsIndex');
+
+    relations.currentEntryLink =
+      relation('linkNewsEntry', newsEntry);
+
+    if (query.previousEntry || query.nextEntry) {
+      relations.previousNextLinks =
+        relation('generatePreviousNextLinks');
+
+      if (query.previousEntry) {
+        relations.previousEntryLink =
+          relation('linkNewsEntry', query.previousEntry);
+      }
+
+      if (query.nextEntry) {
+        relations.nextEntryLink =
+          relation('linkNewsEntry', query.nextEntry);
+      }
+    }
+
+    return relations;
+  },
+
+  data(query, sprawl, newsEntry) {
+    return {
+      name: newsEntry.name,
+      date: newsEntry.date,
+    };
+  },
+
+  generate(data, relations, {html, language}) {
+    return relations.layout.slots({
+      title:
+        language.$('newsEntryPage.title', {
+          entry: data.name,
+        }),
+
+      headingMode: 'sticky',
+
+      mainClasses: ['long-content'],
+      mainContent: [
+        html.tag('p',
+          language.$('newsEntryPage.published', {
+            date: language.formatDate(data.date),
+          })),
+
+        relations.content,
+      ],
+
+      navLinkStyle: 'hierarchical',
+      navLinks: [
+        {auto: 'home'},
+        {
+          html:
+            relations.newsIndexLink
+              .slot('content', language.$('newsEntryPage.nav.news')),
+        },
+        {
+          html:
+            language.$('newsEntryPage.nav.entry', {
+              date: language.formatDate(data.date),
+              entry:
+                relations.currentEntryLink
+                  .slot('attributes', {class: 'current'}),
+            }),
+
+          accent:
+            (relations.previousNextLinks
+              ? `(${language.formatUnitList(relations.previousNextLinks.slots({
+                  previousLink: relations.previousEntryLink ?? null,
+                  nextLink: relations.nextEntryLink ?? null,
+                }).content)})`
+              : null),
+        },
+      ],
+    });
+  },
+};
diff --git a/src/content/dependencies/generateNewsIndexPage.js b/src/content/dependencies/generateNewsIndexPage.js
new file mode 100644
index 00000000..938d8be7
--- /dev/null
+++ b/src/content/dependencies/generateNewsIndexPage.js
@@ -0,0 +1,3 @@
+export default {
+  generate() {},
+};
diff --git a/src/page/index.js b/src/page/index.js
index 408a7e72..a2cdbbdf 100644
--- a/src/page/index.js
+++ b/src/page/index.js
@@ -14,7 +14,7 @@ export * as artistAlias from './artist-alias.js';
 export * as group from './group.js';
 export * as homepage from './homepage.js';
 export * as listing from './listing.js';
-// export * as news from './news.js';
+export * as news from './news.js';
 export * as static from './static.js';
 export * as tag from './tag.js';
 export * as track from './track.js';
diff --git a/src/page/news.js b/src/page/news.js
index 00d1e4dc..4928a116 100644
--- a/src/page/news.js
+++ b/src/page/news.js
@@ -10,130 +10,25 @@ export function targets({wikiData}) {
   return wikiData.newsData;
 }
 
-export function write(entry, {wikiData}) {
-  const page = {
-    type: 'page',
-    path: ['newsEntry', entry.directory],
-    page: ({
-      generateNavigationLinks,
-      html,
-      language,
-      link,
-      transformMultiline,
-    }) => ({
-      title: language.$('newsEntryPage.title', {entry: entry.name}),
-
-      main: {
-        classes: ['long-content'],
-        headingMode: 'sticky',
-
-        content: [
-          html.tag('p',
-            language.$('newsEntryPage.published', {
-              date: language.formatDate(entry.date),
-            })),
-
-          transformMultiline(entry.content),
-        ],
-      },
-
-      nav: generateNewsEntryNav(entry, {
-        generateNavigationLinks,
-        html,
-        language,
-        link,
-        wikiData,
-      }),
-    }),
-  };
-
-  return [page];
-}
-
-export function writeTargetless({wikiData}) {
-  const {newsData} = wikiData;
-
-  const page = {
-    type: 'page',
-    path: ['newsIndex'],
-    page: ({
-      html,
-      language,
-      link,
-      transformMultiline,
-    }) => ({
-      title: language.$('newsIndex.title'),
-
-      main: {
-        classes: ['long-content', 'news-index'],
-        headingMode: 'sticky',
-
-        content:
-          newsData.map(entry =>
-            html.tag('article',
-              {id: entry.directory},
-              [
-                html.tag('h2', [
-                  html.tag('time',
-                    language.formatDate(entry.date)),
-                  link.newsEntry(entry),
-                ]),
-
-                transformMultiline(entry.contentShort),
-
-                entry.contentShort !== entry.content &&
-                  html.tag('p',
-                    link.newsEntry(entry, {
-                      text: language.$(
-                        'newsIndex.entry.viewRest'
-                      ),
-                    })),
-              ])),
+export function pathsForTarget(newsEntry) {
+  return [
+    {
+      type: 'page',
+      path: ['newsEntry', newsEntry.directory],
+      contentFunction: {
+        name: 'generateNewsEntryPage',
+        args: [newsEntry],
       },
-
-      nav: {simple: true},
-    }),
-  };
-
-  return [page];
+    },
+  ];
 }
 
-function generateNewsEntryNav(entry, {
-  generateNavigationLinks,
-  html,
-  language,
-  link,
-  wikiData: {newsData},
-}) {
-  // The newsData list is sorted reverse chronologically (newest ones first),
-  // so the way we find next/previous entries is flipped from normal.
-  const previousNextLinks = generateNavigationLinks(entry, {
-    data: newsData.slice().reverse(),
-    linkKey: 'newsEntry',
-
-    html,
-    language,
-    link,
-  });
-
-  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})`,
-      },
-    ],
-  };
+export function pathsTargetless() {
+  return [
+    {
+      type: 'page',
+      path: ['newsIndex'],
+      contentFunction: {name: 'generateNewsIndexPage'},
+    },
+  ];
 }