about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--src/index.js9
-rw-r--r--src/modules/posts.js15
-rw-r--r--test/modules/posts.test.js21
3 files changed, 33 insertions, 12 deletions
diff --git a/src/index.js b/src/index.js
index e811be0..07d7249 100644
--- a/src/index.js
+++ b/src/index.js
@@ -29,14 +29,7 @@ router.get('/post/:filename', async function (ctx) {
   })
 })
 
-const tags = new Map()
-for (let [, post] of posts) {
-  if (post.data.has('tags')) {
-    for (let tag of post.data.get('tags')) {
-      tags.set(tag, (tags.get(tag) || []).concat([post]))
-    }
-  }
-}
+const tags = Posts.toTags(posts)
 router.get('/tags/:tag', async function (ctx) {
   ctx.assert(tags.has(ctx.params.tag), 404, 'Tag not found')
   await ctx.render('tag', {
diff --git a/src/modules/posts.js b/src/modules/posts.js
index 834d45c..011292b 100644
--- a/src/modules/posts.js
+++ b/src/modules/posts.js
@@ -44,7 +44,20 @@ function getFolder (folder) {
   )
 }
 
+function toTags (posts) {
+  const tags = new Map()
+  for (let [, post] of posts) {
+    if (post.data.has('tags')) {
+      for (let tag of post.data.get('tags')) {
+        tags.set(tag, (tags.get(tag) || []).concat([post]))
+      }
+    }
+  }
+  return tags
+}
+
 module.exports = {
   get,
-  getFolder
+  getFolder,
+  toTags
 }
diff --git a/test/modules/posts.test.js b/test/modules/posts.test.js
index 4f1efb5..454488f 100644
--- a/test/modules/posts.test.js
+++ b/test/modules/posts.test.js
@@ -1,7 +1,7 @@
 const test = require('ava')
 const path = require('path')
 
-const posts = require('../../src/modules/posts.js')
+const Posts = require('../../src/modules/posts.js')
 
 test('get', t => {
   const expected = new Map(
@@ -11,7 +11,7 @@ test('get', t => {
       tags: ['a', 'b']
     })
   )
-  const post = posts.get(path.resolve(__dirname, '../data/testfile.md'))
+  const post = Posts.get(path.resolve(__dirname, '../data/testfile.md'))
   t.deepEqual(post.data, expected)
   t.is(post.basename, 'testfile', 'must include basename')
 })
@@ -24,7 +24,7 @@ test('getFolder', t => {
       tags: ['a', 'b']
     })
   )
-  const actual = posts.getFolder(path.resolve(__dirname, '../data/'))
+  const actual = Posts.getFolder(path.resolve(__dirname, '../data/'))
   t.true(actual.size > 0, 'must return a non-empty map')
   t.is(
     actual.get('testfile').path,
@@ -32,3 +32,18 @@ test('getFolder', t => {
   )
   t.deepEqual(actual.get('testfile').data, expected)
 })
+
+test('toTags', t => {
+  const posts = new Map([
+    [
+      'testfile',
+      {
+        data: new Map([['title', 'Test Post'], ['tags', ['a', 'b']]])
+      }
+    ]
+  ])
+  const actual = Posts.toTags(posts)
+  t.is(actual.size, 2)
+  t.is(actual.get('a')[0].data.get('title'), 'Test Post')
+  t.deepEqual(actual.get('a'), actual.get('b'))
+})