-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support sites deployed on paths other than "/" (by generating relative links) #291
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ae07fc2
TRG-128 - Generate Table of Content links as relative links.
eddgrant 17b5c8a
TRG-128: Make search function work when using generating sites using …
eddgrant fabb8a6
TRG-128: Refactor path helpers out in to their own module.
eddgrant d874c33
TRG-128: Make /api/pages.json endpoint return relative links when the…
eddgrant 73080ca
TRG-128: Make nav bar expanding, collapsing and highlighting work whe…
eddgrant a6b0eb9
Fix Rubocop linting errors.
eddgrant 1f2d71b
Remove TODO
eddgrant 749fcc5
isOnSearchPage: Remove erroneous regular expression wildcard prefix.
eddgrant 1c3ebc8
changeSearchAction(): Don't pass already in-scope variable to function.
eddgrant 54ff817
Replace use of const with var.
eddgrant 840a478
Search: Add data-path-to-site-root attribute.
eddgrant 7ccde80
Use :http_prefix when calculating the path to the site root, when usi…
eddgrant babdeb7
TRG-128: Ensure http_prefix is not duplicated when generating search …
eddgrant d49fef0
TRG-128: Revert back to always obtaining search results from /search.…
eddgrant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's best if this doesn't have the
index.html
file name, to be consistent with other URLs. Is there a reason that you changed it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't include the
index.html
then I think we become reliant on the web server that the site is deployed behind being configured to have a default page value ofindex.html
. Having said that I think there are other areas where we generate links which rely on the default page behaviour, so you could argue this is incongruous with that approach. Happy to try taking it out if you'd prefer consistency here.