Class: Jekyll::AgentAudit::UrlResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/agent_audit/url_resolver.rb

Instance Method Summary collapse

Constructor Details

#initialize(entries, site_config, configuration) ⇒ UrlResolver

Returns a new instance of UrlResolver.



8
9
10
11
12
# File 'lib/jekyll/agent_audit/url_resolver.rb', line 8

def initialize(entries, site_config, configuration)
  @entries, @site_config, @configuration = Array(entries), site_config || {}, configuration
  @by_path = {}
  @entries.each { |entry| index(entry) }
end

Instance Method Details

#resolve(href, document, base: true) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/jekyll/agent_audit/url_resolver.rb', line 14

def resolve(href, document, base: true)
  raw = href.to_s.strip
  if raw.empty?
    target = @by_path[canonical_path(document[:route] || document[:url])]
    return result(target ? :local : :missing, url: document[:url], path: document[:route], target: target, reason: target ? nil : :target_missing)
  end
  return result(:unsupported, reason: raw.split(":", 2).first) if raw.match?(/\A(?:mailto|tel|javascript|data):/i)
  URI.parse(raw)
  base_uri = URI.parse(document[:url].to_s)
  if base && document[:base_href].to_s != ""
    base_uri = URI.join(base_uri.to_s, document[:base_href].to_s)
  end
  resolved = URI.join(base_uri.to_s, raw)
  configured = URI.parse(@site_config.fetch("url", @site_config[:url]).to_s) rescue nil
  if resolved.host && configured&.host && !same_origin?(resolved, configured)
    return result(:external, url: resolved.to_s, path: resolved.path, fragment: fragment(resolved.fragment))
  end
  baseurl = @site_config.fetch("baseurl", @site_config[:baseurl]).to_s.sub(%r{/\z}, "")
  if baseurl != "" && raw.start_with?("/") && resolved.path != baseurl && !resolved.path.start_with?("#{baseurl}/")
    return result(:unsupported, url: resolved.to_s, path: resolved.path, fragment: fragment(resolved.fragment), reason: :outside_baseurl)
  end
  baseurl = @site_config.fetch("baseurl", @site_config[:baseurl]).to_s.sub(%r{/\z}, "")
  if !baseurl.empty? && resolved.path != baseurl && !resolved.path.start_with?("#{baseurl}/")
    return result(:unsupported, url: resolved.to_s, path: resolved.path, fragment: fragment(resolved.fragment), reason: :outside_baseurl)
  end
  path = public_path(resolved)
  target = @by_path[canonical_path(path)]
  alias_path = alias_for(path)
  target ||= @by_path[canonical_path(alias_path)] if alias_path
  result(target ? :local : :missing, url: resolved.to_s, path: path, fragment: fragment(resolved.fragment), target: target, reason: target ? nil : :target_missing)
rescue URI::InvalidURIError, ArgumentError => e
  result(:invalid, reason: e.message)
end