« get me outta code hell

generateAdditionalFilesList.js « dependencies « content « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/content/dependencies/generateAdditionalFilesList.js
blob: 92948c7a07690633fa5db539817a16bccf36c51f (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
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`);
    }
  };
}

export default {
  extraDependencies: ['html', 'language'],

  data(additionalFiles) {
    return {
      // Additional files are already a serializable format.
      additionalFiles,
    };
  },

  slots: {
    fileLinks: {
      validate: v => validateFileMapping(v, v.isHTML),
    },

    fileSizes: {
      validate: v => validateFileMapping(v, v.isWholeNumber),
    },
  },

  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],
                    })))))),
      ]));
  },
};