-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #291 from hmrc/TRG-128-for-GDS-review
Support sites deployed on paths other than "/" (by generating relative links)
- Loading branch information
Showing
13 changed files
with
278 additions
and
44 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module GovukTechDocs | ||
module PathHelpers | ||
def get_path_to_resource(config, resource, current_page) | ||
if config[:relative_links] | ||
resource_path_segments = resource.path.split("/").reject(&:empty?)[0..-2] | ||
resource_file_name = resource.path.split("/")[-1] | ||
|
||
path_to_site_root = path_to_site_root config, current_page.path | ||
resource_path = path_to_site_root + resource_path_segments | ||
.push(resource_file_name) | ||
.join("/") | ||
else | ||
resource_path = resource.url | ||
end | ||
resource_path | ||
end | ||
|
||
def path_to_site_root(config, page_path) | ||
if config[:relative_links] | ||
number_of_ascents_to_site_root = page_path.to_s.split("/").reject(&:empty?)[0..-2].length | ||
ascents = number_of_ascents_to_site_root.zero? ? ["."] : number_of_ascents_to_site_root.times.collect { ".." } | ||
path_to_site_root = ascents.join("/").concat("/") | ||
else | ||
middleman_http_prefix = config[:http_prefix] | ||
path_to_site_root = middleman_http_prefix.end_with?("/") ? middleman_http_prefix : "#{middleman_http_prefix}/" | ||
end | ||
path_to_site_root | ||
end | ||
end | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<%= GovukTechDocs::Pages.new(sitemap, config).to_json %> | ||
<%= GovukTechDocs::Pages.new(sitemap, config, current_page).to_json %> |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,33 @@ | ||
RSpec.describe GovukTechDocs::Pages do | ||
describe "#to_json" do | ||
it "returns the pages as JSON" do | ||
it "returns the pages as JSON when using absolute links" do | ||
current_page = double(path: "/api/pages.json") | ||
sitemap = double(resources: [ | ||
double(url: "/a.html", data: double(title: "A thing", owner_slack: "#2ndline", last_reviewed_on: Date.yesterday, review_in: "0 days")), | ||
double(url: "/b.html", data: double(title: "B thing", owner_slack: "#2ndline", last_reviewed_on: Date.yesterday, review_in: "2 days")), | ||
]) | ||
|
||
json = described_class.new(sitemap, tech_docs: {}).to_json | ||
json = described_class.new(sitemap, {}, current_page).to_json | ||
|
||
expect(JSON.parse(json)).to eql([ | ||
{ "title" => "A thing", "url" => "/a.html", "review_by" => Date.yesterday.to_s, "owner_slack" => "#2ndline" }, | ||
{ "title" => "B thing", "url" => "/b.html", "review_by" => Date.tomorrow.to_s, "owner_slack" => "#2ndline" }, | ||
]) | ||
end | ||
|
||
it "returns the pages as JSON when using relative links" do | ||
current_page = double(path: "/api/pages.json") | ||
sitemap = double(resources: [ | ||
double(url: "/a.html", path: "/a.html", data: double(title: "A thing", owner_slack: "#2ndline", last_reviewed_on: Date.yesterday, review_in: "0 days")), | ||
double(url: "/b/c.html", path: "/b/c.html", data: double(title: "B thing", owner_slack: "#2ndline", last_reviewed_on: Date.yesterday, review_in: "2 days")), | ||
]) | ||
|
||
json = described_class.new(sitemap, { relative_links: true }, current_page).to_json | ||
|
||
expect(JSON.parse(json)).to eql([ | ||
{ "title" => "A thing", "url" => "../a.html", "review_by" => Date.yesterday.to_s, "owner_slack" => "#2ndline" }, | ||
{ "title" => "B thing", "url" => "../b/c.html", "review_by" => Date.tomorrow.to_s, "owner_slack" => "#2ndline" }, | ||
]) | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
RSpec.describe GovukTechDocs::PathHelpers do | ||
include GovukTechDocs::PathHelpers | ||
|
||
describe "#get_path_to_resource" do | ||
it "calculates the path to a resource when using absolute links" do | ||
resource_url = "/documentation/introduction/index.html" | ||
|
||
config = {} | ||
resource = double("resource", url: resource_url) | ||
|
||
resource_path = get_path_to_resource(config, resource, nil) | ||
expect(resource_path).to eql(resource_url) | ||
end | ||
|
||
it "calculates the path to a resource when using relative links" do | ||
current_page_path = "/documentation/introduction/section-one/index.html" | ||
url = "/documentation/introduction/index.html" | ||
|
||
config = { | ||
relative_links: true, | ||
} | ||
resource = double("resource", url: url, path: url) | ||
current_page = double("current_page", path: current_page_path) | ||
|
||
resource_path = get_path_to_resource(config, resource, current_page) | ||
expect(resource_path).to eql("../../../documentation/introduction/index.html") | ||
end | ||
end | ||
|
||
describe "#path_to_site_root" do | ||
it "calculates the path from a page to the site root when using absolute links" do | ||
page_path = "/documentation/introduction/index.html" | ||
|
||
config = { | ||
http_prefix: "/", # This is Middleman's default setting. | ||
} | ||
|
||
path_to_site_root = path_to_site_root(config, page_path) | ||
expect(path_to_site_root).to eql("/") | ||
end | ||
|
||
it "calculates the path from a page to the site root when a middleman http prefix" do | ||
page_path = "/bananas/documentation/introduction/index.html" | ||
|
||
config = { | ||
http_prefix: "/bananas", | ||
} | ||
|
||
path_to_site_root = path_to_site_root(config, page_path) | ||
expect(path_to_site_root).to eql("/bananas/") | ||
end | ||
|
||
it "calculates the path from a page to the site root when using relative links" do | ||
page_path = "/documentation/introduction/index.html" | ||
|
||
config = { | ||
relative_links: true, | ||
} | ||
|
||
path_to_site_root = path_to_site_root(config, page_path) | ||
expect(path_to_site_root).to eql("../../") | ||
end | ||
end | ||
end |
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
Oops, something went wrong.