Skip to content

Commit

Permalink
Fixes #5615
Browse files Browse the repository at this point in the history
  • Loading branch information
hexylena committed Dec 12, 2024
1 parent 412c259 commit e41a8af
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
4 changes: 4 additions & 0 deletions _plugins/gtn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,10 @@ def find_learningpaths_including_topic(site, topic_id)
end
end

Jekyll::Hooks.register :site, :post_write do |site|
Gtn::Shortlinks.fix_missing_redirs(site)
end

if $PROGRAM_NAME == __FILE__
result = Gtn::ModificationTimes.obtain_time(ARGV[0].gsub(%r{^/}, ''))
puts "Modification time of #{ARGV[0].gsub(%r{^/}, '')} is #{result}"
Expand Down
45 changes: 44 additions & 1 deletion _plugins/gtn/shortlinks.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

module Gtn
# This module is responsible for generating shortlinks for tutorials and FAQs
# This module is responsible for generating shortlinks for tutorials and FAQs and any other pages we add.
#
# Every category gets its own prefix letter.
module Shortlinks
CATEGORY_TUTORIAL = 'T'
CATEGORY_SLIDES = 'S'
Expand All @@ -11,10 +13,51 @@ module Shortlinks
CATEGORY_EVENTS = 'E'
CATEGORY_WORKFLOW = 'W'

REDIRECT_TEMPLATE = <<~REDIR
<!DOCTYPE html>
<html lang="en-US">
<meta charset="utf-8">
<title>Redirecting&hellip;</title>
<link rel="canonical" href="REDIRECT_URL">
<script>location="REDIRECT_URL"</script>
<meta http-equiv="refresh" content="0; url=REDIRECT_URL">
<meta name="robots" content="noindex">
<h1>Redirecting&hellip;</h1>
<a href="REDIRECT_URL">Click here if you are not redirected.</a>
</html>
REDIR

def self.mapped?(tutorial, current_mapping)
current_mapping['id'].values.include? tutorial
end

##
# Duplicate of the jekyll-redirect-from plugin template.
# We can't use that for, reasons.
def self.html_redirect(target)
REDIRECT_TEMPLATE.gsub('REDIRECT_URL', target)
end

##
# Fix missing symlinks (usually exist because the target file has been
# renamed and doesn't exist anymore.) However, a redirect *will* be present
# for the original filename so we just fix the missing symlink.
#
# Params:
# +site+:: The Jekyll site object
def self.fix_missing_redirs(site)
missing_redirs = site.data['shortlinks']['id'].select do |id, target|
short_link = "short/#{id}.html"
! File.exist?(site.in_dest_dir(short_link))
end

missing_redirs.each do |id, target|
short_link = "short/#{id}.html"
Jekyll.logger.warn "[GTN/Shortlink]" "Shortlink target #{target} does not exist for shortlink #{short_link}, fixing."
File.write(site.in_dest_dir(short_link), Gtn::Shortlinks.html_redirect(target))
end
end

def self.update(current_mapping)
current_mapping['id'] = {} if !current_mapping.key? 'id'

Expand Down
2 changes: 0 additions & 2 deletions metadata/shortlinks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ name:
data-science/data-manipulation-olympics: "/topics/data-science/tutorials/data-manipulation-olympics/tutorial.html"
data-science/data-manipulation-olympics-sql: "/topics/data-science/tutorials/data-manipulation-olympics-sql/tutorial.html"
data-science/git-cli: "/topics/data-science/tutorials/git-cli/tutorial.html"
data-science/how-to-google: "/topics/data-science/tutorials/how-to-google/tutorial.html"
data-science/python-advanced-np-pd: "/topics/data-science/tutorials/python-advanced-np-pd/tutorial.html"
data-science/python-argparse: "/topics/data-science/tutorials/python-argparse/tutorial.html"
data-science/python-basics: "/topics/data-science/tutorials/python-basics/tutorial.html"
Expand Down Expand Up @@ -687,7 +686,6 @@ id:
T00077: "/topics/data-science/tutorials/data-manipulation-olympics/tutorial.html"
T00078: "/topics/data-science/tutorials/data-manipulation-olympics-sql/tutorial.html"
T00079: "/topics/data-science/tutorials/git-cli/tutorial.html"
T00080: "/topics/data-science/tutorials/how-to-google/tutorial.html"
T00081: "/topics/data-science/tutorials/python-advanced-np-pd/tutorial.html"
T00082: "/topics/data-science/tutorials/python-argparse/tutorial.html"
T00083: "/topics/data-science/tutorials/python-basics/tutorial.html"
Expand Down

0 comments on commit e41a8af

Please sign in to comment.