Skip to content

Commit 4a1e203

Browse files
D-Sketonuiolee
andauthored
fix: hexo.locals.get('posts') doesn't show all posts (hexojs#5612)
* fix: hexo.locals.get('posts') doesn't show all posts * fix --------- Co-authored-by: Uiolee <22849383+uiolee@users.noreply.github.com>
1 parent dbf1ce1 commit 4a1e203

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

lib/hexo/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,9 @@ class Hexo extends EventEmitter {
705705
this.emit('generateBefore');
706706

707707
// Run before_generate filters
708+
// https://github.com/hexojs/hexo/issues/5287
709+
// locals should be invalidated before before_generate filters because tags may use locals
710+
this.locals.invalidate();
708711
return this.execFilter('before_generate', null, { context: this })
709712
.then(() => this._routerRefresh(this._runGenerators(), useCache)).then(() => {
710713
this.emit('generateAfter');

test/scripts/extend/tag.ts

+114
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,53 @@
1+
import { join } from 'path';
12
import Tag from '../../../lib/extend/tag';
23
import chai from 'chai';
4+
import Hexo from '../../../lib/hexo';
5+
import defaultConfig from '../../../lib/hexo/default_config';
6+
import posts from '../../../lib/plugins/processor/post';
7+
import Filter from '../../../lib/extend/filter';
8+
import renderPostFilter from '../../../lib/plugins/filter/before_generate/render_post';
9+
import { mkdirs, rmdir, writeFile } from 'hexo-fs';
10+
// @ts-ignore
11+
import Promise from 'bluebird';
312
const should = chai.should();
413

14+
type PostParams = Parameters<ReturnType<typeof posts>['process']>
15+
type PostReturn = ReturnType<ReturnType<typeof posts>['process']>
16+
517
describe('Tag', () => {
618
const tag = new Tag();
719

20+
const baseDir = join(__dirname, 'post_test');
21+
const hexo = new Hexo(baseDir);
22+
const post = posts(hexo);
23+
const process: (...args: PostParams) => Promise<PostReturn> = Promise.method(post.process.bind(hexo));
24+
const { source } = hexo;
25+
const { File } = source;
26+
27+
function newFile(options) {
28+
const { path } = options;
29+
30+
options.path = (options.published ? '_posts' : '_drafts') + '/' + path;
31+
options.source = join(source.base, options.path);
32+
33+
options.params = {
34+
published: options.published,
35+
path,
36+
renderable: options.renderable
37+
};
38+
39+
return new File(options);
40+
}
41+
42+
before(async () => {
43+
await mkdirs(baseDir);
44+
hexo.init();
45+
});
46+
47+
beforeEach(() => { hexo.config = Object.assign({}, defaultConfig); });
48+
49+
after(() => rmdir(baseDir));
50+
851
it('register()', async () => {
952
const tag = new Tag();
1053

@@ -180,4 +223,75 @@ describe('Tag', () => {
180223
spy.should.eql(true);
181224
});
182225
});
226+
227+
it('tag should get right locals', async () => {
228+
let count = 0;
229+
hexo.extend.filter = new Filter();
230+
hexo.extend.tag = new Tag();
231+
hexo.extend.tag.register('series', () => {
232+
count = hexo.locals.get('posts').length;
233+
return '';
234+
}, {ends: false});
235+
hexo.extend.filter.register('before_generate', renderPostFilter.bind(hexo));
236+
237+
const body1 = [
238+
'title: "test1"',
239+
'date: 2023-09-03 16:59:42',
240+
'tags: foo',
241+
'---',
242+
'{% series %}'
243+
].join('\n');
244+
245+
const file = newFile({
246+
path: 'test1.html',
247+
published: true,
248+
type: 'create',
249+
renderable: true
250+
});
251+
252+
const body2 = [
253+
'---',
254+
'title: test2',
255+
'date: 2023-09-03 16:59:46',
256+
'tags: foo',
257+
'---'
258+
];
259+
260+
const file2 = newFile({
261+
path: 'test2.html',
262+
published: true,
263+
type: 'create',
264+
renderable: true
265+
});
266+
267+
const body3 = [
268+
'title: test3',
269+
'date: 2023-09-03 16:59:49',
270+
'tags: foo',
271+
'---'
272+
];
273+
274+
const file3 = newFile({
275+
path: 'test3.html',
276+
published: true,
277+
type: 'create',
278+
renderable: true
279+
});
280+
281+
await Promise.all([
282+
writeFile(file.source, body1),
283+
writeFile(file2.source, body2),
284+
writeFile(file3.source, body3)
285+
]);
286+
287+
await Promise.all([
288+
process(file),
289+
process(file2),
290+
process(file3)
291+
]);
292+
293+
await hexo._generate({ cache: false });
294+
295+
count.should.eql(3);
296+
});
183297
});

0 commit comments

Comments
 (0)