Skip to content

Commit

Permalink
fix: hexo.locals.get('posts') doesn't show all posts
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon committed Jan 12, 2025
1 parent bcfb030 commit c9bc836
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/hexo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,9 @@ class Hexo extends EventEmitter {
this.emit('generateBefore');

// Run before_generate filters
// https://github.com/hexojs/hexo/issues/5287
// locals should be invalidated before before_generate filters because tags may use locals
this.locals.invalidate();
return this.execFilter('before_generate', null, { context: this })
.then(() => this._routerRefresh(this._runGenerators(), useCache)).then(() => {
this.emit('generateAfter');
Expand Down
114 changes: 114 additions & 0 deletions test/scripts/extend/tag.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,53 @@
import { join } from 'path';
import Tag from '../../../lib/extend/tag';
import chai from 'chai';
import Hexo from '../../../lib/hexo';
import defaultConfig from '../../../lib/hexo/default_config';
import posts from '../../../lib/plugins/processor/post';
import Filter from '../../../lib/extend/filter';
import renderPostFilter from '../../../lib/plugins/filter/before_generate/render_post';
import { mkdirs, rmdir, writeFile } from 'hexo-fs';
// @ts-ignore
import Promise from 'bluebird';
const should = chai.should();

type PostParams = Parameters<ReturnType<typeof posts>['process']>
type PostReturn = ReturnType<ReturnType<typeof posts>['process']>

describe('Tag', () => {
const tag = new Tag();

const baseDir = join(__dirname, 'post_test');
const hexo = new Hexo(baseDir);
const post = posts(hexo);
const process: (...args: PostParams) => Promise<PostReturn> = Promise.method(post.process.bind(hexo));
const { source } = hexo;
const { File } = source;

function newFile(options) {
const { path } = options;

options.path = (options.published ? '_posts' : '_drafts') + '/' + path;
options.source = join(source.base, options.path);

options.params = {
published: options.published,
path,
renderable: options.renderable
};

return new File(options);
}

before(async () => {
await mkdirs(baseDir);
hexo.init();
});

beforeEach(() => { hexo.config = Object.assign({}, defaultConfig); });

after(() => rmdir(baseDir));

it('register()', async () => {
const tag = new Tag();

Expand Down Expand Up @@ -180,4 +223,75 @@ describe('Tag', () => {
spy.should.eql(true);
});
});

it.only('tag should get right locals', async () => {
let count = 0;
hexo.extend.filter = new Filter();
hexo.extend.tag = new Tag();
hexo.extend.tag.register('series', () => {
count = hexo.locals.get('posts').length;
return '';
}, {ends: false});
hexo.extend.filter.register('before_generate', renderPostFilter.bind(hexo));

const body1 = [
'title: "test1"',
'date: 2023-09-03 16:59:42',
'tags: foo',
'---',
'{% series %}'
].join('\n');

const file = newFile({
path: 'test1.html',
published: true,
type: 'create',
renderable: true
});

const body2 = [
'---',
'title: test2',
'date: 2023-09-03 16:59:46',
'tags: foo',
'---'
];

const file2 = newFile({
path: 'test2.html',
published: true,
type: 'create',
renderable: true
});

const body3 = [
'title: test3',
'date: 2023-09-03 16:59:49',
'tags: foo',
'---'
];

const file3 = newFile({
path: 'test3.html',
published: true,
type: 'create',
renderable: true
});

await Promise.all([
writeFile(file.source, body1),
writeFile(file2.source, body2),
writeFile(file3.source, body3)
]);

await Promise.all([
process(file),
process(file2),
process(file3)
]);

await hexo._generate({ cache: false });

count.should.eql(3);
});
});

0 comments on commit c9bc836

Please sign in to comment.