Skip to content

Commit

Permalink
Moved urls redirects (#4342)
Browse files Browse the repository at this point in the history
Generate redirect for /latest/ on moved URLs
  • Loading branch information
mheap authored Aug 29, 2022
1 parent 91c2239 commit f461803
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app/_plugins/generators/alias_generator.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'yaml'
# Alias Generator for Posts using Netlify _redirects
#
# Generates redirect pages for posts with aliases set in the YAML Front Matter.
Expand Down Expand Up @@ -33,10 +34,15 @@ class AliasGenerator < Generator
def generate(site) # rubocop:disable Metrics/AbcSize
@redirects = []

# Generate redirect_to from frontmatter redirects
site.pages.each do |page|
generate_aliases(page.destination('').gsub(/index\.(html|htm)$/, ''), page)
end

# Generate redirects from moved_urls
moved_pages = YAML.load_file("#{__dir__}/../../moved_urls.yml")
generate_moved_aliases(moved_pages)

# Read existing _redirects file
existing = "#{File.read("#{__dir__}/../../_redirects")}\n"

Expand All @@ -47,6 +53,14 @@ def generate(site) # rubocop:disable Metrics/AbcSize
site.pages << page
end

def generate_moved_aliases(moved_pages)
moved_pages.each do |k,v|
k = k.sub("/VERSION/", "/latest/")
v = v.sub("/VERSION/", "/latest/")
@redirects.push("#{k}\t#{v}")
end
end

def generate_aliases(_destination_path, page) # rubocop:disable Metrics/MethodLength
aliases = page.data['alias']
alias_paths ||= []
Expand Down

0 comments on commit f461803

Please sign in to comment.