Generator

  • 2018-12-03

A generator builds routes based on processed files.

Synopsis

1
2
3
hexo.extend.generator.register(name, function(locals){
// ...
});

A locals argument will get passed into the function, containing the site variables. You should use this argument to get the website data, thereby avoiding having to access the database directly.

Update Routes

1
2
3
4
5
6
7
8
9
10
11
12
13
hexo.extend.generator.register('test', function(locals){
// Object
return {
path: 'foo',
data: 'foo'
};

// Array
return [
{path: 'foo', data: 'foo'},
{path: 'bar', data: 'bar'}
];
});
Attribute Description
path Path not including the prefixing /.
data Data
layout Layout. Specify the layouts for rendering. The value can be a string or an array. If it’s ignored then the route will return data directly.

When the source files are updated, Hexo will execute all generators and rebuild the routes. Please return the data and do not access the router directly.

Example

Archive Page

Create an archive page at archives/index.html. We pass all posts as data to the templates. This data is equivalent to the page variable in templates.

Next, set the layout attribute to render with the theme templates. We’re setting two layouts in this example: if the archive layout doesn’t exist, the index layout will be used instead.

1
2
3
4
5
6
7
hexo.extend.generator.register('archive', function(locals){
return {
path: 'archives/index.html',
data: locals,
layout: ['archive', 'index']
}
});

Archive Page with Pagination

You can use the convenient official tool hexo-pagination to easily build archive pages with pagination.

1
2
3
4
5
6
7
8
9
var pagination = require('hexo-pagination');

hexo.extend.generator.register('archive', function(locals){
return pagination('archives/index.html', locals.posts, {
perPage: 10,
layout: ['archive', 'index'],
data: {}
});
});

Generate All Posts

Iterate over all posts in locals.posts and create routes for all the posts.

1
2
3
4
5
6
7
8
9
hexo.extend.generator.register('post', function(locals){
return locals.posts.map(function(post){
return {
path: post.path,
data: post,
layout: 'post'
};
});
});

Copy Files

This time we don’t return the data explicitly but instead set data to a function so the route will build fs.ReadStream only when needed.

1
2
3
4
5
6
7
8
9
10
var fs = require('hexo-fs');

hexo.extend.generator.register('asset', function(locals){
return {
path: 'file.txt',
data: function(){
return fs.createReadStream('path/to/file.txt')
}
};
});

扫码领红包

2018双11超级红包
PC访问链接: 2018双11超级红包 预售开启 汇聚全球潮流新品

淘口令:¥20Y0b6q4eHR¥ (复制此行再打开手机淘宝客访问)

最后更新: 2018年12月03日 09:29

原始链接: https://cyc.oy99.com/api/generator.html

× 多少都行~
打赏二维码
×
  • {title}