Created
July 9, 2018 19:37
-
-
Save ind1go/3e1e523602987893f86059784df26521 to your computer and use it in GitHub Desktop.
html-pipeline plugin and ERB tags to take absolute URLs to relative URLs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="description" content="<%= name %> GraphQL documentation"> | |
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> | |
<title><%= title || name %></title> | |
<% | |
def relative_filename_path(opts) | |
filename_path = Pathname.new(opts[:filename]) | |
output_dir_path = Pathname.new(opts[:output_dir]) | |
filename_path.relative_path_from(output_dir_path) | |
end | |
def prepend_parent(relative_href) | |
Pathname.new('..') + relative_href | |
end | |
def depth(opts) | |
relative_filename_path(opts).each_filename.count - 1 | |
end | |
def relative_href(opts, href) | |
relative_href = Pathname.new(href[base_url.length + 1..-1]) | |
depth(opts).times do | |
relative_href = prepend_parent(relative_href) | |
end | |
relative_href.to_s | |
end | |
%> | |
<link rel="stylesheet" href="<%= relative_href(opts, "#{base_url}/assets/style.css") %>"> | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require 'uri' | |
require 'pathname' | |
# Make internal absolute HREFs relative | |
class ToRelativeFilter < HTML::Pipeline::Filter | |
def call | |
doc.search('a').each do |link| | |
href = link['href'] | |
next if href.nil? | |
href = href.strip | |
next unless href.start_with?(base_url) | |
modify_href(link, href) | |
end | |
doc | |
end | |
def validate | |
needs :filename, :output_dir, :base_url | |
end | |
private | |
def base_url | |
context[:base_url] | |
end | |
def relative_filename_path | |
filename_path = Pathname.new(context[:filename]) | |
output_dir_path = Pathname.new(context[:output_dir]) | |
filename_path.relative_path_from(output_dir_path) | |
end | |
def prepend_parent(relative_href) | |
Pathname.new('..') + relative_href | |
end | |
def depth | |
relative_filename_path.each_filename.count - 1 | |
end | |
def relative_href(href) | |
relative_href = Pathname.new(href[base_url.length + 1..-1]) | |
depth.times do | |
relative_href = prepend_parent(relative_href) | |
end | |
relative_href.to_s | |
end | |
def modify_href(link, href) | |
link['href'] = relative_href(href) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was prompted by conversation at brettchalupa/graphql-docs#51.