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

Conversation

dailyrandomphoto
Copy link
Member

What does it do?

permalink_defaults option does not work correctly from 4.1.0.
According https://hexo.io/docs/permalinks, set _config.yml as

permalink: :lang/:title/
permalink_defaults:
  lang: en
$ hexo new "Hello World" --lang tw

and the URL will be:

# expected is
http://localhost:4000/tw/hello-world/

# but actual is 
http://localhost:4000/en/hello-world/

Compare v4.0.0 and v4.1.0,
I found the related change is e0f2478#diff-dbb03cb234189f8e709e7c5f9921aa67L45 (#3790)

- return permalink.stringify(defaults(meta, config.permalink_defaults));
+ return permalink.stringify(Object.assign(meta, config.permalink_defaults));

If change to

Object.assign([], config.permalink_defaults, meta)

it causes "Maximum call stack size exceeded" error, so use for loop to copy values.

How to test

git clone -b fix-permalinkDefaults https://github.com/dailyrandomphoto/hexo.git
cd hexo
npm install
npm test

Pull request tasks

  • Add test cases for the changes.
  • Passed the CI test.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.004%) to 97.135% when pulling 2e8ec74 on dailyrandomphoto:fix-permalinkDefaults into c19096b on hexojs:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage increased (+0.004%) to 97.135% when pulling 2e8ec74 on dailyrandomphoto:fix-permalinkDefaults into c19096b on hexojs:master.

@curbengh curbengh added this to the 4.1.1 milestone Dec 11, 2019
@@ -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.

👍

Copy link
Contributor

@curbengh curbengh left a comment

Choose a reason for hiding this comment

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

Verified on a test blog.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants