Skip to content

Commit

Permalink
fix: permalink_defaults should not overwrite values (hexojs#3926)
Browse files Browse the repository at this point in the history
  • Loading branch information
dailyrandomphoto authored and Thomas Parisot committed Jan 17, 2020
1 parent 4787961 commit 44d053a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
33 changes: 21 additions & 12 deletions lib/plugins/filter/post_permalink.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
24 changes: 24 additions & 0 deletions test/scripts/filters/post_permalink.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

0 comments on commit 44d053a

Please sign in to comment.