Hexo: The front-matter's permalink can not override config.permalink

Created on 23 Feb 2019  ·  8Comments  ·  Source: hexojs/hexo

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

bug

All 8 comments

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

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

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...

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

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

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

@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.

So here is what I found.

https://github.com/hexojs/hexo/blob/edef5c2a9c1217aaab02f64397e7e94d2f98e4fa/lib/plugins/processor/post.js#L123-L126

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:

https://github.com/hexojs/hexo/blob/edef5c2a9c1217aaab02f64397e7e94d2f98e4fa/lib/models/post.js#L48-L51

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.

https://github.com/hexojs/hexo/blob/edef5c2a9c1217aaab02f64397e7e94d2f98e4fa/lib/models/post.js#L53-L55

Was this page helpful?
0 / 5 - 0 ratings