From a074fd54107c51c4fcbfedbbf6df6eca539d19d3 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Mon, 26 Jan 2026 15:14:38 -0400 Subject: data, yaml: split yaml loading specs into src/data/files --- src/data/files/album.js | 74 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/data/files/album.js (limited to 'src/data/files/album.js') diff --git a/src/data/files/album.js b/src/data/files/album.js new file mode 100644 index 00000000..84cda226 --- /dev/null +++ b/src/data/files/album.js @@ -0,0 +1,74 @@ +import * as path from 'node:path'; + +import {traverse} from '#node-utils'; +import {sortAlbumsTracksChronologically, sortChronologically} from '#sort'; +import {empty} from '#sugar'; + +export default ({ + documentModes: {headerAndEntries}, + thingConstructors: {Album, Track, TrackSection}, +}) => ({ + title: `Process album files`, + + files: dataPath => + traverse(path.join(dataPath, 'album'), { + filterFile: name => path.extname(name) === '.yaml', + prefixPath: 'album', + }), + + documentMode: headerAndEntries, + headerDocumentThing: Album, + entryDocumentThing: document => + ('Section' in document + ? TrackSection + : Track), + + connect({header: album, entries}) { + const trackSections = []; + + let currentTrackSection = new TrackSection(); + let currentTrackSectionTracks = []; + + Object.assign(currentTrackSection, { + name: `Default Track Section`, + isDefaultTrackSection: true, + }); + + const closeCurrentTrackSection = () => { + if ( + currentTrackSection.isDefaultTrackSection && + empty(currentTrackSectionTracks) + ) { + return; + } + + currentTrackSection.tracks = currentTrackSectionTracks; + currentTrackSection.album = album; + + trackSections.push(currentTrackSection); + }; + + for (const entry of entries) { + if (entry instanceof TrackSection) { + closeCurrentTrackSection(); + currentTrackSection = entry; + currentTrackSectionTracks = []; + continue; + } + + entry.album = album; + entry.trackSection = currentTrackSection; + + currentTrackSectionTracks.push(entry); + } + + closeCurrentTrackSection(); + + album.trackSections = trackSections; + }, + + sort({albumData, trackData}) { + sortChronologically(albumData); + sortAlbumsTracksChronologically(trackData); + }, +}); -- cgit 1.3.0-6-gf8a5