diff options
-rw-r--r-- | src/index.js | 12 | ||||
-rw-r--r-- | src/modules/posts.js | 12 | ||||
-rw-r--r-- | src/views/index.html | 2 | ||||
-rw-r--r-- | src/views/post.html | 3 | ||||
-rw-r--r-- | test/data/testfile.md | 1 | ||||
-rw-r--r-- | test/index.test.js | 10 | ||||
-rw-r--r-- | test/modules/posts.test.js | 10 |
7 files changed, 41 insertions, 9 deletions
diff --git a/src/index.js b/src/index.js index 3d25faa..f691930 100644 --- a/src/index.js +++ b/src/index.js @@ -15,9 +15,17 @@ const posts = Posts.getFolder(process.env.POST_DIR) app.use(view(`${__dirname}/views`)) -router.get('/', async function (ctx, next) { +const postsArray = Array.from(posts.entries()) +router.get('/', async function (ctx) { await ctx.render('index', { - posts + posts: postsArray + }) +}) + +router.get('/post/:filename', async function (ctx) { + ctx.assert(posts.has(ctx.params.filename), 404, 'Post not found') + await ctx.render('post', { + post: posts.get(ctx.params.filename) }) }) diff --git a/src/modules/posts.js b/src/modules/posts.js index 26db63d..2d831af 100644 --- a/src/modules/posts.js +++ b/src/modules/posts.js @@ -28,8 +28,18 @@ function get (filename) { return canonicaliseMetadata(matter.read(filename, grayMatterOptions)) } +function getTitle (file) { + return path.basename(file.path, path.extname(file.path)) +} + function getFolder (folder) { - return fs.readdirSync(folder).map(f => path.resolve(folder, f)).map(get) + return new Map( + fs + .readdirSync(folder) + .map(f => path.resolve(folder, f)) + .map(get) + .map(f => [getTitle(f), f]) + ) } module.exports = { diff --git a/src/views/index.html b/src/views/index.html index 0e900e6..dcc142c 100644 --- a/src/views/index.html +++ b/src/views/index.html @@ -1,5 +1,5 @@ hello world -{% for post in posts %} +{% for filename, post in posts %} {{ post.data.get('title') }} {% endfor %} diff --git a/src/views/post.html b/src/views/post.html new file mode 100644 index 0000000..fcc4dbd --- /dev/null +++ b/src/views/post.html @@ -0,0 +1,3 @@ +{{ post.data.title }} + +{{ post.content }} diff --git a/test/data/testfile.md b/test/data/testfile.md index 6899d52..bafc456 100644 --- a/test/data/testfile.md +++ b/test/data/testfile.md @@ -3,3 +3,4 @@ Title = "This is a test" Description = "Test file" Tags = ["a", "b"] +++ +# Lorem ipsum diff --git a/test/index.test.js b/test/index.test.js index ebac87d..897438c 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -5,7 +5,7 @@ const request = require('supertest') process.env.POST_DIR = path.resolve(__dirname, '../test/data/') const app = require('../src/index.js') -test(t => { +test('homepage', t => { return request(app.listen()) .get('/') .expect(200) @@ -13,3 +13,11 @@ test(t => { .expect(/This is a test/) .then(() => t.pass()) }) + +test('post', t => { + return request(app.listen()) + .get('/post/testfile') + .expect(200) + .expect(/Lorem ipsum/) + .then(() => t.pass()) +}) diff --git a/test/modules/posts.test.js b/test/modules/posts.test.js index 28a25f2..cdbf22a 100644 --- a/test/modules/posts.test.js +++ b/test/modules/posts.test.js @@ -24,8 +24,10 @@ test('getFolder', t => { }) ) const actual = posts.getFolder(path.resolve(__dirname, '../data/')) - t.true(Array.isArray(actual), 'must return an array') - t.true(actual.length > 0, 'must return a non-empty array') - t.is(actual[0].path, path.resolve(__dirname, '../data/testfile.md')) - t.deepEqual(actual[0].data, expected) + t.true(actual.size > 0, 'must return a non-empty map') + t.is( + actual.get('testfile').path, + path.resolve(__dirname, '../data/testfile.md') + ) + t.deepEqual(actual.get('testfile').data, expected) }) |