From f3c0efe101a38dee7920c5e4115a773ec98d1882 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Tue, 2 Apr 2024 12:05:54 -0300 Subject: content: generate{Album,}AdditionalFiles* cleanup --- .../dependencies/generateAdditionalFilesList.js | 99 +++------------------- 1 file changed, 14 insertions(+), 85 deletions(-) (limited to 'src/content/dependencies/generateAdditionalFilesList.js') diff --git a/src/content/dependencies/generateAdditionalFilesList.js b/src/content/dependencies/generateAdditionalFilesList.js index fc24ee52..2737a410 100644 --- a/src/content/dependencies/generateAdditionalFilesList.js +++ b/src/content/dependencies/generateAdditionalFilesList.js @@ -1,95 +1,24 @@ -import {empty} from '#sugar'; - -function validateFileMapping(v, validateValue) { - return value => { - v.isObject(value); - - const valueErrors = []; - for (const [fileKey, fileValue] of Object.entries(value)) { - if (fileValue === null) { - continue; - } - - try { - validateValue(fileValue); - } catch (error) { - error.message = `(${fileKey}) ` + error.message; - valueErrors.push(error); - } - } - - if (!empty(valueErrors)) { - throw new AggregateError(valueErrors, `Errors validating values`); - } - }; -} +import {stitchArrays} from '#sugar'; export default { - extraDependencies: ['html', 'language'], - - data: (additionalFiles) => ({ - // Additional files are already a serializable format. - additionalFiles, - }), + extraDependencies: ['html'], slots: { - fileLinks: { - validate: v => validateFileMapping(v, v.isHTML), + chunks: { + validate: v => v.strictArrayOf(v.isHTML), }, - fileSizes: { - validate: v => validateFileMapping(v, v.isWholeNumber), + chunkItems: { + validate: v => v.strictArrayOf(v.isHTML), }, }, - generate(data, slots, {html, language}) { - if (!slots.fileLinks) { - return html.blank(); - } - - const filesWithLinks = new Set( - Object.entries(slots.fileLinks) - .filter(([key, value]) => value) - .map(([key]) => key)); - - if (empty(filesWithLinks)) { - return html.blank(); - } - - const filteredFileGroups = data.additionalFiles - .map(({title, description, files}) => ({ - title, - description, - files: files.filter(f => filesWithLinks.has(f)), - })) - .filter(({files}) => !empty(files)); - - if (empty(filteredFileGroups)) { - return html.blank(); - } - - return html.tag('dl', - filteredFileGroups.flatMap(({title, description, files}) => [ - html.tag('dt', - (description - ? language.$('releaseInfo.additionalFiles.entry.withDescription', { - title, - description, - }) - : language.$('releaseInfo.additionalFiles.entry', {title}))), - - html.tag('dd', - html.tag('ul', - files.map(file => - html.tag('li', - (slots.fileSizes?.[file] - ? language.$('releaseInfo.additionalFiles.file.withSize', { - file: slots.fileLinks[file], - size: language.formatFileSize(slots.fileSizes[file]), - }) - : language.$('releaseInfo.additionalFiles.file', { - file: slots.fileLinks[file], - })))))), - ])); - }, + generate: (slots, {html}) => + html.tag('dl', + stitchArrays({ + chunk: slots.chunks, + items: slots.chunkItems, + }).map(({chunk, items}) => + chunk.clone() + .slot('items', items))), }; -- cgit 1.3.0-6-gf8a5