common.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // This file's shared 8y 8oth the client and the static file 8uilder (i.e,
  2. // upd8.js). It's got common constants and a few utility functions!
  3. const C = {
  4. // Code that's common 8etween the 8uild code (i.e. upd8.js) and gener8ted
  5. // site code should 8e put here. Which, uh, only really means this one
  6. // file. 8ut rather than hard code it, anything in this directory can 8e
  7. // shared across 8oth ends of the code8ase.
  8. // (This gets symlinked into the --data directory.)
  9. COMMON_DIRECTORY: 'common',
  10. // Code that's used only in the static site! CSS, cilent JS, etc.
  11. // (This gets symlinked into the --data directory.)
  12. STATIC_DIRECTORY: 'static',
  13. // Su8directory under DATA_DIRECTORY for al8um files.
  14. DATA_ALBUM_DIRECTORY: 'album',
  15. // Media files! This is symlinked into the --data directory from the
  16. // also user-provided --media directory.
  17. MEDIA_DIRECTORY: 'media',
  18. // Contains a folder for each al8um, within which is the al8um cover art
  19. // as well as any track art. Structure itself looks somethin' like this:
  20. // * album-art/<album.directory>/cover.jpg
  21. // * album-art/<album.directory>/<track1.directory>.jpg
  22. // * album-art/<album.directory>/<track2.directory>.jpg
  23. MEDIA_ALBUM_ART_DIRECTORY: 'album-art',
  24. // Just one folder, with a single image for each flash, matching its output
  25. // directory like al8um and track art. (Just keep in mind the directory of
  26. // a flash is just its page num8er most of the time.)
  27. MEDIA_FLASH_ART_DIRECTORY: 'flash-art',
  28. // Again, a single folder, with one image for each artist, matching their
  29. // output directory (which is usually their name in ke8a8-case). Although,
  30. // unlike other art directories, you don't to specify an image for *every*
  31. // artist - and present files will 8e automatically added!
  32. MEDIA_ARTIST_AVATAR_DIRECTORY: 'artist-avatar',
  33. // Miscellaneous stuff! This is pretty much only referenced in commentary
  34. // fields.
  35. MEDIA_MISC_DIRECOTRY: 'misc',
  36. // The folder you stick your random downloads in is called "Downloads",
  37. // yeah? (Unless you sort all your downloads into manual, organized
  38. // locations. Good for you.) It might just 8e me, 8ut I've always said "the
  39. // downloads folder." And yet here I say "the al8um directory!" It's like
  40. // we've gotten "Downloads" as a name so ingrained into our heads that we
  41. // use it like an adjective too, even though it doesn't make any
  42. // grammatical sense to do so. Anyway, also for contrast, note that this
  43. // folder is called "album" and not "albums". To 8e clear, that IS against
  44. // how I normally name folders - 8ut here, I'm doing it to match 8andcamp's
  45. // URL schema: "/album/genesis-frog" instead of "/albums/genesis-frog."
  46. // That seems to kind of 8e a standard for a lot of sites? 8ut only KIND OF.
  47. // Twitter has the weird schema of "/<user>/status/<id>" (not "statuses")...
  48. // 8ut it also has "/<user>/likes", so I really have no idea how people
  49. // decide to make their URL schemas consistent. Luckily I don't have to
  50. // worry a8out any of that, 8ecause I'm just stealing 8andcamp.
  51. //
  52. // Upd8 03/11/2020: Oh my god this was a pain to re-align (copying from
  53. // udp8.js over to shared.js).
  54. //
  55. // Upd8 03/10/2021 (wow, almost exactly a year later): This code comment
  56. // from literally the first day of wiki development is finally no longer
  57. // necessary! It was commenting constnats like "ALBUM_DIRECTORY" 8efore.
  58. // 8ut we don't have those constants anymore, 'cuz urlSpec in upd8.js
  59. // covers all that!
  60. UNRELEASED_TRACKS_DIRECTORY: 'unreleased-tracks',
  61. OFFICIAL_GROUP_DIRECTORY: 'official',
  62. FANDOM_GROUP_DIRECTORY: 'fandom',
  63. // This function was originally made to sort just al8um data, 8ut its exact
  64. // code works fine for sorting tracks too, so I made the varia8les and names
  65. // more general.
  66. sortByDate: data => {
  67. // Just to 8e clear: sort is a mutating function! I only return the array
  68. // 8ecause then you don't have to define it as a separate varia8le 8efore
  69. // passing it into this function.
  70. return data.sort((a, b) => a.date - b.date);
  71. },
  72. // Same details as the sortByDate, 8ut for covers~
  73. sortByArtDate: data => {
  74. return data.sort((a, b) => (a.coverArtDate || a.date) - (b.coverArtDate || b.date));
  75. },
  76. // This gets all the track o8jects defined in every al8um, and sorts them 8y
  77. // date released. Generally, albumData will pro8a8ly already 8e sorted 8efore
  78. // you pass it to this function, 8ut individual tracks can have their own
  79. // original release d8, distinct from the al8um's d8. I allowed that 8ecause
  80. // in Homestuck, the first four Vol.'s were com8ined into one al8um really
  81. // early in the history of the 8andcamp, and I still want to use that as the
  82. // al8um listing (not the original four al8um listings), 8ut if I only did
  83. // that, all the tracks would 8e sorted as though they were released at the
  84. // same time as the compilation al8um - i.e, after some other al8ums (including
  85. // Vol.'s 5 and 6!) were released. That would mess with chronological listings
  86. // including tracks from multiple al8ums, like artist pages. So, to fix that,
  87. // I gave tracks an Original Date field, defaulting to the release date of the
  88. // al8um if not specified. Pretty reasona8le, I think! Oh, and this feature can
  89. // 8e used for other projects too, like if you wanted to have an al8um listing
  90. // compiling a 8unch of songs with radically different & interspersed release
  91. // d8s, 8ut still keep the al8um listing in a specific order, since that isn't
  92. // sorted 8y date.
  93. getAllTracks: albumData => C.sortByDate(albumData.reduce((acc, album) => acc.concat(album.tracks), [])),
  94. getKebabCase: name => name.split(' ').join('-').replace(/&/g, 'and').replace(/[^a-zA-Z0-9\-]/g, '').replace(/-{2,}/g, '-').replace(/^-+|-+$/g, '').toLowerCase(),
  95. // Terri8le hack: since artists aren't really o8jects and don't have proper
  96. // "directories", we just reformat the artist's name.
  97. getArtistDirectory: artistName => C.getKebabCase(artistName),
  98. getArtistNumContributions: artist => (
  99. artist.tracks.asAny.length +
  100. artist.albums.asCoverArtist.length +
  101. (artist.flashes ? artist.flashes.asContributor.length : 0)
  102. ),
  103. getArtistCommentary: (artist, {justEverythingMan}) => justEverythingMan.filter(thing => thing.commentary && thing.commentary.replace(/<\/?b>/g, '').includes('<i>' + artist.name + ':</i>')),
  104. // Graciously stolen from https://stackoverflow.com/a/54071699! ::::)
  105. // in: r,g,b in [0,1], out: h in [0,360) and s,l in [0,1]
  106. rgb2hsl: (r,g,b) => {
  107. let a=Math.max(r,g,b), n=a-Math.min(r,g,b), f=(1-Math.abs(a+a-n-1));
  108. let h= n && ((a==r) ? (g-b)/n : ((a==g) ? 2+(b-r)/n : 4+(r-g)/n));
  109. return [60*(h<0?h+6:h), f ? n/f : 0, (a+a-n)/2];
  110. },
  111. getColors: primary => {
  112. const [ r, g, b ] = primary.slice(1)
  113. .match(/[0-9a-fA-F]{2,2}/g)
  114. .slice(0, 3)
  115. .map(val => parseInt(val, 16) / 255);
  116. const [ h, s, l ] = C.rgb2hsl(r, g, b);
  117. const dim = `hsl(${Math.round(h)}deg, ${Math.round(s * 50)}%, ${Math.round(l * 80)}%)`;
  118. return {primary, dim};
  119. }
  120. };
  121. if (typeof module === 'object') {
  122. module.exports = C;
  123. } else if (typeof window === 'object') {
  124. window.C = C;
  125. }