更细致地调整了部分间距。
现在,在 goalkeeper 那边提到的最后一个问题也解决了:新专栏——“页边空白”!现在已经能从页面左侧进入。
搞这么一个新栏目还是稍微费点功夫的。我的目标是通过自定义字段 group: narrow-margin,来区分普通文章和“页边空白”文章,并让主页只显示普通文章,而“页边空白”文章则集中显示在专栏页面里。
首先要创建如下的 js 脚本 /scripts/filter_posts.js,用来过滤文章列表:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| const pagination = require('hexo-pagination');
hexo.extend.filter.register('before_generate', function() { const allPosts = hexo.locals.get('posts'); const mainPosts = allPosts.filter(post => !(post.group && post.group === 'narrow-margin')); const narrowMarginPosts = allPosts.filter(post => post.group && post.group === 'narrow-margin'); hexo.locals.set('main_posts', mainPosts.sort('-date')); hexo.locals.set('narrow_margin_posts', narrowMarginPosts.sort('-date')); });
hexo.extend.generator.register('my_index_generator', function(locals) { const posts = locals.main_posts; if (!posts || posts.length === 0) return;
const config = hexo.config.my_index_generator || {}; const perPage = config.per_page || 10; return pagination('', posts, { perPage: perPage, layout: ['index', 'archive'], format: 'page/%d/', }); });
hexo.extend.generator.register('narrow_margin_generator', function(locals) { const posts = locals.narrow_margin_posts; if (!posts || posts.length === 0) return;
const config = hexo.config.narrow_margin_generator || {}; const perPage = config.per_page || 10; const path = config.path || 'narrow-margin';
return pagination(path, posts, { perPage: perPage, layout: ['narrow-margin', 'archive'], format: 'page/%d/', data: { title: 'Narrow Margin Archive' } }); });
|
这样一来,我们以后就可以用 site.narrow_margin_posts 来访问“页边空白”文章列表,同时用 site.main_posts 来访问普通文章列表了。
然后在 /themes/next/layout/ 下新建 narrow-margin.njk,内容大致是复制 archive.njk,改一些关键的地方就可以。
顺便修复了 more 会跳转到文章中间的问题,现在点击 more 会跳转到文章顶部了,这是通过把 /themes/next/layout/_macro/post.njk 里的
1 2 3 4 5 6 7
| {%- if theme.read_more_btn %} <div class="post-button"> <a class="btn" href="{{ url_for(post.path) }}#more" rel="contents"> {{ __('post.read_more') }} » </a> </div> {%- endif %}
|
去掉 #more 实现的。要是有人在网址后面直接加 #more 跳转倒也能跳到中间位置。
这代码块怎么不自带 njk 的语法……当纯文本凑合看吧。
等着,很快就会开始更新数学了