Module: JekyllRelativeLinks::Filter

Defined in:
lib/jekyll-relative-links/filter.rb

Instance Method Summary collapse

Instance Method Details



24
25
26
27
28
29
30
31
32
33
# File 'lib/jekyll-relative-links/filter.rb', line 24

def process_link(match, regex_match, url_base, site)
  relative_path = regex_match[1]
  fragment = regex_match[3] ? "##{regex_match[3]}" : ""

  return match if absolute_url?(relative_path) || !relative_path.end_with?(".md")

  path = path_from_root(relative_path, url_base)
  url = url_for_path(path, site)
  url ? "<a href=\"#{url}#{fragment}\"" : match
end


15
16
17
18
19
20
21
22
# File 'lib/jekyll-relative-links/filter.rb', line 15

def process_links(html, site)
  page = @context.registers[:page]
  url_base = page ? File.dirname(page["path"].to_s) : ""

  html.gsub(%r!<a href="([^"]+\.md)(#([^"]+))?"!) do |match|
    process_link(match, Regexp.last_match, url_base, site)
  end
end

This filter processes HTML content that's already been converted by the markdownify filter and updates any relative links to markdown files to point to their HTML equivalents. Usage: content | markdownify | rellinks }



8
9
10
11
12
13
# File 'lib/jekyll-relative-links/filter.rb', line 8

def rellinks(html)
  return html if html.nil? || html.empty?
  return html if @context.registers[:site].nil?

  process_links(html, @context.registers[:site])
end