今天把 Blog 升级到 Ghost 0.5.1 了。
但是由于 CentOS 6.5 的 glibc 没有支持到 2.14,索性装上了 CentOS7。
现在运行博客时,运用 --production
参数,就能默认开启 gzip 和 js 的压缩了。终于不用自己优化 js 文件,自己搞 gzip 了。特别轻松。
但是这次还是没有加入 sitemap 的功能,所以用老办法,hack into ghost :P
Hack into ghost
先在 Ghost 的目录下运行:
然后编辑以下几个文件:
- /core/server/routes/frontend.js
1 2 3
| server.get('/sitemap.xml', frontend.sitemap); // <- 添加这行 server.get('/rss/', frontend.rss); server.get('/rss/:page/', frontend.rss);
|
- /core/server/controllers/frontend.js
1 2 3
| when = require('when'), Route = require('express').Route, sm = require('sitemap'), <- 添加这行
|
在一堆 require 下面添加一个函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| function buildSitemap(posts, done, sitemap) { var sitemap = sitemap || sm.createSitemap ({ hostname: "http://yourdomain.com", cacheTime: 600000 });
if(posts.length > 0) { var post = posts.shift(); sitemap.add({ url: '/' + post.slug + '/' }); process.nextTick(buildSitemap.bind(this, posts, done, sitemap)); } else { sitemap.toXML(function(xml) { done(xml); }); } }
|
在 frontendControllers 中添加一个 sitemap 方法:
1 2 3 4 5 6 7 8 9 10
| frontendControllers = { 'sitemap': function(req, res, next) { api.posts.browse({ staticPages: 'all', limit: 1000 }).then(function(result) { buildSitemap(result.posts, function(sitemap) { res.header('Content-Type', 'application/xml'); res.send(sitemap); }); }); }, 'homepage': function (req, res, next) {
|
然后重启你的 Ghost 博客,就 ok 了。
例子: http://www.fancycoding.com/sitemap.xml