这个原理我不太懂,直接引用好友完善后的代码。
首先修改主题中的模板文件 post-card.hbs
(<ghost>/content/themes/casper/partials/post-card.hbs);
<section class="post-card-excerpt">
<p>{{excerpt characters="80" round="true"}}</p>
</section>
再修改 Ghost 的 expert.js
(<ghost>/current/core/server/helpers/excerpt.js)
var downsize = require('downsize');
var proxy = require('./proxy'),
_ = require('lodash'),
SafeString = proxy.SafeString,
getMetaDataExcerpt = proxy.metaData.getMetaDataExcerpt;
module.exports = function excerpt(options) {
var truncateOptions = (options || {}).hash || {};
truncateOptions = _.pick(truncateOptions, ['words', 'characters', 'append', 'round']);
_.keys(truncateOptions).map(function (key) {
switch (key) {
case "words":
case "characters":
truncateOptions[key] = parseInt(truncateOptions[key], 10);
break;
case "round":
truncateOptions[key] = String(truncateOptions[key]).toLowerCase() === "true";
break;
}
});
var result = downsize(String(this.html), truncateOptions);
// Strip inline and bottom footnotes
result = result.replace(/<a href="#fn.*?rel="footnote">.*?<\/a>/gi, '');
result = result.replace(/<div class="footnotes"><ol>.*?<\/ol><\/div>/, '');
// Strip other html
result = result.replace(/<\/?[^>]+>/gi, '');
result = result.replace(/(\r\n|\n|\r)+/gm, ' ');
return new SafeString(result);
};
保存并重启 Ghost 即可看到优化后的中文摘要。
后记
Ghost 的默认主题 Casper 在手动修改摘要后还是蛮好看的……
所以现在没有用这个修改了。
参考: