Skip to content

Commit 3610a45

Browse files
committed
Fix select_top_level_html_files ignoring custom http_prefix
The parent URL check was hardcoded to "/" so top-level pages were filtered out when http_prefix was set to a custom path.
1 parent 7f0b62f commit 3610a45

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

lib/govuk_tech_docs/table_of_contents/helpers.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ def sort_resources_stably(resources)
3030
.to_a
3131
end
3232

33-
def select_top_level_html_files(resources)
33+
def select_top_level_html_files(resources, config)
34+
home_url = config[:http_prefix].end_with?("/") ? config[:http_prefix] : "#{config[:http_prefix]}/"
35+
3436
resources
35-
.select { |r| r.path.end_with?(".html") && (r.parent.nil? || r.parent.url == "/") }
37+
.select { |r| r.path.end_with?(".html") && (r.parent.nil? || r.parent.url == home_url) }
3638
end
3739

3840
def multi_page_table_of_contents(resources, current_page, config, current_page_html = nil)
3941
resources = sort_resources_stably(
40-
select_top_level_html_files(resources),
42+
select_top_level_html_files(resources, config),
4143
)
4244

4345
render_page_tree(resources, current_page, config, current_page_html)

spec/table_of_contents/helpers_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,24 @@ def is_a?(klass)
342342
expect(subject.multi_page_table_of_contents(resources, current_page, config, current_page_html).strip).to eq(expected_multi_page_table_of_contents.strip)
343343
end
344344

345+
it "selects top-level pages whose parent URL matches the http prefix" do
346+
prefix_root = FakeResource.new("/prefix/", "")
347+
resources = []
348+
resources[0] = FakeResource.new("/prefix/index.html", '<h1 id="heading-one">Heading one</h1>', 10, "Index", prefix_root)
349+
resources[1] = FakeResource.new("/prefix/a.html", '<h1 id="heading-one">Heading one</h1>', 20, "Page A", prefix_root)
350+
351+
config = {
352+
http_prefix: "/prefix",
353+
tech_docs: {
354+
max_toc_heading_level: 3,
355+
},
356+
}
357+
358+
result = subject.select_top_level_html_files(resources, config)
359+
360+
expect(result).to eq(resources)
361+
end
362+
345363
it "builds a table of contents from a single page resources" do
346364
resources = []
347365
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>')

0 commit comments

Comments
 (0)