From f7e40b713df8e4cf082896dd191f558d6e87b5ba Mon Sep 17 00:00:00 2001 From: Ro Date: Sun, 14 Jun 2020 18:18:31 -0700 Subject: [PATCH] added archive page that contains all site pages --- brain/data/Book.js | 1 + brain/data/Render.js | 17 ++++++++++++++++ brain/data/Utils.js | 47 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/brain/data/Book.js b/brain/data/Book.js index 80496ac..a414326 100644 --- a/brain/data/Book.js +++ b/brain/data/Book.js @@ -43,6 +43,7 @@ export default class Book { setTimeout(() => { //TODO: Duct tape solution until something better created utils.organizeTags(pages); + utils.organizeArchive(pages); resolve(pages); }, 100); } else { diff --git a/brain/data/Render.js b/brain/data/Render.js index 549ad25..dde337a 100644 --- a/brain/data/Render.js +++ b/brain/data/Render.js @@ -143,6 +143,23 @@ export default class Render { } }); } + publishArchive(archive) { + let file = pug.renderFile('content/themes/' + config.global.theme + '/archive.pug', { + title: 'ARCHIVES', + default_bg: config.global.background, + content_tags: 'COLD STORAGE', + archives: archive + }); + + fs.writeFile('public/archives.html', file, err => { + // throws an error, you could also catch it here + if (err) { + console.log('ERROR', err); + //response = { type: DataEvent.TAG_PAGES_NOT_RENDERED, message: err }; + } + // success case, the file was saved + }); + } //-------------------------- // event handlers diff --git a/brain/data/Utils.js b/brain/data/Utils.js index 7430383..bcccb8b 100644 --- a/brain/data/Utils.js +++ b/brain/data/Utils.js @@ -5,6 +5,7 @@ import _ from 'lodash'; const settings = new Settings(); const render = new Render(); const stringUtils = new StringUtils(); +const moment = require('moment'); export default class Utils { constructor() {} @@ -45,4 +46,50 @@ export default class Utils { }); }); } + organizeArchive(pages) { + let years = []; + let archive = []; + for (let index = 0; index < pages.length; index++) { + let page = pages[index].metadata; + let year = moment(page.created).format('YYYY'); + if (!_.find(years, { year: year })) { + years.push({ year: year, count: 1 }); + } else { + _.find(years, { year: year }).count++; + } + } + years.sort((a, b) => parseFloat(b.year) - parseFloat(a.year)); + for (let index = 0; index < years.length; index++) { + let item = years[index]; + let sorted = []; + let filtered = _.filter(pages, page => { + return moment(page.metadata.created).format('YYYY') === item.year; + }); + for (let index = 0; index < filtered.length; index++) { + let obj = filtered[index].metadata; + let month = moment(obj.created).format('MM'); + if (!_.find(sorted, { month: month })) { + let sortedPages = _.filter(pages, page => { + return ( + moment(page.metadata.created).format('YYYY') === item.year && + moment(page.metadata.created).format('MM') === month + ); + }); + + sorted.push({ + month: month, + full_month: moment(obj.created).format('MMMM'), + count: 1, + pages: sortedPages + }); + } else { + _.find(sorted, { month: month }).count++; + } + } + + archive.push({ year: item.year, year_data: sorted }); + } + render.publishArchive(archive); + //console.log('ARCHIVE', archive[0].meta[0]); + } }