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

The front-matter's permalink can not override config.permalink #3464

Closed
yoshinorin opened this issue Feb 23, 2019 · 8 comments · Fixed by #4359
Closed

The front-matter's permalink can not override config.permalink #3464

yoshinorin opened this issue Feb 23, 2019 · 8 comments · Fixed by #4359
Labels
bug Something isn't working

Comments

@yoshinorin
Copy link
Member

from #3442

Environment Info

Your site _config.yml (Optional):

new_post_name: :year-:month-:day-:title.md

and frontmatter is

---
title: "This is a test"
date: 2019-01-31 20:44:31
permalink: first-blog
---

Expected behavior

URL is http://localhost:3000/first-blog

Actual behaviot

URL is http://localhost:3000/2019/01/first-blog

@yoshinorin yoshinorin added the bug Something isn't working label Feb 23, 2019
@gautamz07
Copy link
Contributor

@yoshinorin curious , so if you exclude permalink from the front matter the expected behavior is ::-

http://localhost:3000/2019/01/first-blog ?

@yoshinorin
Copy link
Member Author

Thanks @gautamz07 :)

I think maybe ...

// _config.yml
new_post_name: :year-:month-:day-:title.md

// Front-matter
---
title: "This is a test"
date: 2019-01-31 20:44:31
---

// Result
http://localhost:3000/2019/01/31/This-is-a-test

But it is just my personal opinion...

@gautamz07
Copy link
Contributor

Oh got it :) i ran into this same issue a while back .. i did't want that YY/MM/DD

@stevenjoezhang
Copy link
Member

stevenjoezhang commented Mar 16, 2020

Relevant issues:
#1099
#1158
#1813

@SukkaW
Copy link
Member

SukkaW commented Jun 14, 2020

It seems that only post is affected. The dedicated pages won't be affected.

@SukkaW
Copy link
Member

SukkaW commented Jun 15, 2020

Since only the post is affected, I wonder if the issue is about new_post_path filter or post_permalink filter.

@SukkaW
Copy link
Member

SukkaW commented Jun 15, 2020

@yoshinorin @stevenjoezhang

Let me make the issue a little more clear:

When config.permalink is set and no permalink in the front-matter

# _config.yml
new_post_name: :title.md # File name of new posts
permalink: post/:name/
permalink_defaults: :title/

source/_posts/2020/hello-world.md:

---
title: "This is a test"
date: 2021-01-31 20:44:31
---

Then I get: /post/hello-world/. LGTM, it fits config.permalink just as I want it to be.

And if I change it to this:

# _config.yml
new_post_name: :title.md # File name of new posts
permalink: post/:title/ # Use :title instead of :name
permalink_defaults: :title/
---
title: "This is a test"
date: 2021-01-31 20:44:31
---

I will get http://localhost:5000/post/2020/hello-world/. Still, it fits config.permalink.

Try to use front-matter to override config.permalink

Here you go!

# _config.yml
new_post_name: :title.md # File name of new posts
permalink: post/:title/ # Use :title instead of :name
permalink_defaults: :title/
---
title: "This is a test"
date: 2021-01-31 20:44:31
permalink: hexo-hexo-hexo/i-love-hexo
---

Guess what! I get /post/hexo-hexo-hexo/i-love-hexo/ instead of /hexo-hexo-hexo/i-love-hexo/!

So I changed my config again, this time I will use :name instead of :title

# _config.yml
new_post_name: :title.md # File name of new posts
permalink: post/:title/ # Use :title instead of :name
permalink_defaults: :title/
---
title: "This is a test"
date: 2021-01-31 20:44:31
permalink: hexo-hexo-hexo/i-love-hexo
---

Then what? I get /post/i-love-hexo/! Even the hexo-hexo-hexo is now gone!


So it appears that not only page.permalink can't override anything, it is even being affected by config.permalink.

@SukkaW
Copy link
Member

SukkaW commented Jun 15, 2020

So here is what I found.

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

As you can see, permalink in the front-matter will become data.slug (permalink overriding slug), and data.permalink will be deleted.

But post.slug doesn't matter much. The real thing that matters most is post.path. Then guess what? post.path is a [getter]:

image

So what [getter]? Take a look at the model of the post:

hexo/lib/models/post.js

Lines 48 to 51 in edef5c2

Post.virtual('path').get(function() {
const path = ctx.execFilterSync('post_permalink', this, {context: ctx});
return typeof path === 'string' ? path : '';
});

Ha! The post.path is actually post_permalink filter.

Yes, post_permalink is what we should looking at.

Even the post.permalink we used in out template is not the permalink in the front-matter. It is also a [getter], which is full_url_for(this.path). That means post.permalink is still affected by post_permalink filter.

hexo/lib/models/post.js

Lines 53 to 55 in edef5c2

Post.virtual('permalink').get(function() {
return full_url_for.call(ctx, this.path);
});

@SukkaW SukkaW closed this as completed Jun 15, 2020
@SukkaW SukkaW reopened this Jun 15, 2020
@SukkaW SukkaW changed the title If default permalink is not :title/ the Front-matter's permalink seems not override correctly The front-matter's permalink can not override config.permalink Jun 16, 2020
SukkaW added a commit to SukkaW/hexo that referenced this issue Jun 16, 2020
SukkaW added a commit to SukkaW/hexo that referenced this issue Jun 16, 2020
SukkaW added a commit to SukkaW/hexo that referenced this issue Jun 16, 2020
SukkaW added a commit to SukkaW/hexo that referenced this issue Jun 16, 2020
SukkaW added a commit to SukkaW/hexo that referenced this issue Jun 16, 2020
@SukkaW SukkaW mentioned this issue Jul 25, 2020
22 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants