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 |
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.
if p.data['slug'].include?("/test/")
puts("rewriting the permalink")
permalink = p.data['permalink'] = "/newurl"
end
Do you have any idea?
Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use, please put the
.rb
file in a directory named_plugins
. See the article for more details.Disclaimer: I only know as much ruby as I've learnt in the past 20 minutes. Safety not guaranteed.