module Jekyll::UniqueURL::URLSetter

Modifies the url method to check if the URL is a duplicate

Public Class Methods

included(base) click to toggle source
   # File lib/jekyll/unique_urls/url_setter.rb
 7 def self.included(base)
 8   base.class_eval do
 9     alias original_url url
10 
11     # Only do this once.  The first time we check if the URL
12     # already exists and add a unique string to it.
13     def url
14       return @url if @url
15 
16       original_url
17 
18       if @site.urls.include? @url
19         @url = unique_url
20 
21         Jekyll.logger.warn "URL for #{relative_path} is duplicate, changing to #{@url}"
22       end
23 
24       @site.urls << @url
25 
26       @url
27     end
28 
29     # Make the URL unique by appending a unique string to it.  If
30     # it's a directory, add it to the directory; if it's a file,
31     # add it to the file name.
32     def unique_url
33       if @url.end_with? '/'
34         @url.sub(%r{/\z}, '-' + digest + '/')
35       elsif respond_to? :output_ext
36         @url.sub(%r{#{output_ext}\z}, '-' + digest + output_ext)
37       else
38         @url.sub(%r{#{extname}\z}, digest + extname)
39       end
40     end
41 
42     # Paths are unique for every resource type so we just digest
43     # those and take the first 8 chars.
44     #
45     # XXX: What's the chance of collisions?
46     def digest
47       @digest ||= Digest::SHA256.hexdigest(relative_path)[0..7]
48     end
49   end
50 end

Public Instance Methods

digest() click to toggle source

Paths are unique for every resource type so we just digest those and take the first 8 chars.

XXX: What's the chance of collisions?

   # File lib/jekyll/unique_urls/url_setter.rb
46 def digest
47   @digest ||= Digest::SHA256.hexdigest(relative_path)[0..7]
48 end
unique_url() click to toggle source

Make the URL unique by appending a unique string to it. If it's a directory, add it to the directory; if it's a file, add it to the file name.

   # File lib/jekyll/unique_urls/url_setter.rb
32 def unique_url
33   if @url.end_with? '/'
34     @url.sub(%r{/\z}, '-' + digest + '/')
35   elsif respond_to? :output_ext
36     @url.sub(%r{#{output_ext}\z}, '-' + digest + output_ext)
37   else
38     @url.sub(%r{#{extname}\z}, digest + extname)
39   end
40 end
url() click to toggle source

Only do this once. The first time we check if the URL already exists and add a unique string to it.

   # File lib/jekyll/unique_urls/url_setter.rb
13 def url
14   return @url if @url
15 
16   original_url
17 
18   if @site.urls.include? @url
19     @url = unique_url
20 
21     Jekyll.logger.warn "URL for #{relative_path} is duplicate, changing to #{@url}"
22   end
23 
24   @site.urls << @url
25 
26   @url
27 end