From ad56116d0df26b4a6fe7f9cd04a21965d3184af5 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Tue, 3 Oct 2017 15:30:54 +0200 Subject: Reify taxonomies into just tags. I'm probably not going to need anything else, and this makes it a lot clearer --- src/actions.js | 16 +++++++--------- src/app.js | 12 +++++------- src/domain/posts.js | 22 +++++++++------------- 3 files changed, 21 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/actions.js b/src/actions.js index 2ad0c77..61f4fd8 100644 --- a/src/actions.js +++ b/src/actions.js @@ -50,17 +50,15 @@ function post(config, responder, posts) { }; } -function taxonGenerator(config, responder, term, items) { +function tag(config, responder, items) { return async function(ctx, next) { - const value = ctx.params.value; - ctx.assert(items.has(ctx.params.value), 404, `${term} ${value} not found`); - - const taxonItems = items.get(value); + const tag = ctx.params.name; + ctx.assert(items.has(tag), 404, `tag ${tag} not found`); responder(ctx, config, { - listType: term, - listName: value, - posts: taxonItems + listType: "tag", + listName: tag, + posts: items.get(tag) }); }; } @@ -79,6 +77,6 @@ module.exports = { posts, highlightTheme, post, - taxonGenerator, + tag, serveFiles }; diff --git a/src/app.js b/src/app.js index a323b9c..2c23258 100644 --- a/src/app.js +++ b/src/app.js @@ -53,13 +53,11 @@ module.exports = async function() { actions.post(config, responders.post, Posts.posts) ); - for (let [term, items] of Posts.taxonomies) { - router.get( - `taxon-${term}`, - `/${term}/:value`, - actions.taxonGenerator(config, responders.list, term, items) - ); - } + router.get( + "tag", + "/tag/:name", + actions.tag(config, responders.list, Posts.tags) + ); app.use( helmet({ diff --git a/src/domain/posts.js b/src/domain/posts.js index c6b57c5..1214072 100644 --- a/src/domain/posts.js +++ b/src/domain/posts.js @@ -68,33 +68,29 @@ async function getFolder(folder, getURL) { return new Map(posts.map(f => [getTitle(f), f])); } -function taxonomise(taxonomies, posts) { - const taxons = new Map(Object.keys(taxonomies).map(t => [t, new Map()])); +function tag(posts) { + const tags = new Map(); for (let [, post] of posts) { - for (let [singularName, pluralName] of Object.entries(taxonomies)) { - if (post.data.has(pluralName)) { - for (let term of post.data.get(pluralName)) { - const current = taxons.get(singularName).get(term); - taxons - .get(singularName) - .set(term, current ? current.concat(post) : [post]); - } + if (post.data.has("tags")) { + for (let term of post.data.get("tags")) { + const current = tags.get(term); + tags.set(term, current ? current.concat(post) : [post]); } } } - return taxons; + return tags; } module.exports = async function(config, getURL) { const posts = await getFolder(config.folder, getURL); - const taxonomies = taxonomise(config.taxonomies, posts); + const tags = tag(posts); const lastPostDate = Math.max(Array.from(posts.values(), p => p.date)); return { lastPostDate, posts, - taxonomies, + tags, get }; }; -- cgit 1.4.1