about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--src/index.js16
-rw-r--r--src/modules/posts.js10
-rw-r--r--src/views/tag.html3
-rw-r--r--test/index.test.js8
-rw-r--r--test/modules/posts.test.js5
5 files changed, 36 insertions, 6 deletions
diff --git a/src/index.js b/src/index.js
index f691930..e811be0 100644
--- a/src/index.js
+++ b/src/index.js
@@ -29,6 +29,22 @@ 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]))
+    }
+  }
+}
+router.get('/tags/:tag', async function (ctx) {
+  ctx.assert(tags.has(ctx.params.tag), 404, 'Tag not found')
+  await ctx.render('tag', {
+    tag: ctx.params.tag,
+    posts: tags.get(ctx.params.tag)
+  })
+})
+
 app.use(router.routes()).use(router.allowedMethods())
 
 module.exports = app
diff --git a/src/modules/posts.js b/src/modules/posts.js
index 2d831af..834d45c 100644
--- a/src/modules/posts.js
+++ b/src/modules/posts.js
@@ -24,14 +24,16 @@ function canonicaliseMetadata (meta) {
   return meta
 }
 
-function get (filename) {
-  return canonicaliseMetadata(matter.read(filename, grayMatterOptions))
-}
-
 function getTitle (file) {
   return path.basename(file.path, path.extname(file.path))
 }
 
+function get (filename) {
+  const fileMatter = matter.read(filename, grayMatterOptions)
+  fileMatter.basename = getTitle(fileMatter)
+  return canonicaliseMetadata(fileMatter)
+}
+
 function getFolder (folder) {
   return new Map(
     fs
diff --git a/src/views/tag.html b/src/views/tag.html
new file mode 100644
index 0000000..7ff9871
--- /dev/null
+++ b/src/views/tag.html
@@ -0,0 +1,3 @@
+{% for post in posts %}
+  {{ post.data.get('title') }}
+{% endfor %}
diff --git a/test/index.test.js b/test/index.test.js
index 897438c..a81dbb6 100644
--- a/test/index.test.js
+++ b/test/index.test.js
@@ -21,3 +21,11 @@ test('post', t => {
     .expect(/Lorem ipsum/)
     .then(() => t.pass())
 })
+
+test('tags', t => {
+  return request(app.listen())
+    .get('/tags/a')
+    .expect(200)
+    .expect(/This is a test/)
+    .then(() => t.pass())
+})
diff --git a/test/modules/posts.test.js b/test/modules/posts.test.js
index cdbf22a..4f1efb5 100644
--- a/test/modules/posts.test.js
+++ b/test/modules/posts.test.js
@@ -11,8 +11,9 @@ test('get', t => {
       tags: ['a', 'b']
     })
   )
-  const meta = posts.get(path.resolve(__dirname, '../data/testfile.md'))
-  t.deepEqual(meta.data, expected)
+  const post = posts.get(path.resolve(__dirname, '../data/testfile.md'))
+  t.deepEqual(post.data, expected)
+  t.is(post.basename, 'testfile', 'must include basename')
 })
 
 test('getFolder', t => {