# encoding: utf-8

require 'nokogiri'

class NormalizeLinks < ::Nanoc::Filter
  identifier :normalize_links

  DOMAIN = 'https://prometheus.io'

  def run(content, config = {})
    doc = Nokogiri::HTML(content)

    doc.xpath('//a').each do |link|
      link['href'] =
        case
        when link['href'].start_with?(DOMAIN)
          link['href'][DOMAIN.size..-1]
        when link['href'].start_with?('/')
          # TODO(ts): It's not guaranteed that a repository is hosted on Github.
          github_link_to(link['href'], config)
        when link['href'].include?('.md')
          relative_link_to(link['href'])
        else
          link['href']
        end
    end

    doc.xpath('//img').each do |img|
      next if img['src'].start_with?('/') || img['src'].start_with?('http')
      img['src'] = File.join("../", img['src'])
    end

    doc.to_s
  end

  def github_link_to(file, config)
    base = config[:repository]
    if base.end_with?('.git')
      base = base[0..-5]
    end
    File.join(base, 'blob', config[:refspec], file)
  end

  def relative_link_to(link)
    # All nanoc pages end on a trailing slash.
    File.join("../", link.gsub(/\.md($|#)/, '/\\1'))
  end
end

# encoding: utf-8

require 'nokogiri'
require 'uri'

class NormalizeLinks < ::Nanoc::Filter
  identifier :normalize_links

  DOMAIN = 'https://prometheus.io'

  def run(content, config = {})
    doc = Nokogiri::HTML(content)

    doc.xpath('//a').each do |link|
      link['href'] =
        case
        when link['href'].start_with?(DOMAIN)
          link['href'][DOMAIN.size..-1]
        when link['href'].start_with?('/')
          # TODO(ts): It's not guaranteed that a repository is hosted on Github.
          github_link_to(link['href'], config)
        when link['href'].include?('.md') && !URI.parse(link['href']).absolute?
          relative_link_to(link['href'])
        else
          link['href']
        end
    end

    doc.xpath('//img').each do |img|
      next if img['src'].start_with?('/') || img['src'].start_with?('http')
      img['src'] = File.join("../", img['src'])
    end

    doc.to_s
  end

  def github_link_to(file, config)
    base = config[:repository]
    if base.end_with?('.git')
      base = base[0..-5]
    end
    File.join(base, 'blob', config[:refspec], file)
  end

  def relative_link_to(link)
    # All nanoc pages end on a trailing slash.
    File.join("../", link.gsub(/\.md($|#)/, '/\\1'))
  end
end

Fix absolute links to markdown pages in repo docs
Fix absolute links to markdown pages in repo docs

Absolute links to markdown pages should not be rendered relative.

Fixes prometheus/prometheus#4627

Signed-off-by: Tobias Schmidt <25d72cd05b750b8e431a98389fb858a4199d229b@gmail.com>
