Skip to content
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

Fix multipage site build with custom http_prefix #79

Merged
merged 2 commits into from
Apr 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## Unreleased

This release fixes a bug (#79 - reported in
[alphagov/tech-docs-template#183][tdt-183]) which prevented a multipage site
from being generated when a custom `http_prefix` was configured.

[tdt-183]: https://github.com/alphagov/tech-docs-template/issues/183

## 1.8.0

🎉 Our first contributor from outside of GDS. Thanks [@timja](https://github.com/timja) from [HMCTS](https://hmcts.github.io)! 🤝
Expand Down
18 changes: 14 additions & 4 deletions lib/govuk_tech_docs/table_of_contents/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,21 @@ def render_page_tree(resources, current_page, config, current_page_html)
# Avoid redirect pages
next if content.include? "http-equiv=refresh"
# If this page has children, just print the title and recursively
# render the children.
# If not, print the heading structure.
# render the children. If not, print the heading structure.

# We avoid printing the children of the root index.html as it is the
# parent of every other top level file.
if resource.children.any? && resource.url != "/"
# parent of every other top level file. We need to take any custom
# prefix in to consideration when checking for the root index.html.
# The prefix may be set with or without a trailing slash: make sure
# it has one for this comparison check.
home_url =
if config[:http_prefix].end_with?("/")
config[:http_prefix]
else
config[:http_prefix] + "/"
end

if resource.children.any? && resource.url != home_url
output += %{<ul><li><a href="#{resource.url}">#{resource.data.title}</a>\n}
output += render_page_tree(resource.children, current_page, config, current_page_html)
output += '</li></ul>'
Expand Down
51 changes: 51 additions & 0 deletions spec/table_of_contents/helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def add_children(children)
current_page_html = '<h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2>';

config = {
http_prefix: "/",
tech_docs: {
max_toc_heading_level: 3
}
Expand Down Expand Up @@ -173,6 +174,55 @@ def add_children(children)
expect(subject.multi_page_table_of_contents(resources, current_page, config, current_page_html).strip).to eq(expected_multi_page_table_of_contents.strip)
end

it 'builds a table of contents from several page resources with a custom http prefix ocnfigured' do
resources = []
resources[0] = FakeResource.new('/prefix/index.html', '<h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2>', 10, 'Index');
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]);
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]);
resources[0].add_children [resources[1], resources[2]]

current_page = double("current_page",
data: double("page_frontmatter", description: "The description.", title: "The Title"),
url: "/prefix/index.html",
metadata: { locals: {} })

current_page_html = '<h1 id="heading-one">Heading one</h1><h2 id="heading-two">Heading two</h2>';

config = {
http_prefix: "/prefix",
tech_docs: {
max_toc_heading_level: 3
}
}

expected_multi_page_table_of_contents = %{
<ul><li><a href="/prefix/index.html">Index</a>
<ul>
<li>
<a href="/prefix/a.html#heading-one">Heading one</a>
<ul>
<li>
<a href="/prefix/a.html#heading-two">Heading two</a>
</li>
</ul>
</li>
</ul>
<ul>
<li>
<a href="/prefix/b.html#heading-one">Heading one</a>
<ul>
<li>
<a href="/prefix/b.html#heading-two">Heading two</a>
</li>
</ul>
</li>
</ul>
</li></ul>
}

expect(subject.multi_page_table_of_contents(resources, current_page, config, current_page_html).strip).to eq(expected_multi_page_table_of_contents.strip)
end

it 'builds a table of contents from a single page resources' do
resources = []
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>');
Expand All @@ -185,6 +235,7 @@ def add_children(children)
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>';

config = {
http_prefix: "/",
tech_docs: {
max_toc_heading_level: 3,
multipage_nav: true
Expand Down