Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hexo.locals.get('posts') doesn't show all posts #5612

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('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);
});
});
Loading