From 529946dc5bdccc2153c97e2040953047319480f4 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Tue, 3 Oct 2017 17:28:29 +0200 Subject: Add tags listing --- src/actions.js | 9 +++++ src/app.js | 2 + src/responders.js | 22 +++++++++- src/templates/tags.html | 5 +++ test/app.test.js | 51 ++++++++++++++++++++++- test/snapshots/app.test.js.md | 87 +++++++++++++++++++++++++++++++++++++++- test/snapshots/app.test.js.snap | Bin 1936 -> 2388 bytes 7 files changed, 172 insertions(+), 4 deletions(-) create mode 100644 src/templates/tags.html diff --git a/src/actions.js b/src/actions.js index 61f4fd8..50b8079 100644 --- a/src/actions.js +++ b/src/actions.js @@ -50,6 +50,14 @@ function post(config, responder, posts) { }; } +function tags(config, responder, tags) { + return async function(ctx, next) { + responder(ctx, config, { + tags: tags.keys() + }); + }; +} + function tag(config, responder, items) { return async function(ctx, next) { const tag = ctx.params.name; @@ -77,6 +85,7 @@ module.exports = { posts, highlightTheme, post, + tags, tag, serveFiles }; diff --git a/src/app.js b/src/app.js index 2c23258..20ffb69 100644 --- a/src/app.js +++ b/src/app.js @@ -53,6 +53,8 @@ module.exports = async function() { actions.post(config, responders.post, Posts.posts) ); + router.get("tags", "/tag", actions.tags(config, responders.tags, Posts.tags)); + router.get( "tag", "/tag/:name", diff --git a/src/responders.js b/src/responders.js index f4ea0a6..8260734 100644 --- a/src/responders.js +++ b/src/responders.js @@ -27,7 +27,9 @@ const postIndentLevel = baseIndentLevel + getTemplateIndent(findPostContent, "post.html"); function indentForTemplate(text, indentLevel = 0) { - return indent(text, indentLevel).slice(indentLevel).replace(/\n+$/, ""); + return indent(text, indentLevel) + .slice(indentLevel) + .replace(/\n+$/, ""); } function templateReader(template, indentLevel) { @@ -39,6 +41,7 @@ const templates = { home: templateReader("home.html", baseIndentLevel), post: templateReader("post.html", baseIndentLevel), list: templateReader("list.html", baseIndentLevel), + tags: templateReader("tags.html", baseIndentLevel), feed: templateReader("feed.xml") }; @@ -135,6 +138,23 @@ module.exports = { ); }, + tags(ctx, config, { tags }) { + ctx.type = "html"; + + ctx.body = layout( + config, + "Tags", + hyperfast(templates.tags, { + ".tag": Array.from(tags).map(tag => ({ + ".u-url": { + href: ctx.getURL("tag", tag), + _text: Case.title(tag) + } + })) + }) + ); + }, + post(ctx, config, { post }) { ctx.type = "html"; diff --git a/src/templates/tags.html b/src/templates/tags.html new file mode 100644 index 0000000..01a7e37 --- /dev/null +++ b/src/templates/tags.html @@ -0,0 +1,5 @@ + diff --git a/test/app.test.js b/test/app.test.js index 33c8905..a3f06a1 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -45,8 +45,20 @@ test("homepage", async function(t) { t.is($("head > title").text(), "John Doe", "head title is site author"); t.is($("main").length, 1, "only one
tag"); - t.is($("nav a").first().text(), "Home", "nav link has text"); - t.is($("nav a").first().attr("href"), "/", "nav links to homepage"); + t.is( + $("nav a") + .first() + .text(), + "Home", + "nav link has text" + ); + t.is( + $("nav a") + .first() + .attr("href"), + "/", + "nav links to homepage" + ); t.is( $("a[rel=me]").length, 2, @@ -131,6 +143,41 @@ test("post", async function(t) { }); test("tags", async function(t) { + const res = await request(app.listen()).get("/tag"); + + t.is(res.statusCode, 200); + t.is(res.type, "text/html"); + t.is(res.charset, "utf-8"); + t.regex(res.text, /^/); + + const $ = parseResponse(res); + + t.is( + $("head > title").text(), + "Tags · John Doe", + "head title contains 'Tags' and site name" + ); + + t.is($(".tags > .tag").length, 2, "lists two tags"); + + t.is( + $(".tag .p-name") + .first() + .text(), + "A", + "sets tag text" + ); + + t.is( + $(".tag .u-url") + .first() + .attr("href"), + "/tag/a", + "sets tag link" + ); +}); + +test("tag", async function(t) { const res = await request(app.listen()).get("/tag/a"); t.is(res.statusCode, 200); diff --git a/test/snapshots/app.test.js.md b/test/snapshots/app.test.js.md index 306f2ce..cf7b0f3 100644 --- a/test/snapshots/app.test.js.md +++ b/test/snapshots/app.test.js.md @@ -332,4 +332,89 @@ Generated by [AVA](https://ava.li). '/css/code.css', ], }, - } + + +## tag + +> should contain relevant microformats data + + { + items: [ + { + properties: { + name: [ + 'John Doe', + ], + note: [ + 'Nobody in particular', + ], + photo: [ + '/static/johndoe.jpg', + ], + url: [ + '/', + ], + }, + type: [ + 'h-card', + ], + }, + { + children: [ + { + properties: { + name: [ + 'This is a test', + ], + published: [ + '2017-01-01T00:00:00.000Z', + ], + url: [ + '/post/testfile', + ], + }, + type: [ + 'h-entry', + ], + value: 'This is a test Sunday, January 1, 2017', + }, + ], + properties: { + name: [ + 'A · John Doe', + ], + }, + type: [ + 'h-feed', + ], + }, + ], + 'rel-urls': { + '/css/code.css': { + rels: [ + 'stylesheet', + ], + }, + 'https://twitter.com/johndoe': { + rels: [ + 'me', + ], + text: 'Twitter', + }, + 'mailto:johndoe@johndoe.org': { + rels: [ + 'me', + ], + text: 'johndoe@johndoe.org', + }, + }, + rels: { + me: [ + 'mailto:johndoe@johndoe.org', + 'https://twitter.com/johndoe', + ], + stylesheet: [ + '/css/code.css', + ], + }, + } \ No newline at end of file diff --git a/test/snapshots/app.test.js.snap b/test/snapshots/app.test.js.snap index df5f772..0d8d31d 100644 Binary files a/test/snapshots/app.test.js.snap and b/test/snapshots/app.test.js.snap differ -- cgit 1.4.1