From c9bc836ecd048a1e386ba48941caf16880b3bd6c Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Sun, 12 Jan 2025 21:40:21 +0800 Subject: [PATCH 1/2] fix: hexo.locals.get('posts') doesn't show all posts --- lib/hexo/index.ts | 3 + test/scripts/extend/tag.ts | 114 +++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) diff --git a/lib/hexo/index.ts b/lib/hexo/index.ts index 86926900a1..faaed9da33 100644 --- a/lib/hexo/index.ts +++ b/lib/hexo/index.ts @@ -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'); diff --git a/test/scripts/extend/tag.ts b/test/scripts/extend/tag.ts index cad5876d18..5c914bd600 100644 --- a/test/scripts/extend/tag.ts +++ b/test/scripts/extend/tag.ts @@ -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['process']> +type PostReturn = ReturnType['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 = 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(); @@ -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); + }); }); From 170840738b61d2ff8360520885f87697ecc2e8f6 Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Mon, 13 Jan 2025 22:25:31 +0800 Subject: [PATCH 2/2] fix --- test/scripts/extend/tag.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/scripts/extend/tag.ts b/test/scripts/extend/tag.ts index 5c914bd600..bbe7b643ff 100644 --- a/test/scripts/extend/tag.ts +++ b/test/scripts/extend/tag.ts @@ -224,7 +224,7 @@ describe('Tag', () => { }); }); - it.only('tag should get right locals', async () => { + it('tag should get right locals', async () => { let count = 0; hexo.extend.filter = new Filter(); hexo.extend.tag = new Tag();