Skip to content

Commit

Permalink
fix slugs
Browse files Browse the repository at this point in the history
  • Loading branch information
halkeye committed Feb 25, 2024
1 parent a37ae16 commit 7c5c359
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
1 change: 0 additions & 1 deletion .nvmrc

This file was deleted.

24 changes: 24 additions & 0 deletions content/posts/posts.11tydata.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,28 @@ module.exports = {
"posts"
],
"layout": "layouts/post.njk",
eleventyComputed: {
// data here is the post's data
permalink: function (data) {
if (data.permalink) {
// First, if I have already specified a permalink, just use it
return data.permalink;
}

function urlDatePrefix(postDate) {
const date = new Date(postDate);
return `${[date.getFullYear(), date.getMonth() + 1, date.getDate()].map(v => String(v).padStart(2, '0')).join('/')}`;
}
// If there is no permalink, look for a slug in the data.
// If there is no slug, just use the slugify filter on the title
const slug = data.slug ?? this.slugify(data.title);

if (slug.includes('/')) {
return `${slug}/index.html`;
}

// Combine it all for your new, consistent permalink
return `/${urlDatePrefix(data.date)}/${slug}/index.html`;
}
}
};

0 comments on commit 7c5c359

Please sign in to comment.