« get me outta code hell

content: avoid boilerplate around listings, make spec relevant again - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-07-01 14:14:38 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-07-01 14:14:38 -0300
commit34a1115df603aff44f354abe9f15d24d53f7a5ac (patch)
tree09726c8bbfff8c2eec2c189a18841a10827e1ff5
parent28514381d7c4639608786151dc94e5a1b22f85fe (diff)
content: avoid boilerplate around listings, make spec relevant again
-rw-r--r--src/content/dependencies/generateListingPage.js19
-rw-r--r--src/content/dependencies/listAlbumsByName.js28
-rw-r--r--src/listing-spec.js10
-rw-r--r--src/page/listing.js16
4 files changed, 42 insertions, 31 deletions
diff --git a/src/content/dependencies/generateListingPage.js b/src/content/dependencies/generateListingPage.js
index ae186d28..6e9db689 100644
--- a/src/content/dependencies/generateListingPage.js
+++ b/src/content/dependencies/generateListingPage.js
@@ -9,28 +9,33 @@ export default {
     };
   },
 
-  slots: {
-    title: {type: 'string'},
+  data(query, sprawl, listing) {
+    return {
+      stringsKey: listing.stringsKey,
+    };
+  },
 
+  slots: {
     type: {
       validate: v => v.is('rows'),
     },
 
     rows: {
-      validate: v => v.arrayOf(v.isHTML),
+      validate: v => v.arrayOf(v.isObject),
     },
   },
 
-  generate(relations, slots, {html}) {
+  generate(data, relations, slots, {html}) {
     return relations.layout.slots({
-      title: slots.title,
+      title: language.$(`listingPage.${data.stringsKey}.title`),
       headingMode: 'sticky',
 
       mainContent: [
         slots.type === 'rows' &&
           html.tag('ul',
-            slots.rows
-              .map(row => html.tag('li', row))),
+            slots.rows.map(row =>
+              html.tag('li',
+                language.$(`listingPage.${data.stringsKey}.item`, row)))),
       ],
 
       navLinkStyle: 'hierarchical',
diff --git a/src/content/dependencies/listAlbumsByName.js b/src/content/dependencies/listAlbumsByName.js
index 2182efa5..c302a9cb 100644
--- a/src/content/dependencies/listAlbumsByName.js
+++ b/src/content/dependencies/listAlbumsByName.js
@@ -9,38 +9,42 @@ export default {
     return {albumData};
   },
 
-  query({albumData}) {
+  query({albumData}, spec) {
     return {
+      spec,
       albums: sortAlphabetically(albumData.slice()),
     };
   },
 
   relations(relation, query) {
     return {
-      page: relation('generateListingPage'),
-      links: query.albums.map(album => relation('linkAlbum', album)),
+      page: relation('generateListingPage', query.spec),
+
+      albumLinks:
+        query.albums
+          .map(album => relation('linkAlbum', album)),
     };
   },
 
   data(query) {
     return {
-      numTracks: query.albums.map(album => album.tracks.length),
+      counts:
+        query.albums
+          .map(album => album.tracks.length),
     };
   },
 
   generate(data, relations, {language}) {
     return relations.page.slots({
-      title: language.$('listingPage.listAlbums.byName.title'),
       type: 'rows',
       rows:
         stitchArrays({
-          link: relations.links,
-          numTracks: data.numTracks,
-        }).map(({link, numTracks}) =>
-            language.$('listingPage.listAlbums.byName.item', {
-              album: link,
-              tracks: language.countTracks(numTracks, {unit: true}),
-            })),
+          link: relations.albumLinks,
+          count: data.counts,
+        }).map(({link, count}) => ({
+            album: link,
+            tracks: language.countTracks(count, {unit: true}),
+          })),
     });
   },
 };
diff --git a/src/listing-spec.js b/src/listing-spec.js
index 36637ee0..c81c0866 100644
--- a/src/listing-spec.js
+++ b/src/listing-spec.js
@@ -20,19 +20,11 @@ const listingSpec = [];
 listingSpec.push({
   directory: 'albums/by-name',
   stringsKey: 'listAlbums.byName',
+  contentFunction: 'listAlbumsByName',
 
   seeAlso: [
     'tracks/by-album',
   ],
-
-  data: ({wikiData: {albumData}}) =>
-    sortAlphabetically(albumData.slice()),
-
-  row: (album, {language, link}) =>
-    language.$('listingPage.listAlbums.byName.item', {
-      album: link.album(album),
-      tracks: language.countTracks(album.tracks.length, {unit: true}),
-    }),
 });
 
 listingSpec.push({
diff --git a/src/page/listing.js b/src/page/listing.js
index da4c2968..c770f825 100644
--- a/src/page/listing.js
+++ b/src/page/listing.js
@@ -14,11 +14,21 @@ import {getTotalDuration} from '../util/wiki-data.js';
 
 export const description = `wiki-wide listing pages & index`;
 
-export function pathsTargetless() {
+export function targets({wikiData}) {
+  return (
+    wikiData.listingSpec
+      .filter(listing => listing.contentFunction));
+}
+
+export function pathsForTarget(listing) {
   return [
     {
-      type: 'page', path: ['listing', 'albums/by-name'],
-      contentFunction: {name: 'listAlbumsByName'},
+      type: 'page',
+      path: ['listing', listing.directory],
+      contentFunction: {
+        name: listing.contentFunction,
+        args: [listing],
+      },
     },
   ];
 }