Skip to content

Commit 325222a

Browse files
authored
Update plugin for SIP redirects (#3243)
* Fix permalink due to SIP changes * Update plugin for SIP redirects This goes hand in hand with scala/improvement-proposals#128
1 parent a51b8ac commit 325222a

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

_plugins/sip_number_permalink.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
# This plugin allows using front matter variables in permalinks.
3+
# This plugin allows using front matter variables in permalinks and redirect_from.
44
# For example: permalink: /sips/:number will use the 'number' front matter field.
55

66
Jekyll::Hooks.register :site, :after_init do
@@ -10,3 +10,18 @@ def number
1010
end
1111
end
1212
end
13+
14+
# Expand :number and :title in redirect_from values
15+
Jekyll::Hooks.register :documents, :pre_render do |doc|
16+
next unless doc.data["redirect_from"]
17+
18+
redirects = doc.data["redirect_from"]
19+
redirects = [redirects] unless redirects.is_a?(Array)
20+
21+
doc.data["redirect_from"] = redirects.map do |redirect|
22+
result = redirect
23+
result = result.gsub(":number", doc.data["number"].to_s) if doc.data["number"]
24+
result = result.gsub(":title", Jekyll::Utils.slugify(doc.data["title"])) if doc.data["title"]
25+
result
26+
end
27+
end

0 commit comments

Comments
 (0)