Skip to content

Commit

Permalink
feat: custom layout (#96)
Browse files Browse the repository at this point in the history
* feat: custom layout

* test: add test case

* docs

* test: adapt to latest testsuite
  • Loading branch information
uiolee authored Jul 9, 2024
1 parent 67d7923 commit 9c0bd9e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ index_generator:
per_page: 10
order_by: -date
pagination_dir: page
layout: ["index", "archive"]
```
- **path**: Root path for your blog's index page.
Expand All @@ -36,6 +37,8 @@ index_generator:
- **pagination_dir**: URL format.
- default: `page`
- e.g. set `awesome-page` makes the URL ends with `awesome-page/<page number>` for second page and beyond.
- **layout**: custom layout.
- defalut: `["index", "archive"]`

## Usage

Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

hexo.config.index_generator = Object.assign({
per_page: typeof hexo.config.per_page === 'undefined' ? 10 : hexo.config.per_page,
order_by: '-date'
order_by: '-date',
layout: ['index', 'archive']
}, hexo.config.index_generator);

hexo.extend.generator.register('index', require('./lib/generator'));
2 changes: 1 addition & 1 deletion lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = function(locals) {

return pagination(path, posts, {
perPage: config.index_generator.per_page,
layout: ['index', 'archive'],
layout: config.index_generator.layout || ['index', 'archive'],
format: paginationDir + '/%d/',
data: {
__index: true
Expand Down
13 changes: 13 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,17 @@ describe('Index generator', () => {
result[2].path.should.eql('yoyo/3/');

});

it('custom layout', () => {
const custom_layout = ['custom', 'archive', 'index'];
hexo.config.index_generator.layout = [...custom_layout];

const result = generator(locals);

result.forEach(res => {
res.layout.should.eql(custom_layout);
});

});

});

0 comments on commit 9c0bd9e

Please sign in to comment.