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(postPermalinkFilter): permalink_defaults should not overwrite values #3926

Merged
merged 1 commit into from
Dec 11, 2019
Merged
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
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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];
Copy link
Member

@SukkaW SukkaW Dec 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try deepMerge() to see if Maximum call stack size exceeded still occurs.


Update

According to the documents of lodash.defaults:

Source objects are applied from left to right. Once a property is set, additional values of the same property are ignored.

Yeah, it is my mistake to replace lodash.defaults with Object.assign.

}

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();
});
});