From 2e8ec742147c9ed2dbf3cb819029bf3c30cbbc78 Mon Sep 17 00:00:00 2001 From: dailyrandomphoto Date: Tue, 10 Dec 2019 23:50:31 +0800 Subject: [PATCH] permalink_defaults should not overwrite values --- lib/plugins/filter/post_permalink.js | 33 ++++++++++++++++---------- test/scripts/filters/post_permalink.js | 24 +++++++++++++++++++ 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/lib/plugins/filter/post_permalink.js b/lib/plugins/filter/post_permalink.js index 588f826afd..084aeccf14 100644 --- a/lib/plugins/filter/post_permalink.js +++ b/lib/plugins/filter/post_permalink.js @@ -6,18 +6,19 @@ let permalink; function postPermalinkFilter(data) { const { config } = this; + const { id, _id, slug, title, date } = data; const meta = { - id: data.id || data._id, - title: data.slug, - name: typeof data.slug === 'string' ? basename(data.slug) : '', - post_title: slugize(data.title, {transform: 1}), - year: data.date.format('YYYY'), - month: data.date.format('MM'), - day: data.date.format('DD'), - hour: data.date.format('HH'), - minute: data.date.format('mm'), - i_month: data.date.format('M'), - i_day: data.date.format('D') + id: id || _id, + title: slug, + name: typeof slug === 'string' ? basename(slug) : '', + post_title: slugize(title, {transform: 1}), + year: date.format('YYYY'), + month: date.format('MM'), + day: date.format('DD'), + hour: date.format('HH'), + minute: date.format('mm'), + i_month: date.format('M'), + i_day: date.format('D') }; if (!permalink || permalink.rule !== config.permalink) { @@ -42,7 +43,15 @@ function postPermalinkFilter(data) { Object.defineProperty(meta, key, Object.getOwnPropertyDescriptor(data, key)); } - return permalink.stringify(Object.assign(meta, config.permalink_defaults)); + const keys2 = Object.keys(config.permalink_defaults); + + for (const key of keys2) { + if (Object.prototype.hasOwnProperty.call(meta, key)) continue; + + meta[key] = config.permalink_defaults[key]; + } + + return permalink.stringify(meta); } module.exports = postPermalinkFilter; diff --git a/test/scripts/filters/post_permalink.js b/test/scripts/filters/post_permalink.js index 17967ab6f3..23a708fb03 100644 --- a/test/scripts/filters/post_permalink.js +++ b/test/scripts/filters/post_permalink.js @@ -125,4 +125,28 @@ describe('post_permalink', () => { return Post.removeById(post._id); }); }); + + it('permalink_defaults', () => { + hexo.config.permalink = 'posts/:lang/:title/'; + const orgPermalinkDefaults = hexo.config.permalink_defaults; + hexo.config.permalink_defaults = {lang: 'en'}; + + return Post.insert([{ + source: 'my-new-post.md', + slug: 'my-new-post', + title: 'My New Post1' + }, { + source: 'my-new-fr-post.md', + slug: 'my-new-fr-post', + title: 'My New Post2', + lang: 'fr' + }]).then(posts => { + postPermalink(posts[0]).should.eql('posts/en/my-new-post/'); + postPermalink(posts[1]).should.eql('posts/fr/my-new-fr-post/'); + + hexo.config.permalink = PERMALINK; + hexo.config.permalink_defaults = orgPermalinkDefaults; + return Promise.all(posts.map(post => Post.removeById(post._id))); + }).then(); + }); });