Hi.
I am trying to connect some stuff up in a template, hoping to match on a post’s slug. Tried so many things to no avail until I finally decided to dump the post.data and to my dismay…
"title": "A Second Note",
"permalink": "/notes/:year/:month/:day/:slug",
"slug": "A Second Note",
The permalink and all links all work and are slugified properly… but for some reason in the data it’s untouched. I was of course expecting:
"slug": "a-second-note",
Kinda scratching my head here… why would this be?
And more importantly, how to fix it?
Thanks!
The permalink
and slug
are combined to form the url
property. So page.url
should contain something like: /notes/2020/7/24/a-second-note
Thanks @chuckhoupt
Except, the .slug
is not slugified, and the .url
isn’t something I can match my string against.
I suppose I can write Liquid code to parse the .url
, but .slug
should be, you know, the slug. No? 
Are you explicitly setting the slug in the post’s front-matter? When slug is explicitly set in the front-matter, Jekyll won’t overwrite it.
Normally, .slug
is generated by feeding the title thru the sluggify
filter [correction: it’s base on file name], but if a slug is found in the front-matter, then it will be used instead of the title (but still fed thru the sluggify
filter, just in case it isn’t properly formatted).
Ideally, an explicitly set slug should be a correctly formatted URL component (i.e. “a-second-note”). However, to auto-correct errors, Jekyll applies the sluggify
filter to ensure consistent URLs. So a slug like “A Second Note” will appear in the URL as “a-second-note”.
You might be able to use the sluggify
filter to help with matching…
Thanks again @chuckhoupt
I am not setting the slug in the front matter.
I was totally expecting the Jekyll-generated slug in the data struct to be slugified but it is not. Perhaps a config issue?
(I am running latest Jekyll)
Odd, when I eval the slug in Liquid, I always get the generated slug. For example: {{ site.posts[0].slug }}
or {{ page.slug }}
will eval to “welcome-to-jekyll” in a freshly created Jekyll site (4.1.1).
You mention Post.data['slug']
, but I don’t recognize that as Liquid. Maybe you’re writing a Ruby plugin? That might explain the confusion.
Aha ok mystery.
Definitely in a template.
Right after <body>
I insert
Page Slug: {{ page.slug }}
and I get:
Page Slug: A First Note
( at localhost/2020/07/26/a-first-note )
Ok so I installed a clean test site and here’s what I found.
page.slug
is not the title slugified. It is a straight copy of the source filename. (In the case of posts, it strips the date, which is ignored anyways if the front matter has a date… weird behavior as well.)
So, a source filename like
_posts/2020-07-20-A First Post.md
will give a slug of
A First Post
regardless of what is or isn’t in the front matter.
Only a source filename like
_posts/2020-07-20-a-first-post.md
will give a slug of
a-first-post
Very surprising and disappointing. Users don’t write slugs, they write titles. But the system can use and rely on “automatically slugified” titles, and this behavior breaks that potential.
I wonder if this is a bug?
Guess I’ll be parsing page.url in Liquid:
{%- assign realslug = page.url | split:'/' | last -%}
<pre>Post URL : {{ page.url }}</pre>
<pre>Real Slug: {{ realslug }}</pre>
Edit: So this doesn’t solve my particular problem as I am querying the post
data which needs a real slug to be present there to work. Maybe I need to write a plugin to push that in.
Anyways, thank you @chuckhoupt for your help! 