class JekyllRelativeLinks::Generator

Constants

COLLECTIONS_KEY
CONFIG_KEY
CONVERTER_CLASS
ENABLED_KEY
FRAGMENT_REGEX
LOG_KEY

Attributes

config[RW]
site[RW]

Public Class Methods

new(config) click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 24
def initialize(config)
  @config = config
end

Public Instance Methods

generate(site) click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 28
def generate(site)
  return if disabled?

  @site    = site
  @context = context

  documents = site.pages
  documents = site.pages + site.docs_to_write if collections?

  documents.each do |document|
    next unless markdown_extension?(document.extname)
    next if document.is_a?(Jekyll::StaticFile)
    next if excluded?(document)

    replace_relative_links!(document)
  end
end

Private Instance Methods

absolute_url?(string) click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 111
def absolute_url?(string)
  return unless string

  Addressable::URI.parse(string).absolute?
rescue Addressable::URI::InvalidURIError
  nil
end
collections?() click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 135
def collections?
  option(COLLECTIONS_KEY) == true
end
context() click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 74
def context
  @context ||= JekyllRelativeLinks::Context.new(site)
end
disabled?() click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 131
def disabled?
  option(ENABLED_KEY) == false
end
excluded?(document) click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 139
def excluded?(document)
  return false unless option("exclude")

  entry_filter = if document.respond_to?(:collection)
                   document.collection.entry_filter
                 else
                   global_entry_filter
                 end

  entry_filter.glob_include?(option("exclude"), document.relative_path).tap do |excluded|
    Jekyll.logger.debug(LOG_KEY, "excluded #{document.relative_path}") if excluded
  end
end
fragment?(string) click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 119
def fragment?(string)
  string&.start_with?("#")
end
global_entry_filter() click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 153
def global_entry_filter
  @global_entry_filter ||= Jekyll::EntryFilter.new(site)
end
markdown_converter() click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 82
def markdown_converter
  @markdown_converter ||= site.find_converter_instance(CONVERTER_CLASS)
end
markdown_extension?(extension) click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 78
def markdown_extension?(extension)
  markdown_converter.matches(extension)
end
option(key) click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 127
def option(key)
  config[CONFIG_KEY] && config[CONFIG_KEY][key]
end
path_from_root(relative_path, url_base) click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 95
def path_from_root(relative_path, url_base)
  relative_path.sub!(%r!\A/!, "")
  absolute_path = File.expand_path(relative_path, url_base)
  absolute_path.sub(%r!\A#{Regexp.escape(Dir.pwd)}/!, "")
end
potential_targets() click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 91
def potential_targets
  @potential_targets ||= site.pages + site.static_files + site.docs_to_write
end
replacement_text(type, text, url, fragment = nil) click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 101
def replacement_text(type, text, url, fragment = nil)
  url << fragment if fragment

  if type == :inline
    "[#{text}](#{url})"
  else
    "\n[#{text}]: #{url}"
  end
end
url_for_path(path) click to toggle source
# File lib/jekyll-relative-links/generator.rb, line 86
def url_for_path(path)
  target = potential_targets.find { |p| p.relative_path.sub(%r!\A/!, "") == path }
  relative_url(target.url) if target&.url
end