6
votes

I have a markdown file as follows:

---
title: My Page
categories:
- first
- second
---

In my _config.yml file, I set the permalink to /:categories/:title.html

So when I generate the site, the permalink ends up being /first/second/title.html, whereas I was hoping Jekyll would create /first/title.html and /second/title.html

Is there a way to do this without custom plugins?

Cheers

2
To clarify, you want one document to be duplicated in several categories? - Max
yes. It is odd to me that Jekyll assumes that 'first' would 'subsume' second in some way. What seems more intuitive is that the page belongs to the categories of both 'first' and 'second', and those categories are not related in some transitive fashion. - user3653270

2 Answers

4
votes

The easiest and to me best way is to define the permalink via frontmatter. This is also great for search engine optimization. First you tell Jekyll via _config.yml how Jekyll should build the links if you forget to set it via frontmatter:

_config.yml

# Build settings
permalink: /:categories/:title/

Define a permalink...

2014-10-17_my_post.md

---
layout: post
title: 'Post with permalink'
permalink: /this-is-the-unique-permalink/
---

My Post
2
votes

According to these docs, it looks like each Jekyll page can only have one category. categories is kind of a misnomer, because you're really defining a "category hierarchy" - like a file path - so the post really resides in a single (sub)category. In this limited sense, you can't do what you want with vanilla Jekyll.

However, Jekyll will process files just sitting around in any directory that doesn't start with an underscore and it follows symlinks. So, for example, if you make directories for each category and place your page in one of them, you can create symlinks to any number of other "categories".

mkdir first second
touch first/page.md
ln -s ../first/page.md second/