From 3e7e3447db92496e3c20031010ccbe7e44d36e78 Mon Sep 17 00:00:00 2001 From: Cassie Jones Date: Sat, 2 Nov 2019 17:04:51 -0400 Subject: [PATCH] Archive polish work --- .eleventy.js | 41 ++++++++++++++++++++++++++++++++++++ _includes/layouts/layout.njk | 5 +++-- archive.njk | 13 +++++++----- years.njk | 20 ++++++++++++++++++ 4 files changed, 72 insertions(+), 7 deletions(-) create mode 100644 years.njk diff --git a/.eleventy.js b/.eleventy.js index 38ba442..5b80ddb 100644 --- a/.eleventy.js +++ b/.eleventy.js @@ -14,13 +14,54 @@ module.exports = (eleventyConfig) => { eleventyConfig.addNunjucksShortcode("youtube", youtubeShortcode); eleventyConfig.addPairedNunjucksShortcode("tweet", tweetShortcode); + eleventyConfig.addFilter("markdown", value => md.renderInline(value)); + eleventyConfig.addFilter("groupby", groupbyFilter); + + eleventyConfig.addCollection("years", collection => { + const posts = collection.getFilteredByTag("posts"); + const items = groupby(posts, item => item.date.getFullYear()); + console.log(items); + return items.reduce((obj, [k, v]) => (obj[k] = v, obj), {}); + }); return { markdownTemplateEngine: "njk", }; }; +function access(item, path) { + const segments = path.split("."); + for (const seg of segments) { + if (item === undefined) { return null; } + if (seg.endsWith("()")) { + const method = item[seg.slice(0, -2)]; + if (method === undefined) { return null; } + item = method.bind(item)(); + } else { + item = item[seg]; + } + } + return item; +} + +function groupby(items, keyFn) { + const results = []; + for (const item of items) { + const key = keyFn(item); + if (results.length == 0 || key != results[results.length-1][0]) { + results.push([key, [item]]); + } else { + results[results.length-1][1].push(item); + } + } + return results; +} + +function groupbyFilter(items, path) { + return groupby(items, item => access(item, path)); +} + function youtubeShortcode(items, inWidth, inHeight) { const width = items.width || inWidth || 560; const height = items.height || inHeight || 315; diff --git a/_includes/layouts/layout.njk b/_includes/layouts/layout.njk index 3d9dea3..5ba4f22 100644 --- a/_includes/layouts/layout.njk +++ b/_includes/layouts/layout.njk @@ -4,8 +4,9 @@ Witch of Light