Skip to content

Commit 49e7aac

Browse files
authored
Merge pull request #79 from alphagov/prefix-fix
Fix multipage site build with custom http_prefix
2 parents 20dfebd + c96749e commit 49e7aac

File tree

3 files changed

+73
-4
lines changed

3 files changed

+73
-4
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
This release fixes a bug (#79 - reported in
6+
[alphagov/tech-docs-template#183][tdt-183]) which prevented a multipage site
7+
from being generated when a custom `http_prefix` was configured.
8+
9+
[tdt-183]: https://github.com/alphagov/tech-docs-template/issues/183
10+
311
## 1.8.0
412

513
🎉 Our first contributor from outside of GDS. Thanks [@timja](https://github.com/timja) from [HMCTS](https://hmcts.github.io)! 🤝

lib/govuk_tech_docs/table_of_contents/helpers.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,21 @@ def render_page_tree(resources, current_page, config, current_page_html)
4545
# Avoid redirect pages
4646
next if content.include? "http-equiv=refresh"
4747
# If this page has children, just print the title and recursively
48-
# render the children.
49-
# If not, print the heading structure.
48+
# render the children. If not, print the heading structure.
49+
5050
# We avoid printing the children of the root index.html as it is the
51-
# parent of every other top level file.
52-
if resource.children.any? && resource.url != "/"
51+
# parent of every other top level file. We need to take any custom
52+
# prefix in to consideration when checking for the root index.html.
53+
# The prefix may be set with or without a trailing slash: make sure
54+
# it has one for this comparison check.
55+
home_url =
56+
if config[:http_prefix].end_with?("/")
57+
config[:http_prefix]
58+
else
59+
config[:http_prefix] + "/"
60+
end
61+
62+
if resource.children.any? && resource.url != home_url
5363
output += %{<ul><li><a href="#{resource.url}">#{resource.data.title}</a>\n}
5464
output += render_page_tree(resource.children, current_page, config, current_page_html)
5565
output += '</li></ul>'

spec/table_of_contents/helpers_spec.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def add_children(children)
140140
current_page_html = '<h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2>';
141141

142142
config = {
143+
http_prefix: "/",
143144
tech_docs: {
144145
max_toc_heading_level: 3
145146
}
@@ -173,6 +174,55 @@ def add_children(children)
173174
expect(subject.multi_page_table_of_contents(resources, current_page, config, current_page_html).strip).to eq(expected_multi_page_table_of_contents.strip)
174175
end
175176

177+
it 'builds a table of contents from several page resources with a custom http prefix ocnfigured' do
178+
resources = []
179+
resources[0] = FakeResource.new('/prefix/index.html', '<h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2>', 10, 'Index');
180+
resources[1] = FakeResource.new('/prefix/a.html', '<h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2>', 10, 'Sub page A', resources[0]);
181+
resources[2] = FakeResource.new('/prefix/b.html', '<h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2>', 20, 'Sub page B', resources[0]);
182+
resources[0].add_children [resources[1], resources[2]]
183+
184+
current_page = double("current_page",
185+
data: double("page_frontmatter", description: "The description.", title: "The Title"),
186+
url: "/prefix/index.html",
187+
metadata: { locals: {} })
188+
189+
current_page_html = '<h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2>';
190+
191+
config = {
192+
http_prefix: "/prefix",
193+
tech_docs: {
194+
max_toc_heading_level: 3
195+
}
196+
}
197+
198+
expected_multi_page_table_of_contents = %{
199+
<ul><li><a href="/prefix/index.html">Index</a>
200+
<ul>
201+
<li>
202+
<a href="/prefix/a.html#heading-one">Heading one</a>
203+
<ul>
204+
<li>
205+
<a href="/prefix/a.html#heading-two">Heading two</a>
206+
</li>
207+
</ul>
208+
</li>
209+
</ul>
210+
<ul>
211+
<li>
212+
<a href="/prefix/b.html#heading-one">Heading one</a>
213+
<ul>
214+
<li>
215+
<a href="/prefix/b.html#heading-two">Heading two</a>
216+
</li>
217+
</ul>
218+
</li>
219+
</ul>
220+
</li></ul>
221+
}
222+
223+
expect(subject.multi_page_table_of_contents(resources, current_page, config, current_page_html).strip).to eq(expected_multi_page_table_of_contents.strip)
224+
end
225+
176226
it 'builds a table of contents from a single page resources' do
177227
resources = []
178228
resources.push FakeResource.new('/index.html', '<h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2><h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2><h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2>');
@@ -185,6 +235,7 @@ def add_children(children)
185235
current_page_html = '<h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2><h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2><h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2>';
186236

187237
config = {
238+
http_prefix: "/",
188239
tech_docs: {
189240
max_toc_heading_level: 3,
190241
multipage_nav: true

0 commit comments

Comments
 (0)