Last active
October 19, 2023 12:14
-
-
Save peey/897e8ed33e412fdfe0fcacf002acc150 to your computer and use it in GitHub Desktop.
Jekyll plugin for parsing of custom variables in permalinks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# will substitute :author with the variable author in your file (also works with defaults). | |
permalink: /:author/:slug/ | |
# You need to have this extra permalink_custom_vars array to tell the plugin which substitutions to make | |
permalink_custom_vars: ['author'] | |
# Note that you don't have to include the supported variables in this list as jekyll takes care of that | |
# For a complete list of variables jekyll supports, see: https://jekyllrb.com/docs/permalinks/#template-variables |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Based on http://stackoverflow.com/a/17206081/1412255 | |
module Jekyll | |
class PermalinkRewriter < Generator | |
safe true | |
priority :low | |
def generate(site) | |
if site.config['permalink'].include? ":" # ignore presets | |
site.posts.docs.each do |item| | |
item.data['permalink'] = site.config['permalink'].dup | |
site.config['permalink_custom_vars'].each do |var| | |
# if data isn't available, replace with empty | |
substitution = item.data[var].to_s or '' | |
item.data['permalink'].gsub! ":" + var, substitution | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @peey
Does this work with the modern version of Jekyll? I'm trying to make it something for the pages, where if they contains a :slug, this is going to be forming the pemalink later without success.
In fact, even a simple, hardcoded replacement is ignored.
Do you have any idea?
Thanks