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(#3464): override permalink use the front-matter #4359

Merged
merged 1 commit into from Jun 20, 2020

Conversation

SukkaW
Copy link
Member

@SukkaW SukkaW commented Jun 16, 2020

What does it do?

The PR closes #1158, closes #1813, closes #3464

#3464 (comment)

if (data.permalink) {
data.slug = data.permalink;
delete data.permalink;
}

Originally, during the processing of the post, the permalink defined in the front-matter which is data.permalink, will override data.slug and data.permalink will be deleted.

And, in post_permalink filter, data.slug will be used to get name and title property:

title: slug,
name: typeof slug === 'string' ? basename(slug) : '',

So if I got a post: source/_posts/i-love-hexo/hello.md, I will get:

  • title: i-love-hexo/hello
  • name: hello

If I then defines permalink as permalink-test/hi.md, I will get:

  • title: permalink-test/hi
  • name: hi.

I don't know why it is designed like this. Maybe using the permalink to override the relative path under source/_posts. But it doesn't make any sense - why I have to override the relative path? I could just create required directories under source/_posts and it will be fine.

Also, the behavior of Post is different from Page, which permalink in the front-matter is truly able to override the hexo.config.permalink. And that behavior is what user really want.

So in this PR, I made a few changes toward post processor and post_permalink filter.

As the behavior of the permalink in the front-matter has changed, thus the PR is still considered as a Breaking Change

How to test

git clone -b fix-issue-3464 https://github.com/sukkaw/hexo.git
cd hexo
npm install
npm test

Screenshots

Pull request tasks

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

@@ -121,7 +121,7 @@ module.exports = ctx => {
}

if (data.permalink) {
data.slug = data.permalink;
data.__permalink = data.permalink;
Copy link
Member Author

@SukkaW SukkaW Jun 16, 2020

Choose a reason for hiding this comment

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

BREAKING CHANGES.

data.slug won't be override by data.permalink. That means data.slug will still come from the original path relative to source/_posts.

Also, a new __permalink is created to avoid overriding Post.virtual('permalink').get.

Comment on lines +11 to +14
if (__permalink) {
if (!__permalink.startsWith('/')) return `/${__permalink}`;
return __permalink;
}
Copy link
Member Author

@SukkaW SukkaW Jun 16, 2020

Choose a reason for hiding this comment

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

When a __permalink is detected (which is added by post processor from permalink inside the front-matter), it will be return directly.

BREAKING CHANGES:

permalink inside the front-matter is no longer controlled by hexo.config.permalink.

@coveralls
Copy link

coveralls commented Jun 16, 2020

Coverage Status

Coverage remained the same at 97.793% when pulling 2de3e25 on SukkaW:fix-issue-3464 into edef5c2 on hexojs:master.

@SukkaW SukkaW added this to the 5.0.0 milestone Jun 16, 2020
@SukkaW SukkaW added this to In progress in 5.0.0 via automation Jun 20, 2020
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.

WFM. One thing to note that is that hexo-server currently needs a trailing slash in permalink: (regardless of _config.yml or front-matter), otherwise it responds with invalid mime type; this limitation could be resolved in hexojs/hexo-server#111.

5.0.0 automation moved this from In progress to Reviewer approved Jun 20, 2020
@SukkaW SukkaW merged commit 29e28e2 into hexojs:master Jun 20, 2020
5.0.0 automation moved this from Reviewer approved to Done Jun 20, 2020
@SukkaW SukkaW mentioned this pull request Jul 25, 2020
22 tasks
@stevenjoezhang stevenjoezhang mentioned this pull request Nov 25, 2022
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
5.0.0
  
Done
4 participants