From 30e7d5ea532b301b3ca55b3f69c193d6a4214a12 Mon Sep 17 00:00:00 2001
From: Ashwin Maroli
Date: Mon, 13 Apr 2020 13:21:33 +0530
Subject: [PATCH] Add slugified_categories URL placeholder (#8094)
Merge pull request 8094
---
docs/_docs/permalinks.md | 20 +++++++++++++++++
features/post_data.feature | 42 ++++++++++++++++++++++++++++++++++++
lib/jekyll/drops/url_drop.rb | 8 +++++++
3 files changed, 70 insertions(+)
diff --git a/docs/_docs/permalinks.md b/docs/_docs/permalinks.md
index 6ab7994b..99a90cea 100644
--- a/docs/_docs/permalinks.md
+++ b/docs/_docs/permalinks.md
@@ -258,6 +258,26 @@ Here's the full list of placeholders available:
+
+
+ slugified_categories
+ {% include docs_version_badge.html version="4.1" %}
+ |
+
+
+ The specified categories for this post but slugified. If a category is a
+ composite of multiple words, Jekyll will downcase all alphabets and replace any
+ non-alphanumeric character with a hyphen. (e.g. "Work 2 Progress"
+ will be converted into "work-2-progress" )
+
+
+ If a post has multiple categories, Jekyll will create a hierarchy
+ (e.g. /work-2-progress/category2 ).
+ Also Jekyll automatically parses out double slashes in the URLs,
+ so if no categories are present, it will ignore this.
+
+ |
+
diff --git a/features/post_data.feature b/features/post_data.feature
index c288115d..aee7f3fd 100644
--- a/features/post_data.feature
+++ b/features/post_data.feature
@@ -129,6 +129,48 @@ Feature: Post data
And the _site directory should exist
And I should see "Post category: movies" in "_site/movies/2009/03/27/star-wars.html"
+ Scenario: Use post.categories when category is a composite of multiple words
+ Given I have a Sci-Fi Movi3s directory
+ And I have a Sci-Fi Movi3s/_posts directory
+ And I have a _layouts directory
+ And I have the following post in "Sci-Fi Movi3s":
+ | title | date | layout | category | content |
+ | Star Wars | 2020-04-03 | simple | vintage | Luke, I am your father. |
+ And I have a "_layouts/simple.html" file with content:
+ """
+ Post categories: {{ page.categories | join: ', ' }}
+ Post URL: {{ page.url }}
+ """
+ When I run jekyll build
+ Then I should get a zero exit status
+ And the _site directory should exist
+ And I should see "Post categories: Sci-Fi Movi3s, vintage" in "_site/sci-fi movi3s/vintage/2020/04/03/star-wars.html"
+ And I should see "Post URL: /sci-fi%20movi3s/vintage/2020/04/03/star-wars.html" in "_site/sci-fi movi3s/vintage/2020/04/03/star-wars.html"
+
+ Scenario: Use post.slugified_categories to generate URL when category is a composite of multiple words
+ Given I have a Sci-Fi Movi3s directory
+ And I have a Sci-Fi Movi3s/_posts directory
+ And I have a _layouts directory
+ And I have the following post in "Sci-Fi Movi3s":
+ | title | date | layout | category | content |
+ | Star Wars | 2020-04-03 | simple | vintage | Luke, I am your father. |
+ And I have a "_layouts/simple.html" file with content:
+ """
+ Post categories: {{ page.categories | join: ', ' }}
+ Post URL: {{ page.url }}
+ """
+ And I have a "_config.yml" file with content:
+ """
+ collections:
+ posts:
+ permalink: "/:slugified_categories/:year/:month/:day/:title:output_ext"
+ """
+ When I run jekyll build
+ Then I should get a zero exit status
+ And the _site directory should exist
+ And I should see "Post categories: Sci-Fi Movi3s, vintage" in "_site/sci-fi-movi3s/vintage/2020/04/03/star-wars.html"
+ And I should see "Post URL: /sci-fi-movi3s/vintage/2020/04/03/star-wars.html" in "_site/sci-fi-movi3s/vintage/2020/04/03/star-wars.html"
+
Scenario: Use post.tags variable
Given I have a _posts directory
And I have a _layouts directory
diff --git a/lib/jekyll/drops/url_drop.rb b/lib/jekyll/drops/url_drop.rb
index 98a03f4a..a710368f 100644
--- a/lib/jekyll/drops/url_drop.rb
+++ b/lib/jekyll/drops/url_drop.rb
@@ -35,6 +35,14 @@ module Jekyll
category_set.to_a.join("/")
end
+ # Similar to output from #categories, but each category will be downcased and
+ # all non-alphanumeric characters of the category replaced with a hyphen.
+ def slugified_categories
+ Array(@obj.data["categories"]).each_with_object(Set.new) do |category, set|
+ set << Utils.slugify(category.to_s)
+ end.to_a.join("/")
+ end
+
# CCYY
def year
@obj.date.strftime("%Y")