[KeystoneJS] ブログ機能の URL /blog の変更方法

Tadashi Shigeoka ·  Sun, August 13, 2017

KeystoneJS のブログ機能の URL は /blog と固定になっていますが、これを /news など他の URL に変更してみたので、その手順をご紹介します。

と言っても、/blogBlog などの文字列がソースコード上にあるので、それを全て置換するだけです。

[前提] KeystoneJS のバージョン

[email protected]

KeystoneJS ブログ機能の URL 変更方法

以下のように /blog Blog blog などの文字列を /news News news などに変更をしました。

diff --git a/routes/index.js b/routes/index.js
index 231db96..700ec6d 100644
--- a/routes/index.js
+++ b/routes/index.js
@@ -35,8 +35,8 @@ var routes = {
 exports = module.exports = function (app) {
        // Views
        app.get('/', routes.views.index);
-       app.get('/blog/:category?', routes.views.blog);
-       app.get('/blog/post/:post', routes.views.post);
+       app.get('/news/:category?', routes.views.news);
+       app.get('/news/post/:post', routes.views.post);
        app.get('/gallery', routes.views.gallery);
        app.all('/contact', routes.views.contact);
diff --git a/routes/middleware.js b/routes/middleware.js
index 01abc9b..1534f41 100644
--- a/routes/middleware.js
+++ b/routes/middleware.js
@@ -20,7 +20,7 @@ var _ = require('lodash');
 exports.initLocals = function (req, res, next) {
        res.locals.navLinks = [
                { label: 'Home', key: 'home', href: '/' },
-               { label: 'Blog', key: 'blog', href: '/blog' },
+               { label: 'News', key: 'news', href: '/news' },
                { label: 'Gallery', key: 'gallery', href: '/gallery' },
                { label: 'Contact', key: 'contact', href: '/contact' },
        ];
diff --git a/routes/views/blog.js b/routes/views/news.js
similarity index 97%
rename from routes/views/blog.js
rename to routes/views/news.js
index 27b17e9..a822d25 100644
--- a/routes/views/blog.js
+++ b/routes/views/news.js
@@ -7,7 +7,7 @@ exports = module.exports = function (req, res) {
        var locals = res.locals;
 
        // Init locals
-       locals.section = 'blog';
+       locals.section = 'news';
        locals.filters = {
                category: req.params.category,
        };
@@ -79,5 +79,5 @@ exports = module.exports = function (req, res) {
        });
 
        // Render the view
-       view.render('blog');
+       view.render('news');
 };
diff --git a/routes/views/post.js b/routes/views/post.js
index bb1a2be..bdeb851 100644
--- a/routes/views/post.js
+++ b/routes/views/post.js
@@ -6,7 +6,7 @@ exports = module.exports = function (req, res) {
        var locals = res.locals;
 
        // Set locals
-       locals.section = 'blog';
+       locals.section = 'news';
        locals.filters = {
                post: req.params.post,
        };
diff --git a/templates/views/blog.pug b/templates/views/news.pug
similarity index 87%
rename from templates/views/blog.pug
rename to templates/views/news.pug
index e471e12..8885f69 100644
--- a/templates/views/blog.pug
+++ b/templates/views/news.pug
@@ -2,14 +2,14 @@ extends ../layouts/default
 
 mixin post(post)
        .post(data-ks-editable=editable(user, { list: 'Post', id: post.id }))
-               h2: a(href='/blog/post/' + post.slug)= post.title
+               h2: a(href='/news/post/' + post.slug)= post.title
                p.lead.text-muted Posted 
                        if post.publishedDate
                                | on #{post._.publishedDate.format('MMMM Do, YYYY')} 
                        if post.categories && post.categories.length
                                | in 
                                each cat, i in post.categories
-                                       a(href='/blog/' + cat.key)= cat.name
+                                       a(href='/news/' + cat.key)= cat.name
                                        if i < post.categories.length - 1
                                                | , 
                                        else
@@ -20,11 +20,11 @@ mixin post(post)
                        img(src=post._.image.fit(160,160)).img.pull-right
                p!= post.content.brief
                if post.content.extended
-                       p.read-more: a(href='/blog/post/' + post.slug) Read more...
+                       p.read-more: a(href='/news/post/' + post.slug) Read more...
 
 block intro
        .container
-               h1= data.category ? data.category.name : 'Blog'
+               h1= data.category ? data.category.name : 'News'
 
 block content
        .container: .row
@@ -70,6 +70,6 @@ block content
                        .col-sm-4.col-md-3
                                h2 Categories
                                .list-group(style='margin-top: 70px;')
-                                       a(href='/blog', class=!data.category ? 'active' : false).list-group-item All Categories
+                                       a(href='/news', class=!data.category ? 'active' : false).list-group-item All Categories
                                        each cat in data.categories
-                                               a(href='/blog/' + cat.key, class=data.category && data.category.id == cat.id ? 'active' : false).list-group-item= cat.name
+                                               a(href='/news/' + cat.key, class=data.category && data.category.id == cat.id ? 'active' : false).list-group-item= cat.name
diff --git a/templates/views/post.pug b/templates/views/post.pug
index 68fe59b..3352af9 100644
--- a/templates/views/post.pug
+++ b/templates/views/post.pug
@@ -3,7 +3,7 @@ extends ../layouts/default
 block content
        .container: .row: .col-sm-10.col-sm-offset-1.col-md-8.col-md-offset-2
                article
-                       p: a(href='/blog') ← back to the blog
+                       p: a(href='/news') ← back to the news
                        hr
                        if !data.post
                                h2 Invalid Post.
@@ -16,7 +16,7 @@ block content
                                                if data.post.categories && data.post.categories.length
                                                        | in 
                                                        each cat, cat_i in data.post.categories
-                                                               a(href='/blog/' + cat.key)= cat.name
+                                                               a(href='/news/' + cat.key)= cat.name
                                                                if cat_i < data.post.categories.length - 1
                                                                        | , 
                                                                else

幸い MongoDB の中に /blog のような routing の設定はなかったので、簡単に URL を変更することができました。

まだ KeystoneJS を使い始めたばかりですが少しずつカスタマイズしていきたいと思います。