Skip to content

Commit 7ccde80

Browse files
committed
Use :http_prefix when calculating the path to the site root, when using absolute links.
This configuration setting defaults to '/' so we can rely on it to always have a value.
1 parent 840a478 commit 7ccde80

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/govuk_tech_docs/path_helpers.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ def path_to_site_root(config, page_path)
2121
ascents = number_of_ascents_to_site_root.zero? ? ["."] : number_of_ascents_to_site_root.times.collect { ".." }
2222
path_to_site_root = ascents.join("/").concat("/")
2323
else
24-
path_to_site_root = "/"
24+
middleman_http_prefix = config[:http_prefix]
25+
path_to_site_root = middleman_http_prefix.end_with?("/") ? middleman_http_prefix : "#{middleman_http_prefix}/"
2526
end
2627
path_to_site_root
2728
end

spec/govuk_tech_docs/path_helpers_spec.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,25 @@
3131
it "calculates the path from a page to the site root when using absolute links" do
3232
page_path = "/documentation/introduction/index.html"
3333

34-
config = {}
34+
config = {
35+
http_prefix: "/", # This is Middleman's default setting.
36+
}
3537

3638
path_to_site_root = path_to_site_root(config, page_path)
3739
expect(path_to_site_root).to eql("/")
3840
end
3941

42+
it "calculates the path from a page to the site root when a middleman http prefix" do
43+
page_path = "/bananas/documentation/introduction/index.html"
44+
45+
config = {
46+
http_prefix: "/bananas",
47+
}
48+
49+
path_to_site_root = path_to_site_root(config, page_path)
50+
expect(path_to_site_root).to eql("/bananas/")
51+
end
52+
4053
it "calculates the path from a page to the site root when using relative links" do
4154
page_path = "/documentation/introduction/index.html"
4255

0 commit comments

Comments
 (0)